Reputation: 55
I'm trying to use the rhandsontable
library to generate a table in which I combine two of the package's nifty features: (1) row highlighting using a customer renderer
argument, and (2) checkbox column type for a boolean field.
On their own, both features work fine. The below, for example, shows how row highlighting works:
library(rhandsontable)
df = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
row_highlight = c(5, 7)
rhandsontable(df,
row_highlight = row_highlight) %>%
hot_cols(renderer = "
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
tbl = this.HTMLWidgets.widgets[0]
hrows = tbl.params.row_highlight
hrows = hrows instanceof Array ? hrows : [hrows]
if (hrows.includes(row)) {
td.style.background = 'pink';
}
return td;
}")
And the below shows how the checkbox functionality works:
library(rhandsontable)
df = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
rhandsontable(df, row_highlight = row_highlight) %>%
hot_col(col = 'bool', type = 'checkbox')
My question: how do you combine these two features (ie, generate a table with highlighted rows and functioning checkboxes)?
One would think that the below would work, but the checkboxes don't render (instead showing up as true
and false
):
library(rhandsontable)
df = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
row_highlight = c(5, 7)
rhandsontable(df,
row_highlight = row_highlight) %>%
hot_cols(renderer = "
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
tbl = this.HTMLWidgets.widgets[0]
hrows = tbl.params.row_highlight
hrows = hrows instanceof Array ? hrows : [hrows]
if (hrows.includes(row)) {
td.style.background = 'pink';
}
return td;
}") %>%
hot_col(col = 'bool', type = 'checkbox')
Is there a way to combine both checkboxes and row highlighting in a rhandsontable in R?
Upvotes: 2
Views: 2092
Reputation: 571
Different renderers should be applied to columns with text/numeric and logical variables in order to combine checkbox functionality and rows highlighting.
library(rhandsontable)
df = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
row_highlight = c(5, 7)
rhandsontable(df,
row_highlight = row_highlight) %>%
hot_col(col = names(df)[!names(df) %in% "bool"],
renderer = "
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
tbl = this.HTMLWidgets.widgets[0]
hrows = tbl.params.row_highlight
hrows = hrows instanceof Array ? hrows : [hrows]
if (hrows.includes(row)) {
td.style.background = 'pink';
}
return td;
}") %>%
hot_col(col = "bool",
renderer = "
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.CheckboxRenderer.apply(this, arguments);
tbl = this.HTMLWidgets.widgets[0]
hrows = tbl.params.row_highlight
hrows = hrows instanceof Array ? hrows : [hrows]
if (hrows.includes(row)) {
td.style.background = 'pink';
}
return td;
}")
Upvotes: 3