鉴于以下Shiny应用程序,我想单击一个单选按钮并设置输入并将其存储在reactVariables中。 我的目标是获取适当的输入,并根据单选按钮的选择将其保存到变量中。
这是我到目前为止所做的:
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)
答案 0 :(得分:1)
我对airDatepickerInput不太熟悉,并且您的range_time输入出现错误,因此将其删除。在任何情况下,您可能都希望使用带有某些if-else逻辑的react(...)来规范化用户的选择。您可以尝试以下方法:
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,
timepicker = TRUE
),
)
)
)
)
server <- function(input, output, session) {
output$queryText <- renderText({
query <- parseQueryString(session$clientData$url_search)
paste("Reminder for ", query[['drug']], " on date(s): ", paste0(AllInputs(), collapse = "; "), sep = "")
})
AllInputs <- reactive({
if(input$type == "single"){
return(input$datetime)
}
if(input$type == "multi"){
return(input$multiple)
}
if(input$type == "from_to"){
return(input$range)
}
})
}
shinyApp(ui, server)
您还可以保存更强大的反应式,如下所示:
server <- function(input, output, session) {
output$queryText <- renderText({
query <- parseQueryString(session$clientData$url_search)
paste("Reminder for ", query[['drug']], " on date(s): ", AllInputs()$pretty, sep = "")
})
AllInputs <- reactive({
if(input$type == "single"){
return(list("raw" = input$datetime,
"type" = "single",
"pretty" = input$datetime))
}
if(input$type == "multi"){
return(list("raw" = input$multiple,
"type" = "multi",
"pretty" = paste0(input$multiple, collapse = "; ")))
}
if(input$type == "from_to"){
return(list("raw" = input$range,
"type" = "range",
"pretty" = paste0(input$range[1], " to ", input$range[2])))
}
})
}