使用
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';}
}"
)发布于 2017-07-18 07:12:16
这可能不是最有效的答案,但它有效:您可以使用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';
}
}
"
)
)https://stackoverflow.com/questions/39752455
复制相似问题