【发布时间】:2020-07-12 01:09:41
【问题描述】:
鉴于以下 Shiny 应用程序,我想单击一个单选按钮并设置我的输入并将其存储在反应变量中。 我的目标是获取适当的输入并将其保存到基于单选按钮选择的变量中。
这是我到目前为止所做的:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
verbatimTextOutput("queryText"),
sidebarLayout(
sidebarPanel(
radioButtons(
inputId = "type",
label = "Reminder Type",
choices = c("Single Date Reminder" = "single",
"Multi Date Reminder" = "multi",
"From-To Reminder" = "from_to"),
selected = "single", width = '100%'
)
),
mainPanel(
conditionalPanel(
condition = "input.type == 'single'",
airDatepickerInput(
inputId = "datetime",
label = "Pick date and time:",
timepicker = TRUE,
clearButton = TRUE,
update_on = "change"
)
),
conditionalPanel(
condition = "input.type == 'multi'",
airDatepickerInput(
inputId = "multiple",
label = "Select multiple dates:",
placeholder = "You can pick 10 dates",
multiple = 10,
timepicker = TRUE,
clearButton = TRUE
),
),
conditionalPanel(
condition = "input.type == 'from_to'",
airDatepickerInput(
inputId = "range",
label = "Select range of dates:",
range = TRUE,
value = c(Sys.Date()-7, Sys.Date()),
clearButton = TRUE
),
airDatepickerInput(
inputId = "range_time",
label = "Pick Time:",
timepicker = TRUE,
onlyTimepicker = TRUE,
clearButton = TRUE
)
)
)
),
tableOutput('show_inputs')
)
server <- function(input, output, session) {
output$queryText <- renderText({
query <- parseQueryString(session$clientData$url_search)
paste("Reminder for ", query[['drug']], sep = "")
})
AllInputs <- reactive({
x <- reactiveValuesToList(input)
data.frame(
names = names(x),
values = unlist(x, use.names = FALSE)
)
})
output$show_inputs <- renderTable({
AllInputs()
})
}
shinyApp(ui, server)
【问题讨论】:
-
你不能用不均匀的行来制作数据框,例如,如果你选择了三个 3 行的日期,但你的从到输出是 2 行
-
@Bruno 如果我点击单选按钮,如何收集输出?
-
我认为您的意思是提交底部,如果您只是将输入保留在那里,您已经可以访问输入以使用 input$x
-
我也强烈建议您不要像在 AllInputs 中那样取消嵌套输入,分别处理每个输入,您的代码将变得不那么复杂
-
我想你可以阅读讨论 community.rstudio.com/t/…,不鼓励在闪亮的应用程序上保存持久数据还有这个优秀的 rstudio conf talk resources.rstudio.com/rstudio-conf-2020/…