我有多个滑块,它们对我想要更改颜色的其他数据有反应。我试图避免冗长的 CSS 代码,所以我想使用 shinyWidget 的 setSliderColor() 函数是可能的。这个答案在我只有一个滑块时有效,但现在我有两个滑块,它就不起作用了。这是一个可重现的示例:
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)
我尝试将sliderId 都更改为1,甚至从-100:100 开始,但我只能更改一个滑块。奇怪的是,在我的真实仪表板中,它只更改了最后一个,但不更改较早的滑块,但在这个中,它只更改了第一个。我想知道它是否可以与我编码的顺序有关?任何帮助,将不胜感激!