简体   繁体   中英

Shiny: Store values according to the radio button choice?

Given the following Shiny app, I want to click on a radio button and set my inputs and store it in reactiveVariables. My goal is to get the appropriate input and save it to a variable based on the radio button selection.

Here is what I have done so far:

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)

I'm not very familiar with the airDatepickerInput and I got an error from your range_time input so I removed it. In any case, you probably want a reactive(...) with some if-else logic to regularize the user's choices. You can try this:

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)

You could also save a more robust reactive like in the following:

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

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