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