How to ask R Shiny to create several “select boxes” - based on previous input

Hello!

In my tiny Shiny app I am asking the user: how many time periods do you want to cut your time series into? For example, the user selects 3. I want to use this input to take a fixed vector of dates and make it possible for the user the select from it the desired last date of Time Period 1 (in select box 1), and Time Period 2 (in select box 2). (The last date for time period 3 will be the very last date, so I don't need to ask).

I am not sure how to do it. I understand that because I don't know the desired number of time periods in advance, I have to create a list. But how do I then collect the input from those select boxes?

Thanks a lot!

library(shiny)

### UI #######################################################################

ui = shinyUI(fluidPage(

  titlePanel("Defining time periods"),

  # Sidebar: 
  sidebarLayout(
    sidebarPanel(
      # Slider input for the number of time periods:
      numericInput("num_periodsnr", label = "Desired number of time periods?",
                  min = 1, max = 10, value = 2),
      uiOutput("period_cutpoints")
    ),


    # Show just the number of periods so far.
    mainPanel(
      textOutput("nr_of_periods")
    )
  )
))



### SERVER ##################################################################

server = shinyServer(function(input, output, session) {

  library(lubridate)

  output$nr_of_periods <- renderPrint(input$num_periodsnr)

    # Define our dates vector:
  dates <- seq(ymd('2016-01-02'), ymd('2017-12-31'), by = '1 week')


  # STUCK HERE:
  # output$period_cutpoints<-renderUI({
  #   list.out <- list()
  #   for (i in 1:input$num_periodsnr) {
  #     list.out[[i]] <- renderPrint(paste0("Sometext", i), ,
  #                                  )
  #   }
  #   return(list.out)
  # })

})

# Run the application 
shinyApp(ui = ui, server = server)

I think I figured it out - partly. But I don't know how to show the dates chosen by the user in the mainPanel

library(shiny)

### UI ######################################################################

ui = shinyUI(fluidPage(
  
  titlePanel("Defining time periods"),
  
  # Sidebar: 
  sidebarLayout(
    sidebarPanel(
      # Numeric input (box) for the number of time periods:
      numericInput("num_periodsnr", label = "Desired number of time periods?",
                  min = 1, max = 10, value = 2),
      uiOutput("period_cutpoints")
    ),

    # Show the number of time periods chosen:
    mainPanel(
      textOutput("nr_of_periods")
    )
  )
))



### SERVER ######################################################################

server = shinyServer(function(input, output, session) {
  
  library(lubridate)
  output$nr_of_periods <- renderPrint(input$num_periodsnr)

    # Define our dates vector:
  dates <- seq(ymd('2016-01-02'), ymd('2017-12-31'), by = '1 week')
  
  
  output$period_cutpoints <- renderUI({
    
    if (is.null(input$num_periodsnr)) {return()}
    list.out <- list()
    for (i in 1:input$num_periodsnr) {
       list.out[[i]] <- selectInput(paste0("boxes_", i), label = paste0("Select end of period ", i), 
                                    choices = dates, selected = dates[2])
     }
     return(list.out)
  })
})

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.