0

I am a newbie in Rshiny, right now I try to design a dynamic ui Rshiny. Attached is my ui.R and server.R. It runs out an error. This is a dataset storms in different years.

ui.R
library(shiny)
library(hurricaneexposure)
library(hurricaneexposuredata)


data("hurr_tracks")
storms <- unique(hurr_tracks$storm_id)
storm_years <- as.numeric(gsub(".+-", "", storms))
storms <- storms[storm_years <= 2011]

years <- unique(storm_years)
years <- years[years<=2011]


## Split storm_id based on same year
stm <- split(storms, gsub(".+-", "", storms))


shinyUI(fluidPage(

  # Application title
  titlePanel("Hurricane"),

  sidebarLayout(
    sidebarPanel(
      selectInput("years",label = "years",years),

  # This outputs the dynamic UI component
      uiOutput("ui")
    ),
  mainPanel()
  )

))

And

server.R
shinyServer(function(input, output) {

output$ui <- renderUI({

switch (input$years,
  "1988"=selectInput("storm_id",label="storm_id",stm$`1988`)
)


})

As you can see in the ui part. I create variables "years" and "stm". "years" includes all years from 1988 to 2011. "stm" is a split dataset which is to split storms based on years.

> head(stm)


$`1988`
[1] "Alberto-1988"  "Beryl-1988"    "Chris-1988"    "Florence-1988" "Gilbert-1988"  "Keith-1988" 
$`1989`
[1] "Allison-1989" "Chantal-1989" "Hugo-1989"    "Jerry-1989"     

MY goal is to create two "selectInput" whose names are "years" and "storms". In selectInput for years, I can select years from 1988 to 2011, for example, if I select 1989, then the second selectInput will show only the storms in 1989, which are

"Allison-1989" "Chantal-1989" "Hugo-1989"    "Jerry-1989"

Any good suggestions?

Bratt Swan
  • 1,068
  • 3
  • 16
  • 28
  • I'm also new to shiny and I had this question too. What I did is I create a reactive element that reacts to the change in your input (ie. input$years) and creates a subset of the data based on that. Something along the lines of df[df$year == input$years,] inside a reactive. – carlo Jul 29 '16 at 17:04

1 Answers1

2

You should look into updateSelectInput, which is a function that lets you change your selectInputs that are already rendered on the ui side.

Look at this page for more detail.

With this, you can have a proxy selectInput for the storms and whenever the year input changes, you can alter the choices for the selectInput to the right ones.

Sorry, I could not test my solution, because I don't have your data sets. But try out the idea and comment + ask, if this isn't working for you.

library(shiny)
library(hurricaneexposure)
library(hurricaneexposuredata)

data("hurr_tracks")
storms <- unique(hurr_tracks$storm_id)
storm_years <- as.numeric(gsub(".+-", "", storms))
storms <- storms[storm_years <= 2011]

years <- unique(storm_years)
years <- years[years<=2011]

## Split storm_id based on same year
stm <- split(storms, gsub(".+-", "", storms))

ui <- shinyUI(fluidPage(
  # Application title
  titlePanel("Hurricane"),
  sidebarLayout(
    sidebarPanel(
      selectInput("years", label = "years", years),
      selectInput("storm_id", label = "Storms", choices = NULL)
    ),
    mainPanel()
  )
))

server <- function(input, output, session) {
  observeEvent(input$years, {
    updateSelectInput(session, "storm_id", choices = stm[[input$years]])
  })
}

shinyApp(ui, server)
K. Rohde
  • 9,439
  • 1
  • 31
  • 51