Created
March 12, 2019 05:03
-
-
Save shaun-jacks/8aec8982a91eb5e7b263681563cfaac5 to your computer and use it in GitHub Desktop.
Function to color cells in rhandsontable along with code to use it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This generates a string that will be used within the render fxn | |
# in handsontable(). The string needs two parameters for this fxn to work: | |
# 1. A list of columns: so a list named col_highlight, with keys: | |
# colhighlight1, colhighlight2 ... up to vars_len. Each key has a corresponding | |
# value which is the column number within the handsontable to color | |
# 1. A list of lists of rows: so a list named row_highlight, with keys: | |
# rowhighlight1, row_highlight2 ... up to vars_len. Each key has a corresponding | |
# value which is a vector of row numbers within the handsontable to color | |
# The highlighted cells are the cells represented by colhighight1, rowhighlight1,... | |
# colhighlight2 (length 1), rowhighlight2 (length x), up to vars_len. | |
# vars_len is a range of 1:length(col_highlight) | |
generate_failed_cell_renderer <- function(vars_len, | |
highlight_color = '#FFA2A2') { | |
col_row_init_str <- paste(sprintf( | |
"hcols%s = instance.params.col_highlight.col_highlight%s | |
hcols%s = hcols%s instanceof Array ? hcols%s : [hcols%s] | |
hrows%s = instance.params.row_highlight.row_highlight%s | |
hrows%s = hrows%s instanceof Array ? hrows%s : [hrows%s]", vars_len, | |
vars_len, vars_len, vars_len, vars_len, vars_len, | |
vars_len, vars_len, vars_len, vars_len, | |
vars_len, vars_len), collapse = "\n") # dont forget to sep by newline | |
# if statements generated to highlight each failed cell | |
if_statement_str <- paste(sprintf( | |
"if ((instance.params && hcols%s.includes(col)) & (instance.params && hrows%s.includes(row))) | |
td.style.background = '%s';", vars_len, vars_len, | |
highlight_color), collapse = "\n") | |
# overall function used to render highlights | |
render_cell_detect <- paste0( " | |
function(instance, td, row, col, prop, value, cellProperties) { | |
Handsontable.renderers.NumericRenderer.apply(this, arguments); | |
if (instance.params) { | |
", col_row_init_str, " | |
} | |
", if_statement_str, " | |
}") | |
return(render_cell_detect) | |
} | |
## To use it ## | |
col_highlight = list(colhighlight1 = 1, | |
colhighlight2 = 2) | |
row_highlight = list(rowhighlight1 = c(0,1,2), | |
rowhighlight2 = c(2,3)) | |
hot_table <- as.data.frame( | |
list( | |
row1 = c(1,2,3,4,5), | |
row2 = c(5,4,3,2,1), | |
row3 = c(7,8,9,10,11), | |
row4 = c(0,1,2,3,4) | |
) | |
) | |
vars_len <- 1:length(col_highlight) | |
render_cell_detect <- generate_failed_cell_renderer(vars_len, | |
highlight_color = '#FFA2A2') | |
rhandsontable::rhandsontable( | |
row_highlight = row_highlight, | |
col_highlight = col_highlight, | |
as.data.frame(hot_table), | |
height = 750) %>% | |
# highlight failed cells | |
rhandsontable::hot_cols(renderer = render_cell_detect) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment