在 Shiny 中使用 googleVis

Use googleVis in Shiny

我正在开发一款闪亮的应用程序,让用户能够 select 绘制不同变量的时间线图。为了说明,我想使用以下数据:

datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
                    Name=c("Washington", "Adams", "Jefferson",
                           "Adams", "Jefferson", "Burr"),
                    start=as.Date(x=rep(c("1789-03-29", "1797-02-03", 
                                          "1801-02-03"),2)),
                    end=as.Date(x=rep(c("1797-02-03", "1801-02-03", 
                                        "1809-02-03"),2)))

这是我想出的办法:

#ui.R
library(shiny)
library(googleVis)
shinyUI(bootstrapPage(
  selectInput('id','select a name',choices = as.character(unique(datTL$Name))),
  htmlOutput('timeline')
))

#server.R
shinyServer(function(input,output){
  datainput=reactive({daTL[daTL$Name==input$id,]})
  output$timeline=renderGvis({gvisTimeline(data=datTL,
                          rowlabel="Name",
                          barlabel="Position",
                          start="start",
                          end="end")})
})

使用我的代码,我可以创建一个带有下拉列表的应用程序,其中包括总统的姓名和所有总统的时间线图。我正在努力的是使用 link 带有时间轴图的下拉列表,以便用户可以通过 selecting 列表中的名称来过滤图。有人可以帮帮我吗?

这是工作服务器代码。您的代码中有一些错字,反应式也用作 datainput().

shinyServer(function(input,output){
    datainput=reactive({datTL[datTL$Name==input$id,]})
    output$timeline=renderGvis({
        gvisTimeline(data=datainput(),
                                             rowlabel="Name",
                                             barlabel="Position",
                                             start="start",
                                             end="end")})
})