更改rhandsontable中列的字体颜色

时间:2017-07-21 12:53:54

标签: r rstudio rhandsontable

我有一个在R中使用rhandsontable呈现的表。我想将字体颜色更改为特定列的红色。我该怎么做 ?我尝试了以下代码,但它不起作用

output$hot=renderRHandsontable({
rhandontable (table)%>%
hot_col("colum1", color = "red") 

})

1 个答案:

答案 0 :(得分:1)

如果要更改表格内部元素的样式(在您的情况下,它是给定列的每个单元格的字体颜色),您将需要使用一些Javascript并编写一个渲染器函数,做这个工作,像这样:

# Toy data frame
table <- data.frame(a = 1:10, b = letters[1:10])

# Custom renderer function
color_renderer <- "
  function(instance, td) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);
    td.style.color = 'red';
  }
"

rhandsontable(table) %>%
  hot_col("b", renderer = color_renderer)

函数color_renderer()保存为字符串,将用作renderer - 函数的hot_col()参数。注意参数 td 我正在使用反转到表格的单元格对象。 td 有几个属性,一个是 style ,后者又作为属性 color 。 另请注意,您使用的是正确的Handsontable渲染器。在我的情况下,它是 TextRenderer ,但您可以根据列的数据类型使用不同的渲染器。

有关详细信息,请参阅Handsontable documentation

我希望这会有所帮助。 干杯