在 R Shiny 中使用 renderUI 时,如何在多个滑块上使用 setSliderColor 更改滑块条颜色 [英] How to change slider bar color using setSliderColor on multiple sliders when using renderUI in R Shiny

查看:54
本文介绍了在 R Shiny 中使用 renderUI 时,如何在多个滑块上使用 setSliderColor 更改滑块条颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个滑块可以响应我想要更改颜色的其他数据.我试图避免长时间的 CSS 代码,所以我想使用 ShinyWidget 的 setSliderColor() 函数是可能的.这个 answer 在我刚有一个的时候有效滑块,但现在我有两个滑块,它不起作用.这是一个可重现的示例:

I have multiple sliders that are reactive upon other data that I want to change the color of. I'm trying to avoid long bouts of CSS code, so I'd like to use shinyWidget's setSliderColor() function is possible. This answer worked when I just had one slider, but now that I have two sliders, it won't work. Here's a reproducible example:

library(shiny)
library(shinyWidgets)


ui <- fluidPage(
    
    
    sidebarLayout(
        sidebarPanel(
            textInput(inputId = "greeting",
                      label = "Say hi!"),
            actionButton(inputId = "submit", 
                         label = "Submit"),
            
            uiOutput("num_slider"),
            uiOutput("num_slider2"),
            
            
        ),
        mainPanel(DT::DTOutput("table"))
    ))

server <- function(input, output) {
    
        data <- reactive({
            req(input$submit)
            if(input$greeting == "hi!") {
            tibble(name = c("Justin", "Corey", "Sibley"),
                       grade = c(50, 100, 100))}
        })
        
        output$table <- renderDT({
            datatable(data())
        })
        
        
        output$num_slider <- renderUI({
            
            if(length(data()) > 0) {
                
                fluidPage(setSliderColor("#CA001B", sliderId = 1),
                          sliderInput(inputId = "num_filter2",
                                      label = "Filter by Number",
                                      min = 1,
                                      max = 10,
                                      value = c(1, 10)))}
            
        })
        
        output$num_slider2 <- renderUI({
            
            if(length(data()) > 0) {
                #This one won't change color
                fluidPage(setSliderColor("#CA001B", sliderId = 2),
                          sliderInput(inputId = "num_filter2",
                                      label = "Filter by Number",
                                      min = 100,
                                      max = 10000,
                                      value = c(100, 10000)))}
            
        })
    
}

# Run the application 
shinyApp(ui = ui, server = server)

我已经尝试将滑块 ID 更改为 1,甚至从 -100:100 开始,但我只能让它更改一个滑块.奇怪的是,在我的真实仪表板中,它只更改了最后一个滑块,而没有更改前面的滑块,但在这个滑块中,它只更改了第一个滑块.我想知道它是否可以与我编码的顺序有关?任何帮助将不胜感激!

I've tried changing the sliderId to both be 1, or to even go from -100:100, but I can only get it to change one slider. Strangely, in my real dashboard, it only changes the last one but doesn't change earlier sliders, but in this one, it only changes the first one. I wonder if it could do with the order that I coded it? Any help would be appreciated!

推荐答案

我通过将两个颜色开关合并为一个 setSliderColor() 来运行您的代码.像这样,虽然在不同的条件下改变不太舒服.

i got your code running by combining two color-switches into one setSliderColor(). Like this, its not that comfortable to change on different conditions, though.

library(shiny)
library(shinyWidgets)
library(DT) # added DT lib

ui <- fluidPage(
  
  
  sidebarLayout(
    sidebarPanel(
      textInput(inputId = "greeting",
                label = "Say hi!",value = "hi!"), #to not always click
      actionButton(inputId = "submit", 
                   label = "Submit"),
      
      uiOutput("num_slider1"),
      uiOutput("num_slider2"),
      
      
    ),
    mainPanel(DT::DTOutput("table"))
  ))

server <- function(input, output) {
  
  data <- reactive({
    req(input$submit |  0==0) #to not always click
    if(input$greeting == "hi!") {
      tibble(name = c("Justin", "Corey", "Sibley"),
             grade = c(50, 100, 100))}
  })
  
  output$table <- renderDT({
    datatable(data())
  })
  
  
  output$num_slider1 <- renderUI({
    
    if(length(data()) > 0) {
      
      fluidPage(setSliderColor(c("#CA001B","green"), sliderId = c(1,2)), #put vectors here to change the colors
                sliderInput(inputId = "num_slider1",
                            label = "Filter by Number",
                            min = 1,
                            max = 10,
                            value = c(1, 10)))}
    
  })
  
  output$num_slider2 <- renderUI({
    
    if(length(data()) > 0) {
      #This one won't change color
      #fluidPage(setSliderColor("yellow", sliderId = 2),
                sliderInput(inputId = "num_slider2",
                            label = "Filter by Number",
                            min = 100,
                            max = 10000,
                            value = c(100, 10000)))}
    
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

这篇关于在 R Shiny 中使用 renderUI 时,如何在多个滑块上使用 setSliderColor 更改滑块条颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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