Conditional initial values in shiny UI?

╄→гoц情女王★ 提交于 2020-01-15 10:15:57

问题


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

来源:https://stackoverflow.com/questions/38405484/conditional-initial-values-in-shiny-ui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!