简体   繁体   中英

How to dynamically generate Shiny sidebarPanel contents?

I'm trying to create an app where the user is initially presented with a sidebar selectInput containing a list of choices: A, B, C and D – populated by file-system contents.

Depending on which choice the user selects, I want to populate the rest of the sidebar with choice-specific contents. For example, if the user chooses "A", then the sidebar will contain an additional selectInput and dateRangeInput .

To keep the code clean, I've kept all "A"-specific code in handle_A.R , etc. This means that eventually when I decide to add a new choice "E", I just need to put all the code in handle_E.R .

ui.R

pageWithSidebar(
  headerPanel("Test"),
  sidebarPanel(
    selectizeInput("choice", "Choice:", c()),
    uiOutput("sidebar")
  ),
  mainPanel(uiOutput("main"))
)

server.R (relevant bits)

output$sidebar <- renderUI({
  sidebarRenderer[[input$choice]](input, output, session)
})

I have code in server.R that sources all the handle_*.R scripts – each registering their callbacks.

So far, so good. I can select various choices and the relevant callbacks are called. However, I don't know how to implement the callbacks such that I can update the sidebar widgets as the user interacts with the sidebar. I want to essentially do the following (which will not work, because the function needs to return something to renderUI ):

handle_A.R

sidebarRenderer[["A"]] <<- function(input, output, session) {
  selectInput("day", "Day:", c("Mon", "Wed", "Fri"))
  dates <- getDateList(input$day)
  dateRangeInput("date", "Date:", start=dates[0], end=dates[length(dates)])
  if (hasPublicHoliday(dates))
    checkboxInput("ignoreHolidays", "Ignore public holidays")
}

So what I want is to automatically update dateRangeInput to the calculated start and end dates for the corresponding Mon/Wed/Fri. Furthermore, if any of the dates contain a public holiday, I want to display an extra checkbox to let the user ignore public holidays.

If anybody can help me out, I'd greatly appreciate it!

事实证明,我所需要的只是将逻辑放入observe并调用updateCheckBoxInput

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM