Using the input from updateSelectInput

有些话、适合烂在心里 提交于 2020-01-17 02:23:07

问题


My problem is when selecting data using the input from "updateSelectInput".

input$State and input$Company returns no values to my "data selection" function, input$Transport that is not an "update" works fine.

How I call an input when using "updateSelectInput"? Does it work in the same way as "selectInput"?

Thanks in advance!

Here is the data I created to simplify the problem:

    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:


First, there's a bug in this line:

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

Specifically the last clause:

mydata$Company==input$Company

should be replaced with

mydata$Company %in% input$Company

since input$Company can have multiple values.


Also, the selected values you're passing to updateSelectInput are sometimes c(NA, NA, NA, NA, NA) which Shiny doesn't understand (it causes a JavaScript error in the browser). You should call na.omit on the selection before you give it to updateSelectInput. (If this makes things even worse, install the latest version of RJSONIO from CRAN--it used to have a bug with empty vectors in lists.)


Thirdly, I'd recommend adding this code to the top of your renderGvis expression:

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

It will make it easier to detect when there are no rows in your data (rather than a blank graph which could be the result of an error or something).



来源:https://stackoverflow.com/questions/25415613/using-the-input-from-updateselectinput

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!