Soren
Soren

Reputation: 53

SelectInput in Shiny

I am trying to build a simple Shiny App, but cant get it to work. I want to select a state and then the app should calculate the mean of that state for sample.measurement of ozone level. Here is my ui.R code:

require(shiny)
fluidPage(pageWithSidebar(
headerPanel("Ozone Pollution"),
sidebarPanel(
h3('State'),selectInput("inputstate","Select State",state.name)),
mainPanel(
h3('Results'),verbatimTextOutput("res")
)
))

And here is my server.R program:

require(dplyr)
library(shiny)
shinyServer(
function(input, output) {
stat_state<-reactive({filter(ozone_2015,State.Name==input$inputstate)})
output$res<- renderPrint({mean(stat_state$Sample.Measurement)})
}
)

Any Hints? Thanks.....

Upvotes: 0

Views: 541

Answers (1)

Balter
Balter

Reputation: 1095

While I can't replicate your dataset because I don't know where ozone_2015 comes from, I think your problem is that you're not referring to "reactive" objects like this:

stat_state()

Once you make a reactive object, with the exception of reactive values and input$ variables, you need to refer to it with '()' at the end of the variable.

Here is an example using some of your code with a different dataset. Hope this helps.

require(shiny)

ui <- 
fluidPage(pageWithSidebar(
  headerPanel("Population"),
  sidebarPanel(
    h3('State'),selectInput("inputstate","Select State",state.name)),
  mainPanel(
    h3('Results'),verbatimTextOutput("res")
  )
))

server <- function(input,output){
  require(dplyr)

  sample.data <- reactive({as.data.frame(state.x77)})

      stat_state <- reactive({sample.data()[which(row.names(sample.data()) == input$inputstate),]})
      output$res <- renderPrint({stat_state()$Population})
    }
  )
}


shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions