繁体   English   中英

Shiny:根据单选按钮选择存储值?

[英]Shiny: Store values according to the radio button choice?

鉴于以下 Shiny 应用程序,我想单击一个单选按钮并设置我的输入并将其存储在reactiveVariables 中。 我的目标是获得适当的输入并将其保存到基于单选按钮选择的变量中。

这是我到目前为止所做的:

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)

我对 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])))
        }
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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