1

Is there a way to make the initial value of a Shiny UI input conditional?

Consider the below example using the Old Faithful Eruptions app example from Shiny's homepage

I would like to do something akin to having the individual observations be turned off initially except for when input$n_breaks == 50

ui.R

shinyUI(bootstrapPage(

selectInput(inputId = "n_breaks",
          label = "Number of bins in histogram (approximate):",
          choices = c(10, 20, 35, 50),
          selected = 20),

checkboxInput(inputId = "individual_obs",
            label = strong("Show individual observations"),
            value = FALSE),

checkboxInput(inputId = "density",
            label = strong("Show density estimate"),
            value = FALSE),

plotOutput(outputId = "main_plot", height = "300px"),

# Display this only if the density is shown
conditionalPanel(condition = "input.density == true",
               sliderInput(inputId = "bw_adjust",
                           label = "Bandwidth adjustment:",
                           min = 0.2, max = 2, value = 1, step = 0.2))))

server.R

shinyServer(function(input, output) {

output$main_plot <- renderPlot({

hist(faithful$eruptions,
     probability = TRUE,
     breaks = as.numeric(input$n_breaks),
     xlab = "Duration (minutes)",
     main = "Geyser eruption duration")

if (input$individual_obs) {
  rug(faithful$eruptions)
}

if (input$density) {
  dens <- density(faithful$eruptions,
                  adjust = input$bw_adjust)
  lines(dens, col = "blue")}
})
})
TClavelle
  • 578
  • 4
  • 12
  • 1
    You want the control hidden, or the effect changed? If the former, wrap it in a `conditionalPanel`; if the latter, update the condition for when `rug` gets called. – alistaire Jul 15 '16 at 21:56
  • The latter, I want the effect changed. Can you elaborate on how I can update the condition when `rug` gets called for a specific `input$individual_obs` value? – TClavelle Jul 15 '16 at 22:06
  • Just `if (input$n_breaks == 50) { rug(faithful$eruptions) }`. You should probably get rid of the control, then, too, as it won't do anything. – alistaire Jul 15 '16 at 22:18
  • I don't want to remove the control entirely, I simply would like the default view for that bin size to include the rugs. I understand how your answer works, but it does not answer my question of making the initial value conditional. – TClavelle Jul 15 '16 at 22:25
  • 1
    Ah. You could make the default for `"individual_obs"` conditional using `renderUI`, but that adds a level of complexity to your code, as some UI code will end up in the server file. – alistaire Jul 15 '16 at 22:34

0 Answers0