当前位置: 动力学知识库 > 问答 > 编程问答 >

r - Using the input from updateSelectInput

问题描述:

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%'}"))

})

})

网友答案:

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).

分享给朋友:
您可能感兴趣的文章:
随机阅读: