繁体   English   中英

使用 updateSliderTextInput 更改 slider 的颜色

[英]Change color of slider using updateSliderTextInput

我试图在更新其值时更改幻灯片的颜色。 我尝试了不同的方法但没有成功。 以下代码不会运行,但会复制我正在尝试执行的操作:

if (interactive()) {
  library("shiny")
  library("shinyWidgets")
  
  ui <- fluidPage(
    br(),
    sliderTextInput(
      inputId = "mySlider",
      label = "Pick a month :",
      choices = month.abb,
      selected = "Jan"
    ),
    verbatimTextOutput(outputId = "res"),
    radioButtons(
      inputId = "up",
      label = "Update choices:",
      choices = c("Abbreviations", "Full names")
    )
  )
  
  server <- function(input, output, session) {
    output$res <- renderPrint(str(input$mySlider))
    
    observeEvent(input$up, {
      choices <- switch(
        input$up,
        "Abbreviations" = month.abb,
        "Full names" = month.name
      )
      updateSliderTextInput(
        session = session,
        inputId = "mySlider",
        choices = choices,
        color = "red" # This is the line I need to add
      )
    }, ignoreInit = TRUE)
  }
  
  shinyApp(ui = ui, server = server)
}

也许有人回答这个问题?

我能够对此进行更多思考,并想出一种方法来根据输入更新 slider 颜色。 shinyWidgets::setSliderColor本质上只是注入 CSS 来覆盖所有与 sliderInputs 关联的类。 所以它需要包含在 UI 而不是服务器中。 (花了一分钟才意识到)。

我设置了一个空白的uiOutput ,然后通过使用新的或默认颜色观察input$up来更新它。

演示

在此处输入图像描述

ui <- fluidPage(
  br(),
  mainPanel(class = "temp",
    uiOutput('s_color'), # uiOuput
    sliderTextInput(
      inputId = "mySlider",
      label = "Pick a month :",
      choices = month.abb,
      selected = "Jan"
    ),
    verbatimTextOutput(outputId = "res"),
    radioButtons(
      inputId = "up",
      label = "Update choices:",
      choices = c("Abbreviations", "Full names")
    )
  )
)

server <- function(input, output, session) {
  output$res <- renderPrint(str(input$mySlider))
  
  # output$s_color = renderUI({})
  observeEvent(input$up, {
    choices <- switch(
      input$up,
      "Abbreviations" = month.abb,
      "Full names" = month.name
    )
    updateSliderTextInput(
      session = session,
      inputId = "mySlider",
      choices = choices
    )
    output$s_color = renderUI({ # add color 
      if (input$up == "Full names") {
        setSliderColor(c("Red"), c(1))
      } else {
        setSliderColor(c("#428bca"), c(1))
      }
    })
  }, ignoreInit = TRUE)
}

shinyApp(ui = ui, server = server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM