标签不显示为 sliderTextInput

Label does not display for sliderTextInput

我正在使用 shinyWidgets 包中的 sliderTextInput 小部件。滑块工作正常,但未显示我的滑块标签。显示的不是我的标签,而是“[object Object]”。我该如何纠正这个问题?这是我的代表。

# reprex for slider problem
library(shiny)
library(shinyWidgets)

dateD <- seq.Date(as.Date("2017-01-01"),
    Sys.Date()-1,by="day")
dateC <- character()
for (i in 1:length(dateD)) {
    dateC[i] <- format(dateD[i],"%b %d, %Y")
}
strtRang <- c(dateC[1],dateC[length(dateC)])

ui <- fluidPage(
    sliderTextInput("Dates",
        label="Choose starting and ending dates:",
        choices=dateC,
        selected=strtRang,
        dragRange = TRUE,
        width="100%")
)
server <- function(input, output,session) {
    observe({
        updateSliderTextInput(session,inputId="Dates",
            tags$b(tags$span(style="color:blue",
            label="Choose starting and ending dates:")),
            choices = dateC,
            selected=strtRang)
    })
}
shinyApp(ui, server)

原因是 label 需要一个字符串而不是 shiny.tag class 对象。即使你提供了 HTML 字符串,比如 <b>xxxxx</b>,它仍然不起作用,因为这个函数的创建者不允许你输入 HTML,只有字符串。你提供什么字符串就是你看到的,没有HTML解析。

如果要动态添加颜色和字体粗细,请执行以下操作:

# reprex for slider problem
library(shiny)
library(shinyWidgets)
library(shinyjs)
dateD <- seq.Date(as.Date("2017-01-01"),
                  Sys.Date()-1,by="day")
dateC <- character()
for (i in 1:length(dateD)) {
  dateC[i] <- format(dateD[i],"%b %d, %Y")
}
strtRang <- c(dateC[1],dateC[length(dateC)])

ui <- fluidPage(
  shinyjs::useShinyjs(),
  sliderTextInput("Dates",
                  label='Choose starting and ending dates:',
                  choices=dateC,
                  selected=strtRang,
                  dragRange = TRUE,
                  width="100%")
)
server <- function(input, output, session) {
  observe({
    updateSliderTextInput(session,inputId="Dates",
                          label = "Choose starting and ending dates aaaa:",
                          choices = dateC,
                          selected=strtRang)
    shinyjs::runjs("$('.shiny-input-container:has(input[id=\"Dates\"]) > label').css({fontWeight: 900, color: 'blue'})")
  })
  
}
shinyApp(ui, server)

如果样式不需要动态设置,更简单:

ui <- fluidPage(
  sliderTextInput("Dates",
                  label='Choose starting and ending dates:',
                  choices=dateC,
                  selected=strtRang,
                  dragRange = TRUE,
                  width="100%"),
  tags$script(HTML("$('.shiny-input-container:has(input[id=\"Dates\"]) > label').css({fontWeight: 900, color: 'blue'})"))
)
server <- function(input, output, session) {
}
shinyApp(ui, server)