Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependent inputs in Shiny application with R

Tags:

r

shiny

Say I have a shiny::sliderInput:

...
sliderInput("input_1", "Title_1",
            min = 1, max = 10, value = 5)
...

Is it possible to reference min, max, and/or value in a different sliderInput? The use case for this is to make a second a input dependent on the first input. Something to the tune of the minimum of the second input can never be less than the value from input_1.

Something like (this does not work):

sliderInput("input_2", "Title_2",
             min = input_1$value, max = 10, value = input_1$value)

My hunch is this might be possible with renderUI, but not sure where or how to start?

like image 525
JasonAizkalns Avatar asked Jan 09 '23 03:01

JasonAizkalns


2 Answers

This is an example of defining a widget in server.R:

library(shiny)
shiny::runApp(list(
  ui = fluidPage(
    numericInput("input_2", "select min value", value = 5),
    uiOutput("input_1")
  ),
  server = function(input, output) {
    output$input_1 <- renderUI({
      sliderInput("input_1", "Title_1", min = input$input_2, max = 10, value = 5)
    })

  }
))

So it is reactive to changes in ui.R

like image 151
Andriy T. Avatar answered Jan 10 '23 17:01

Andriy T.


An example of updateSliderInput in shiny rmd

---
title: "Dependent Inputs"
runtime: shiny
output:
    html_document
---
```{r}
sliderInput("n", "n", min=0, max=100, value=1)
sliderInput("n2", "n2", min=0, max=100, value=1)

observe({
    updateSliderInput(session, "n", min=input$n2-1, max=input$n2+1, value=input$n2)
})
```
like image 26
Rorschach Avatar answered Jan 10 '23 18:01

Rorschach