更改 rhandsontable 中多行的背景颜色
Changing background color of several rows in rhandsontable
正在使用
library("rhandsontable")
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_col(c(1,3),
renderer = "function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
td.style.background = 'lightblue';
}"
)
可以为选定的列定义背景颜色,这里是第 1 列和第 3 列。
是否可以对选定的行执行相同的操作?
如果我直接引用这些行,这会起作用:
library("rhandsontable")
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_cols(
renderer = "function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
if (row==1 || row==3 || row==4) td.style.background = 'lightblue';
}"
)
但是,我在一个向量中有行索引,我想提供这些行索引(这不起作用,大概是因为渲染器函数看不到 myindex
):
myindex <- c(1, 3, 4)
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_cols(
renderer = "function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
if (row in myindex) td.style.background = 'lightblue';}
}"
)
这可能不是最有效的答案,但它有效:
您可以使用 paste()
/paste0()
将您的 r 对象添加到 javascript 代码中,如下所示:
myindex <- c(1, 3, 4)
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_cols(
renderer = paste0(
"
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
var row_index = ", paste("[", paste(myindex, collapse = ","), "]"), ";
for (i = 0; i < row_index.length; i++)
if (row == row_index[i]) {
td.style.background = 'lightblue';
}
}
"
)
)
正在使用
library("rhandsontable")
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_col(c(1,3),
renderer = "function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
td.style.background = 'lightblue';
}"
)
可以为选定的列定义背景颜色,这里是第 1 列和第 3 列。
是否可以对选定的行执行相同的操作?
如果我直接引用这些行,这会起作用:
library("rhandsontable")
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_cols(
renderer = "function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
if (row==1 || row==3 || row==4) td.style.background = 'lightblue';
}"
)
但是,我在一个向量中有行索引,我想提供这些行索引(这不起作用,大概是因为渲染器函数看不到 myindex
):
myindex <- c(1, 3, 4)
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_cols(
renderer = "function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
if (row in myindex) td.style.background = 'lightblue';}
}"
)
这可能不是最有效的答案,但它有效:
您可以使用 paste()
/paste0()
将您的 r 对象添加到 javascript 代码中,如下所示:
myindex <- c(1, 3, 4)
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_cols(
renderer = paste0(
"
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.TextCell.renderer.apply(this, arguments);
var row_index = ", paste("[", paste(myindex, collapse = ","), "]"), ";
for (i = 0; i < row_index.length; i++)
if (row == row_index[i]) {
td.style.background = 'lightblue';
}
}
"
)
)