Dynamically colored sliderInput

≯℡__Kan透↙ 提交于 2020-01-01 14:27:21

问题


I have a question that is related to post How to color sliderbar (sliderInput)?.

Is it possible to make the sliderInput change its color dependent on the chosen value?

I would like to offer the user to input a value from 0 to 10. However, there is a recommended range from, say, 4 to 8. As a consequence, the slider color should be green if the user chooses a value between 4 and 8, but it should change to orange (or red) if a value outside the recommended range is chosen.

Any help on implementing this would be greatly appreciated.


回答1:


Use renderUI, and control the conditions in color() any way you want

rm(list = ls())
library(shiny)
ui <- fluidPage(

    sliderInput("slider1", "Slider 1",min = 0, max = 10, value =c(4,8), 
                 step = 1),

    uiOutput("abc")

)
server <- function(input, output, session){

    color <- reactive({
       if(input$slider1[1] < 4 || input$slider1[2] > 8 ){
         tags$style(HTML(".js-irs-0 .irs-single, .js-irs-0 .irs-bar-
               edge, .js-irs-0 .irs-bar {background: red}"))
        }else{
          tags$style(HTML(".js-irs-0 .irs-single, .js-irs-0 .irs-bar-
                  edge, .js-irs-0 .irs-bar {background: lightgreen}"))
        }
    })

  output$abc <- renderUI({ 
       color()
  })

}
shinyApp(ui = ui, server=server)


来源:https://stackoverflow.com/questions/48546502/dynamically-colored-sliderinput

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