使用Javascript在Shiny中配置googleVis事件监听器

Han*_*phy 4 javascript r shiny googlevis

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

我可以通过将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)); "
Run Code Online (Sandbox Code Playgroud)

使用此代码,我运行了我的程序,选择了"2015年6月16日"框,我的浏览器上显示了一个新标签:https://en.wikipedia.org/wiki/Tue_Jun_16_2015_00 : 00 : 00_GMT- 0400_(EDT)

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

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

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

Nic*_*icE 7

您可以使用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)
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我将日期解析为Y​​YYY/M/D,如果你想保持javascript长日期格式,你也可以返回selected_date.toString()而不是parsed_date.