0

I am creating a Shiny app where I would like to have the default of a numericInput be dependent on another default to a previously defined numericInput.

e.g.,

Here I would like the numericInput elements of (2) to be the reciprocal of (1), without having to specify values for value,min,max, and step beforehand:

(1) numericInput("obs1", "Label1", value = 10, min = 10, max = 20, step = 1)

(2) numericInput("obs2", "Label2", value = 1/10, min = 1/10, max = 1/20, step = 1)

Above (1) is the previously-defined numericInput.

Is there a simple way to do this?

1 Answer 1

3

If you want an input object to have dynamic parameters you need to use uiOutput, so that you can generate them in runtime (in server.R).

Example: In the first column you can set min, max and value. Modifying any of them renders obs1 and obs2 with new parameter values.

library(shiny)

ui <- fluidPage(
  column(6, 
         tags$h2("Set parameters"),
         numericInput("value", "Value", value = 20, min = 10, max = 60, step = 10),
         numericInput("min", "Min", value = 10, min = 0, max = 30, step = 10),
         numericInput("max", "Max", value = 40, min = 40, max = 60, step = 10)
  ),
  column(6,
         uiOutput("ui")
  )
)

server <- function(input, output, session) {
  output$ui <- renderUI( {
    tagList(
      tags$h2("Numeric inputs that depend on reactive data"),
      numericInput("obs1", "Label1", value = input$value, min = input$min, max = input$max, step = 1),
      numericInput("obs2", "Label2", value = input$value + 5, min = input$min - 5, max = input$max + 5, step = 1)
    )
  })
}

shinyApp(ui, server)

Please note that you need to wrap elements in tagList, when you want to pass more than one input element to renderUI.

Sign up to request clarification or add additional context in comments.

2 Comments

What if, in addition to a user being able to input their own numericInput values, I'd also like to have numericInput values be dependent on those found in an uploaded file? In this case, values to numericInputs will be filled-in automatically by the system. Is the idea the same?
Yes, the idea is exactly the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.