html - 如何使用 css 以特定颜色显示 Rhandsontable 中的特定列标题?

标签 html css r shiny rhandsontable

我想使用 CSS 将特定列的标题颜色更改为特定颜色。例如,有人可以建议如何将标题“mpg”、“hp”和“gear”的颜色更改为红色,并将“disp”、“wt”和“carb”的颜色更改为蓝色以下示例表?

我的问题和本论坛发的问题差不多earlier .

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  rHandsontableOutput("table1"),
  tags$style(type="text/css", "#table1 th {font-weight:bold;}")
)

server=function(input, output, session) {

  output$table1 <- renderRHandsontable({
    rhandsontable(head(mtcars),rowHeaders=F)
  })
}

shinyApp(ui,server)

最佳答案

它需要一些 HTML 知识。一种方法是将列名从纯文本转换为 HTML:

library(shiny)
library(rhandsontable)
library(purrr)
library(glue)
table_headers <- colnames(mtcars)
table_headers_html <- purrr::map_chr(table_headers, function(column){
    if(column %in% c('mpg', 'hp', 'gear')){
        color = "red"
    } else if (column %in% c('disp', 'wt', 'carb')) {
        color = "blue"
    } else {
        color = "black"
    }
    glue::glue("<span style='color:{color}'>{column}</span>")
})
> table_headers_html
 [1] "<span style='color:red'>mpg</span>"    "<span style='color:black'>cyl</span>"  "<span style='color:blue'>disp</span>" 
 [4] "<span style='color:red'>hp</span>"     "<span style='color:black'>drat</span>" "<span style='color:blue'>wt</span>"   
 [7] "<span style='color:black'>qsec</span>" "<span style='color:black'>vs</span>"   "<span style='color:black'>am</span>"  
[10] "<span style='color:red'>gear</span>"   "<span style='color:blue'>carb</span>"

在 HTML 中获得列标题后,您可以在服务器代码中执行以下操作:

server=function(input, output, session) {

    output$table1 <- renderRHandsontable({
        rhandsontable(
            head(mtcars),
            rowHeaders=F,
            colHeaders = table_headers_html
        )
    })
}

enter image description here

关于html - 如何使用 css 以特定颜色显示 Rhandsontable 中的特定列标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58250873/

相关文章:

javascript - 在函数中时不附加表数据

html - 将整个 <li> 变成链接

regex - 查找第三次出现的特殊字符并删除 R 中之前的所有内容

r - 在 R 中绘制堆积条形图

html - CSS 显示表格与普通 HTML 表格

javascript - 动态波浪路径/边框

javascript - 如何使用 jquery 在现有 li 列表之间添加带有文本的 li 元素?

javascript - 在滚动条上翻译 div

html - 为什么带有文本的 block 会移到底部?

r - spread() 基于 ID 的数据框和另一列中的值