更改 rhandsontable 中列的字体颜色
Change font color of a column in rhandsontable
我在 R 中使用 rhandsontable 渲染了一个 table。我想将特定列的字体颜色更改为红色。我该怎么做 ?我尝试了以下代码,但它不起作用
output$hot=renderRHandsontable({
rhandontable (table)%>%
hot_col("colum1", color = "red")
})
如果您想更改 table 中元素的样式(在您的情况下,它是给定列的每个单元格的字体颜色),您将需要使用一些 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()
保存为字符串,将用作 hot_col()
函数的 renderer
参数。请注意,我使用的参数 td 指的是您 table 的单元格对象。 td 有几个属性,一个是 style,它又具有 color 属性。
还要确保您使用的是正确的 Handsontable 渲染器。在我的例子中,它是一个 TextRenderer 但你可以根据你的列的数据类型使用不同的渲染器。
有关详细信息,请参阅 Handsontable documentation。
希望对您有所帮助。
干杯
我在 R 中使用 rhandsontable 渲染了一个 table。我想将特定列的字体颜色更改为红色。我该怎么做 ?我尝试了以下代码,但它不起作用
output$hot=renderRHandsontable({
rhandontable (table)%>%
hot_col("colum1", color = "red")
})
如果您想更改 table 中元素的样式(在您的情况下,它是给定列的每个单元格的字体颜色),您将需要使用一些 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()
保存为字符串,将用作 hot_col()
函数的 renderer
参数。请注意,我使用的参数 td 指的是您 table 的单元格对象。 td 有几个属性,一个是 style,它又具有 color 属性。
还要确保您使用的是正确的 Handsontable 渲染器。在我的例子中,它是一个 TextRenderer 但你可以根据你的列的数据类型使用不同的渲染器。
有关详细信息,请参阅 Handsontable documentation。
希望对您有所帮助。 干杯