【问题标题】:Using Javascript to configure a googleVis event listener in Shiny在 Shiny 中使用 Javascript 配置 googleVis 事件监听器
【发布时间】:2015-08-11 14:32:59
【问题描述】:

基本上,我有一个来自 Shiny 应用程序中 googleVis 包的 gvisCalendar 图表,我想在图表下方显示一个与选定框相对应的数据表。

我可以通过将 gvis.listener.jscode 参数设置为包含 JavaScript 代码字符串的变量来添加事件侦听器。例如,使用此代码,我可以拉出所选日历日期的维基百科页面:

output$dates_plot <- renderGvis({
      gvisCalendar(calendar.ddply,
                   options = list(
                                  colorAxis = "{
                                      minValue: 0,  
                                      colors: ['E9967A', 'A52A2A']
                                   }",
                  gvis.listener.jscode = jscode2 )
          )
    })

    jscode2<- "window.open('http://en.wikipedia.org/wiki/'
              + data.getValue(chart.getSelection()[0].row,0)); "

使用这段代码,我运行了我的程序,选择了“2015 年 6 月 16 日”框,然后在我的浏览器上出现了一个新标签页:https://en.wikipedia.org/wiki/Tue_Jun_16_2015_00:00:00_GMT-0400_(EDT)

我实际上并不想对维基百科做任何事情,我只是以它为例。

我要做的就是将所选日历框的日期保存为 R 对象,以便我可以显示与该日期对应的数据的数据表。

我几乎没有使用 javascript 的经验。 谢谢!

【问题讨论】:

    标签: javascript r shiny googlevis


    【解决方案1】:

    您可以使用Shiny.onInputChange 将数据发送回服务器。 这是一个例子:

    library(shiny)
    library(googleVis)
    server <- function(input, output) {
            output$dates_plot <- renderGvis({
                    gvisCalendar(Cairo,
                                 options = list(
                                         colorAxis = "{
                                          minValue: 0,  
                                          colors: ['E9967A', 'A52A2A']
                                       }",
                                         gvis.listener.jscode = "
                                         var selected_date = data.getValue(chart.getSelection()[0].row,0);
                                         var parsed_date = selected_date.getFullYear()+'-'+(selected_date.getMonth()+1)+'-'+selected_date.getDate();
                                         Shiny.onInputChange('selected_date',parsed_date)")
                    )
            })
            output$date <- renderText({
                   input$selected_date
            })
    }
    
    ui <- shinyUI(fluidPage( 
            htmlOutput("dates_plot"),
            textOutput("date")
    ))
    
    shinyApp(ui = ui, server = server)
    

    在此示例中,我将日期解析为 YYYY/M/D,如果您想保留 javascript 长日期格式,也可以返回 selected_date.toString() 而不是 parsed_date

    【讨论】:

    • 非常感谢!我很感激
    • NicE 的解决方案非常适合作为普通闪亮的应用程序使用。然而,当代码被移动到一个闪亮的模块中时, textOuput("date") 不再打印了。我怀疑这是由于名称空间。不确定shinyjs 是否可以在这里工作。
    • 只想分享Joe Cheng at the comment section of the article 提供的解决方案,以便NicE 的代码可以在闪亮的模块中工作。
    猜你喜欢
    • 2017-03-02
    • 2020-01-27
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多