【问题标题】:Label does not display for sliderTextInput滑块文本输入的标签不显示
【发布时间】:2021-09-13 20:37:12
【问题描述】:

我正在使用来自 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="Choose starting and ending dates:"替换tags$b(tags$span(style="color:blue", label="Choose starting and ending dates:"))吗?
  • 您似乎遇到了嵌套问题。您将 label= 传递给 tags$span() 函数,而不是 updateSliderTextInput() 函数。

标签: r shiny shinywidgets


【解决方案1】:

原因是label 需要一个字符串,而不是shiny.tag 类对象。即使你提供了 HTML 字符串,比如&lt;b&gt;xxxxx&lt;/b&gt;,它仍然不起作用,因为这个函数的创建者不允许你输入 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)

【讨论】:

  • 我发现,在我的原始应用程序中,该解决方案可以显示标签,但不能使其变为蓝色。滑块在我的应用程序的一个闪亮模块中(虽然不在我的代表中),我怀疑这会影响 js 脚本。
  • 在一个模块中你需要改变input[id=\"Dates\"]里面的id,它将是input[id=\"ModuleID-Dates\"]
  • 我也想让字体大小为 26px,但是当我将“font-size: 26px”添加到这样的脚本中时: css({fontWeight: 900, font-size: 26px, color : 'blue'}) 蓝色变回黑色,字体大小不变。这是为什么呢?
  • 我们正在使用 JavaScript 设置 CSS 属性,破折号 - 不被接受,您需要使用 fontSize 代替
  • 再说一遍,这是 js 不是普通的 CSS,所以使用{fontWeight: 900, color: 'blue', fontSize: '26px'}。它必须是 '26px' 引号。 26px 不带引号表示一个名为 this 的对象,但您需要一个字符串而不是对象。当找不到 26px 的对象时,会触发错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多