使用updateSelectInput的输入

时间:2014-08-20 22:38:28

标签: r shiny

我的问题是使用来自" updateSelectInput"的输入选择数据。

输入$ State和输入$ Company不会向我的"数据选择"功能,输入$运输不是"更新"工作正常。

使用" updateSelectInput"时如何调用输入?它是否与" selectInput"?

的工作方式相同

提前致谢!

以下是我为简化问题而创建的数据:

    Company<- paste('company',1:95)
    State<- paste('state',1:20)
    Transport<-c('inter long','nacional long','nacional short','inter short')
    variable1<- sample(0:10,380,replace=T)
    variable2<- sample(0:10,380,replace=T)
    variable3<- sample(0:10,380,replace=T)
    variable4<- sample(0:10,380,replace=T)

    mydata<- data.frame(Company,State,Transport,variable1,variable2,variable3,variable4)

ui.R

    shinyUI(pageWithSidebar(
    headerPanel('Passengers Data'),
    sidebarPanel(
    selectInput("Transport", "Select a Transport", choices = levels(mydata$Transport), selected = levels(mydata$Transport)[1]
    ),
   selectInput("State", "Select a State", choices = sort(levels(mydata$State)),selected = sort(levels(mydata$State)[1])),
    tags$hr(),
    checkboxGroupInput("Company", "Select Company", choices = mydata$Company[mydata$State ==sort(mydata$State )[1] & mydata$Transport==sort(mydata$Transport)[1]]),



    width = 3

    ),
    mainPanel(
    htmlOutput("plot1"),width = 9
    )
    ))

server.R

    library(ggplot2)
    library(googleVis)
    shinyServer(function(input, output, session) {

    observe({
    Transport2<- input$Transport
    updateSelectInput(session, "State", choices = sort(levels(factor(mydata$State[mydata$Transport == Transport2]))),selected = sort(levels(factor(mydata$State[mydata$Transport == Transport2])))[1])
    })

    observe({
    Transport2<- input$Transport
    State2 <- input$State

    updateCheckboxGroupInput(session, "Company", choices = sort(levels(factor(mydata$Company[mydata$State == State2 & mydata$Transport == Transport2]))),selected=sort(levels(factor(mydata$Company[mydata$State == State2 & mydata$Transport == Transport2])))[1:5])
    })


    selectedData<-reactive({
    subset(mydata, mydata$Transport==input$Transport & mydata$State==input$State & mydata$Company==input$Company
    )
    })  




    output$plot1 <- renderGvis({

    gvisBarChart(selectedData(), xvar="Company", yvar="variable1",
                         options=list(legend='none', width=750, height=425,title="Resultado geral",
                                      titleTextStyle="{color:'black',fontName:'Courier',fontSize:16}",
                                      bar="{groupWidth:'70%'}"))



    })

    })

1 个答案:

答案 0 :(得分:2)

首先,这一行有一个错误:

subset(mydata, mydata$Transport==input$Transport & mydata$State==input$State & mydata$Company==input$Company

特别是最后一句:

mydata$Company==input$Company

应替换为

mydata$Company %in% input$Company

因为输入$ Company可以有多个值。


此外,您传递给updateSelectInput的selected值有时是c(NA, NA, NA, NA, NA),Shiny并不理解(它会在浏览器中导致JavaScript错误)。在将其提供给updateSelectInput之前,您应该在选择上调用na.omit。 (如果这会使事情变得更糟,请从CRAN安装最新版本的RJSONIO - 它曾经有一个列表中有空向量的错误。)


第三,我建议将此代码添加到renderGvis表达式的顶部:

validate(
  need(nrow(selectedData()) > 0, "No data")
)

它可以更容易地检测数据中何时没有行(而不是可能是错误或其他结果的空白图表)。