[英]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. 我正在尝试创建一个应用程序,在该应用程序中,最初向用户显示的侧边栏selectInput
包含选项列表:A,B,C和D –由文件系统内容填充。
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
. 例如,如果用户选择“ A”,则侧边栏将包含附加的selectInput
和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
. 为了保持代码干净,我将所有“ A”特定的代码都保留在handle_A.R
等中。这意味着最终当我决定添加新的选择“ E”时,我只需要将所有代码放入handle_E.R
中handle_E.R
。
ui.R ui.R
pageWithSidebar(
headerPanel("Test"),
sidebarPanel(
selectizeInput("choice", "Choice:", c()),
uiOutput("sidebar")
),
mainPanel(uiOutput("main"))
)
server.R (relevant bits) server.R (相关位)
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. 我在server.R
中有代码,该代码可获取所有handle_*.R
脚本–每个脚本都注册其回调。
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
): 我本质上想执行以下操作(由于该函数需要向renderUI
返回某些内容,因此将无法正常工作):
handle_A.R 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. 所以我想要的是自动将dateRangeInput
更新为相应的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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.