Rhandsontable:使用 'value' 字符串的函数进行条件格式化
Rhandsontable: Conditional formatting using functions of 'value' strings
我正在尝试通过对单元格的子集字符串值进行调节来在 R 中的 handsonstable 的各个单元格中着色:
library(rhandsontable)
DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
hot_cols(renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (value == 'C') {
td.style.background = 'pink';
} else if (value == 'D') {
td.style.background = 'green';
}
}")
所以上面的工作正常,但是当我添加一个子集字符串的函数时,整个事情就崩溃了:
library(rhandsontable)
DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
hot_cols(renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (value.substring(0, 1) == 'C') {
td.style.background = 'pink';
} else if (value == 'D') {
td.style.background = 'green';
}
}")
我需要使用 substring 函数来确定单元格内容的前几个字母,以便我可以相应地为它们着色。我该怎么做?
我对Java的了解也是零。只知道R.
谢谢
亚历克斯
在使用 substring
之前,您必须使用 toString
方法将值转换为字符串。
您的代码将如下所示:
DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
hot_cols(renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (value.toString().substring(0, 1) == 'C') {
td.style.background = 'pink';
} else if (value == 'D') {
td.style.background = 'green';
}
}")
希望对您有所帮助!
我正在尝试通过对单元格的子集字符串值进行调节来在 R 中的 handsonstable 的各个单元格中着色:
library(rhandsontable)
DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
hot_cols(renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (value == 'C') {
td.style.background = 'pink';
} else if (value == 'D') {
td.style.background = 'green';
}
}")
所以上面的工作正常,但是当我添加一个子集字符串的函数时,整个事情就崩溃了:
library(rhandsontable)
DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
hot_cols(renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (value.substring(0, 1) == 'C') {
td.style.background = 'pink';
} else if (value == 'D') {
td.style.background = 'green';
}
}")
我需要使用 substring 函数来确定单元格内容的前几个字母,以便我可以相应地为它们着色。我该怎么做?
我对Java的了解也是零。只知道R.
谢谢
亚历克斯
在使用 substring
之前,您必须使用 toString
方法将值转换为字符串。
您的代码将如下所示:
DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
hot_cols(renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (value.toString().substring(0, 1) == 'C') {
td.style.background = 'pink';
} else if (value == 'D') {
td.style.background = 'green';
}
}")
希望对您有所帮助!