Interactive / Reactive change of min / max values of sliderInput

邮差的信 提交于 2019-12-31 22:25:23

问题


I've found some information about how to change the value of a sliderInput with a reactive expression within the siderbarPanel. But instead of the value, I'd like to change min and max of the slider with a numericInput. In this script for server.R it says that only label and value can be changed for sliders. Is there any other possibility to change the min/max values of sliderInput with a reactive expression?

Here's an exmple:

ui.R:

shinyUI(pageWithSidebar(

  #Sidebar with controls to select the variable to plot
  sidebarPanel(


 #Numeric Inputs
    numericInput("min_val", "Enter Minimum Value", 1993),

    numericInput("max_val", "Enter Maximum Value", 2013),



#Slider 
    sliderInput("inSlider", "Slider", 
                min=1993, max=2013, value=2000),

# Now I would like to change min and max from sliderInput by changing the numericInput.

mainPanel()
))

server.R:

library(shiny)

shinyServer(function(input, output, session) {

reactive({

x<-input$min_val

y<-input$max_val


updateSliderInput(session, "inSlider", min=x, max=y, value=x)

})

回答1:


I think this is best accomplished by using shiny's dynamic UI functions via renderUI() and uiOutput(). Try out the following example:

ui.R

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel("Test Shiny App"),

  sidebarPanel(
    #Numeric Inputs
    numericInput("min_val", "Enter Minimum Value", 1993),
    numericInput("max_val", "Enter Maximum Value", 2013),
    #display dynamic UI
    uiOutput("slider")
  ),

  mainPanel()
))

server.R

library(shiny)

shinyServer(function(input, output, session) {

  #make dynamic slider
  output$slider <- renderUI({
    sliderInput("inSlider", "Slider", min=input$min_val, max=input$max_val, value=2000)
  })

})


来源:https://stackoverflow.com/questions/18700589/interactive-reactive-change-of-min-max-values-of-sliderinput

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