【问题标题】:Font color for single text element in Shiny bsPopoverShiny bsPopover中单个文本元素的字体颜色
【发布时间】:2020-09-13 10:59:49
【问题描述】:

我想更改 bsPopover 内容参数中一部分文本的字体颜色。

此语法在服务器端有效,但不适用于 bsPopover 函数的内容参数:


library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyBS)

ui <- dashboardPagePlus(title = 'My Title',
                        ###### Header ####
                        header=dashboardHeaderPlus(
                          title = 'Title'),
      sidebar=dashboardSidebar(
                            disable = TRUE),
                          ###### Body ####
      body = dashboardBody(
                   fluidRow(
                     bsPopover(id = 'attend',
                               title = '', 
                               content = HTML(paste0('<span style=\"color:', '#22a783', '\">', 
                                                      'Green', '</span>', 
                                                      '<br>', 'Red', '<br>', 'Blue', '<br>','Black')), 
                               placement = "bottom", 
                               trigger = "hover",
                               options = NULL),
                     actionButton(inputId = "attend", 
                                  label = "", 
                                  icon = icon('info')))))
#################### SERVER   #################### 
server = function(input, output, session) { 
}
# Run the application
shinyApp(ui = ui, server = server)

我想让文本“绿色”显示为绿色。文本,“红色”,显示为红色等。

我可以更改 css 中的所有文本颜色,但似乎无法微调 css 之外的单个文本元素

感谢您的任何想法。

【问题讨论】:

  • 出于安全原因,您的下拉列表中的 HTML 正在被清理(即除白名单之外的所有标签都将被删除)。如果您确信没有人可以将恶意 HTML 注入其中,则有一个选项,例如 sanitize=FALSE,可以将其关闭。
  • 谢谢。根据列入白名单的元素,您的反馈是有意义的。 getbootstrap.com/docs/4.3/getting-started/javascript。现在我需要找出正确的实现语法。 options = list(sanitize=FALSE) 不起作用
  • 是的,从记忆中,R 包将选项转换为 js 的方式存在一个奇怪的错误,您必须使用非常特定的语法。不过我再也找不到了。

标签: html r shiny shinybs


【解决方案1】:

作为替代方案,您可以使用来自 shinyWidgets 的dropMenu,并直接在其中使用 HTML 标签:

library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(
  title = 'My Title',
  ###### Header ####
  header=dashboardHeader(
    title = 'Title'
  ),
  sidebar=dashboardSidebar(
    disable = TRUE
  ),
  ###### Body ####
  body = dashboardBody(
    fluidRow(
      dropMenu(
        actionButton(
          inputId = "attend", 
          label = "", 
          icon = icon('info')
        ),
        tags$div(
          tags$span(style = "color: #22a783;", "green"),
          tags$span(style = "color: red;", "Red"),
          tags$span(style = "color: green;", "Green"),
          "Black"
        ),
        placement = "bottom",
        trigger = "mouseenter"
      )
    )
  )
)
#################### SERVER   #################### 
server = function(input, output, session) { 
}
# Run the application
shinyApp(ui = ui, server = server)

【讨论】:

  • 我很欣赏这个想法,但我不知道如何添加换行符和附加文本。我尝试过 tags$div( tags$span(style = "color: #22a783;", "green"),'\n', tags$span(style = "color: red;", "Red"), tags $span(style = "color: green;", "Green"), "Black")
  • 需要使用HTML标签(shiny.rstudio.com/articles/tag-glossary.html),换行使用tags$br(),也可以将span(内联)替换为div(块)
猜你喜欢
  • 2014-07-25
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多