简体   繁体   中英

Using checkboxGroupInput as minimum for numeric Input, Shiny R

In my Shiny app I have a tabPanel Second page in which the user inputs some values eg value1, value2, value3. Then I have another tabPanel First page, it consists of checkboxGroupInput in which the user chooses which values should be used for calculating the minimum of inserted values in tabPanel Second page. Is it possible that this min of values is used as initial value of another NumericInput in tabpanel Second page?

Here I have an example for three choices, but actually I have 6, so segregating choices with if & ifelse (like here: R shiny : checkboxGroupInput value ) would be a really long way. Is there a smoother way?

Thank you for your answers.

Eg

ui<- navbarPage(title=div("Problem"),
  tabPanel (title="First page",
            fluidRow(column(2,numericInput("value", "Insert a value", value = **min(of selected values)**, min = 0, max=5, step=0.0001),
            checkboxGroupInput("variable", "Inital values expressed as min of:",
                               choices=c("House",
                                         "Apartment",
                                         "Car" 
                               ))))),

            tabPanel (title="Second page", fluidRow(column(2,
                      numericInput("value1", "House", value =0.02, min = 0, max=5, step=0.0001),
                      numericInput("value2", "Apartment", value =1, min = 0, max=5, step=0.0001),
                      numericInput("value3", "Car", value =0.15,min = 0, max=5, step=0.0001)))))
server <- function(input, output) { }

shinyApp(ui, server)    

You can have this functionality with a following example app.R

library(shiny)
ui<- fluidPage(title=div("Problem"),
               tabsetPanel(
                 tabPanel(title = "First page", 
                   uiOutput("reactiveInput"),
                   checkboxGroupInput("variable", "Inital values expressed as min of:",
                                    choices=c("House"=1, "Apartment"=2, "Car"=3))), 
                 tabPanel (title = "Second page",
                           numericInput("value1", "House", value =0.02, min = 0, max=5, step=0.0001),
                           numericInput("value2", "Apartment", value =1, min = 0, max=5, step=0.0001),
                           numericInput("value3", "Car", value =0.15,min = 0, max=5, step=0.0001))
                ))

server <- function(input, output) {

  insertedValue <- reactive({
    variables <- c(input$value1, input$value2, input$value3)
    value <- min(variables[as.numeric(input$variable)])
  })

  output$reactiveInput <- renderUI ({
    insertedValue <- insertedValue()
    numericInput("value", "Insert a value", value = insertedValue, min = 0, max=5, step=0.0001)
  })

}

shinyApp(ui, server)    

If you want an input element to react to the previous user input, it usually has to be rendered with renderUI and uiOutput . The needed value is extracted in a reactive block, and further directed to the input element inside the renderUI .

You can skip the endless-feeling if...else by using the checkboxGroupInput result as numeric index for the numericInput vector, just make sure you use the same order in the vector and checkboxGroupInput .

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