动态着色的滑块输入 [英] Dynamically colored sliderInput

查看:43
本文介绍了动态着色的滑块输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与帖子相关的问题 如何为滑块 (sliderInput) 着色?.

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?

我想让用户输入一个 0 到 10 的值.但是,有一个推荐的范围,比如 4 到 8.因此,如果用户选择一个值,滑块颜色应该是绿色的介于 4 和 8 之间,但如果选择了超出推荐范围的值,则应更改为橙色(或红色).

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.

推荐答案

使用 renderUI,并随心所欲地控制 color() 中的条件

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)

这篇关于动态着色的滑块输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆