簡體   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