| 1 |
#' Handsontable widget |
| 2 |
#' |
| 3 |
#' Create a \href{https://handsontable.com/}{Handsontable.js} widget. |
| 4 |
#' |
| 5 |
#' For full documentation on the package, visit \url{https://jrowen.github.io/rhandsontable/} |
| 6 |
#' @param data a \code{data.table}, \code{data.frame} or \code{matrix} |
| 7 |
#' @param colHeaders a vector of column names. If missing \code{colnames} |
| 8 |
#' will be used. Setting to \code{NULL} will omit. |
| 9 |
#' @param rowHeaders a vector of row names. If missing \code{rownames} |
| 10 |
#' will be used. Setting to \code{NULL} will omit. |
| 11 |
#' @param comments matrix or data.frame of comments; NA values are ignored |
| 12 |
#' @param useTypes logical specifying whether column classes should be mapped to |
| 13 |
#' equivalent Javascript types. Note that |
| 14 |
#' Handsontable does not support column add/remove when column types |
| 15 |
#' are defined (i.e. useTypes == TRUE in rhandsontable). |
| 16 |
#' @param readOnly logical specifying whether the table is editable |
| 17 |
#' @param selectCallback logical enabling the afterSelect event to return data. |
| 18 |
#' This can be used with shiny to tie updates to a selected table cell. |
| 19 |
#' @param width numeric table width |
| 20 |
#' @param height numeric table height |
| 21 |
#' @param digits numeric passed to \code{jsonlite::toJSON} |
| 22 |
#' @param debug numeric Javascript log level |
| 23 |
#' @param search logical specifying if the data can be searched (see |
| 24 |
#' \url{https://jrowen.github.io/rhandsontable/#Customizing} |
| 25 |
#' and Shiny example in inst/examples/rhandsontable_search) |
| 26 |
#' @param ... passed to \code{hot_table} and to the \code{params} property of the widget |
| 27 |
#' @examples |
| 28 |
#' library(rhandsontable) |
| 29 |
#' DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 30 |
#' small = letters[1:10], |
| 31 |
#' dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 32 |
#' stringsAsFactors = FALSE) |
| 33 |
#' |
| 34 |
#' rhandsontable(DF, rowHeaders = NULL) |
| 35 |
#' @seealso \code{\link{hot_table}}, \code{\link{hot_cols}}, \code{\link{hot_rows}}, \code{\link{hot_cell}} |
| 36 |
#' @export |
| 37 |
rhandsontable <- function(data, colHeaders, rowHeaders, comments = NULL, |
| 38 |
useTypes = TRUE, readOnly = NULL, |
| 39 |
selectCallback = FALSE, |
| 40 |
width = NULL, height = NULL, digits = 4, |
| 41 |
debug = NULL, search = FALSE, ...) { |
| 42 |
rColHeaders = colnames(data) |
| 43 |
if (.row_names_info(data) > 0L) |
| 44 |
rRowHeaders = rownames(data) |
| 45 |
else |
| 46 |
rRowHeaders = NULL |
| 47 |
|
| 48 |
if (missing(colHeaders)) |
| 49 |
colHeaders = colnames(data) |
| 50 |
if (missing(rowHeaders)) |
| 51 |
rowHeaders = rownames(data) |
| 52 |
|
| 53 |
rClass = class(data) |
| 54 |
if ("matrix" %in% rClass) { |
| 55 |
rColClasses = class(data[1, 1]) |
| 56 |
} else { |
| 57 |
rColClasses = lapply(data, class) |
| 58 |
rColClasses[grepl("factor", rColClasses)] = "factor" |
| 59 |
} |
| 60 |
|
| 61 |
if ("tbl_df" %in% rClass) { |
| 62 |
# temp fix for tibbles |
| 63 |
data = as.data.frame(data) |
| 64 |
} else if ("data.table" %in% rClass) { |
| 65 |
# temp fix for data.table with S3 class |
| 66 |
data = as.data.frame(data) |
| 67 |
} |
| 68 |
|
| 69 |
if (!useTypes) { |
| 70 |
data = do.call(cbind, lapply(data, function(x) { |
| 71 |
if (class(x) == "Date") |
| 72 |
as.character(x, format = "%m/%d/%Y") |
| 73 |
else |
| 74 |
as.character(x) |
| 75 |
})) |
| 76 |
data = as.matrix(data, rownames.force = TRUE) |
| 77 |
cols = NULL |
| 78 |
} else { |
| 79 |
# get column data types |
| 80 |
col_typs = get_col_types(data) |
| 81 |
|
| 82 |
# format date for display |
| 83 |
dt_inds = which(col_typs == "date") |
| 84 |
if (length(dt_inds) > 0L) { |
| 85 |
for (i in dt_inds) |
| 86 |
data[, i] = as.character(data[, i], format = "%m/%d/%Y") |
| 87 |
} |
| 88 |
|
| 89 |
cols = lapply(seq_along(col_typs), function(i) { |
| 90 |
type = col_typs[i] |
| 91 |
if (type == "factor") { |
| 92 |
res = list(type = "dropdown", |
| 93 |
source = levels(data[, i]), |
| 94 |
allowInvalid = FALSE |
| 95 |
) |
| 96 |
} else if (type == "numeric") { |
| 97 |
res = list(type = "numeric", |
| 98 |
numericFormat = list(pattern = "0.00")) |
| 99 |
} else if (type == "integer") { |
| 100 |
res = list(type = "numeric", |
| 101 |
numericFormat = list(pattern = "0")) |
| 102 |
} else if (type == "date") { |
| 103 |
res = list(type = "date", |
| 104 |
correctFormat = TRUE, |
| 105 |
dateFormat = "MM/DD/YYYY") |
| 106 |
} else { |
| 107 |
res = list(type = type) |
| 108 |
} |
| 109 |
res$readOnly = readOnly |
| 110 |
res$renderer = JS("customRenderer") |
| 111 |
res$default = NA |
| 112 |
res |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
x = list( |
| 117 |
data = jsonlite::toJSON(data, na = "null", rownames = FALSE, |
| 118 |
digits = digits), |
| 119 |
rClass = rClass, |
| 120 |
rColClasses = rColClasses, |
| 121 |
rColnames = as.list(colnames(data)), |
| 122 |
rColHeaders = rColHeaders, |
| 123 |
rRowHeaders = rRowHeaders, |
| 124 |
rDataDim = dim(data), |
| 125 |
selectCallback = selectCallback, |
| 126 |
colHeaders = colHeaders, |
| 127 |
rowHeaders = rowHeaders, |
| 128 |
columns = cols, |
| 129 |
width = width, |
| 130 |
height = height, |
| 131 |
debug = ifelse(is.null(debug) || is.na(debug) || !is.numeric(debug), 0, debug), |
| 132 |
search = search |
| 133 |
) |
| 134 |
|
| 135 |
# create widget |
| 136 |
hot = htmlwidgets::createWidget( |
| 137 |
name = 'rhandsontable', |
| 138 |
x, |
| 139 |
width = width, |
| 140 |
height = height, |
| 141 |
package = 'rhandsontable', |
| 142 |
sizingPolicy = htmlwidgets::sizingPolicy( |
| 143 |
padding = 5, |
| 144 |
defaultHeight = "100%", |
| 145 |
defaultWidth = "100%" |
| 146 |
) |
| 147 |
) |
| 148 |
|
| 149 |
if (!is.null(readOnly) && !is.logical(hot$x$colHeaders)) { |
| 150 |
for (x in hot$x$colHeaders) |
| 151 |
hot = hot %>% hot_col(x, readOnly = readOnly) |
| 152 |
} |
| 153 |
|
| 154 |
hot = hot %>% hot_table(enableComments = !is.null(comments), ...) |
| 155 |
|
| 156 |
if (!is.null(comments)) { |
| 157 |
inds = as.data.frame(which(!is.na(comments), arr.ind = TRUE)) |
| 158 |
for (i in 1:nrow(inds)) |
| 159 |
hot = hot %>% |
| 160 |
hot_cell(inds$row[i], inds$col[i], |
| 161 |
comment = comments[inds$row[i], inds$col[i]]) |
| 162 |
#hot$x$rComments = jsonlite::toJSON(comments) |
| 163 |
} |
| 164 |
|
| 165 |
hot |
| 166 |
} |
| 167 |
|
| 168 |
#' Handsontable widget |
| 169 |
#' |
| 170 |
#' Configure table. See |
| 171 |
#' \href{https://handsontable.com/}{Handsontable.js} for details. |
| 172 |
#' |
| 173 |
#' @param hot rhandsontable object |
| 174 |
#' @param contextMenu logical enabling the right-click menu |
| 175 |
#' @param stretchH character describing column stretching. Options are 'all', 'right', |
| 176 |
#' and 'none' |
| 177 |
#' @param customBorders json object |
| 178 |
#' @param highlightRow logical enabling row highlighting for the selected |
| 179 |
#' cell |
| 180 |
#' @param highlightCol logical enabling column highlighting for the |
| 181 |
#' selected cell |
| 182 |
#' @param enableComments logical enabling comments in the table |
| 183 |
#' @param overflow character setting the css overflow behavior. Options are |
| 184 |
#' auto (default), hidden and visible |
| 185 |
#' @param rowHeaderWidth numeric width (in px) for the rowHeader column |
| 186 |
#' @param ... passed to \href{https://handsontable.com/}{Handsontable.js} constructor |
| 187 |
#' @examples |
| 188 |
#' library(rhandsontable) |
| 189 |
#' DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 190 |
#' small = letters[1:10], |
| 191 |
#' dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 192 |
#' stringsAsFactors = FALSE) |
| 193 |
#' |
| 194 |
#' rhandsontable(DF) %>% |
| 195 |
#' hot_table(highlightCol = TRUE, highlightRow = TRUE) |
| 196 |
#' @seealso \code{\link{rhandsontable}} |
| 197 |
#' @export |
| 198 |
hot_table = function(hot, contextMenu = TRUE, stretchH = "none", |
| 199 |
customBorders = NULL, highlightRow = NULL, |
| 200 |
highlightCol = NULL, enableComments = FALSE, |
| 201 |
overflow = NULL, rowHeaderWidth = NULL, ...) { |
| 202 |
if (!is.null(stretchH)) hot$x$stretchH = stretchH |
| 203 |
if (!is.null(customBorders)) hot$x$customBorders = customBorders |
| 204 |
if (!is.null(enableComments)) hot$x$comments = enableComments |
| 205 |
if (!is.null(overflow)) hot$x$overflow = overflow |
| 206 |
if (!is.null(rowHeaderWidth)) hot$x$rowHeaderWidth = rowHeaderWidth |
| 207 |
|
| 208 |
if ((!is.null(highlightRow) && highlightRow) || |
| 209 |
(!is.null(highlightCol) && highlightCol)) |
| 210 |
hot$x$ishighlight = TRUE |
| 211 |
if (!is.null(highlightRow) && highlightRow) |
| 212 |
hot$x$currentRowClassName = "currentRow" |
| 213 |
if (!is.null(highlightCol) && highlightCol) |
| 214 |
hot$x$currentColClassName = "currentCol" |
| 215 |
|
| 216 |
if (!is.null(contextMenu) && contextMenu) |
| 217 |
hot = hot %>% |
| 218 |
hot_context_menu(allowComments = enableComments, |
| 219 |
allowCustomBorders = !is.null(customBorders), |
| 220 |
allowColEdit = is.null(hot$x$columns), ...) |
| 221 |
else |
| 222 |
hot$x$contextMenu = FALSE |
| 223 |
|
| 224 |
if (!is.null(list(...))) |
| 225 |
hot$x = c(hot$x, list(...)) |
| 226 |
|
| 227 |
hot |
| 228 |
} |
| 229 |
|
| 230 |
#' Handsontable widget |
| 231 |
#' |
| 232 |
#' Configure the options for the right-click context menu |
| 233 |
#' |
| 234 |
#' @param hot rhandsontable object |
| 235 |
#' @param allowRowEdit logical enabling row editing |
| 236 |
#' @param allowColEdit logical enabling column editing. Note that |
| 237 |
#' Handsontable does not support column add/remove when column types |
| 238 |
#' are defined (i.e. useTypes == TRUE in rhandsontable). |
| 239 |
#' @param allowReadOnly logical enabling read-only toggle |
| 240 |
#' @param allowComments logical enabling comments |
| 241 |
#' @param allowCustomBorders logical enabling custom borders |
| 242 |
#' @param customOpts list |
| 243 |
#' @param ... ignored |
| 244 |
#' @examples |
| 245 |
#' library(rhandsontable) |
| 246 |
#' DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 247 |
#' small = letters[1:10], |
| 248 |
#' dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 249 |
#' stringsAsFactors = FALSE) |
| 250 |
#' |
| 251 |
#' rhandsontable(DF) %>% |
| 252 |
#' hot_context_menu(allowRowEdit = FALSE, allowColEdit = FALSE) |
| 253 |
#' @export |
| 254 |
hot_context_menu = function(hot, allowRowEdit = TRUE, allowColEdit = TRUE, |
| 255 |
allowReadOnly = FALSE, allowComments = FALSE, |
| 256 |
allowCustomBorders = FALSE, |
| 257 |
customOpts = NULL, ...) { |
| 258 |
if (!is.null(hot$x$contextMenu) && is.logical(hot$x$contextMenu) && |
| 259 |
!hot$x$contextMenu) |
| 260 |
warning("The context menu was disabled but will be re-enabled (hot_context_menu)") |
| 261 |
|
| 262 |
if (!is.null(hot$x$columns) && allowColEdit) |
| 263 |
warning("Handsontable.js does not support column add/delete when column types ", |
| 264 |
"are defined. Set useTypes = FALSE in rhandsontable to enable column ", |
| 265 |
"edits.") |
| 266 |
|
| 267 |
if (is.null(hot$x$contextMenu$items)) |
| 268 |
opts = list() |
| 269 |
else |
| 270 |
opts = hot$x$contextMenu$items |
| 271 |
|
| 272 |
add_opts = function(new, old, val = list()) { |
| 273 |
new_ = lapply(new, function(x) { |
| 274 |
if (grepl("^hsep", x) && !is.null(val)) |
| 275 |
return(list(name = "---------")) |
| 276 |
else |
| 277 |
return(val) |
| 278 |
}) |
| 279 |
names(new_) = new |
| 280 |
if (length(old) > 0) { |
| 281 |
modifyList(old, new_) |
| 282 |
} else { |
| 283 |
new_ |
| 284 |
} |
| 285 |
} |
| 286 |
remove_opts = function(new) { |
| 287 |
add_opts(new, opts, val = NULL) |
| 288 |
} |
| 289 |
|
| 290 |
if (!is.null(allowRowEdit) && allowRowEdit) |
| 291 |
opts = add_opts(c("hsep1", "row_above", "row_below", "remove_row"), opts) |
| 292 |
else |
| 293 |
opts = remove_opts(c("hsep1", "row_above", "row_below", "remove_row")) |
| 294 |
|
| 295 |
if (!is.null(allowColEdit) && allowColEdit) |
| 296 |
opts = add_opts(c("hsep2", "col_left", "col_right", "remove_col"), opts) |
| 297 |
else |
| 298 |
opts = remove_opts(c("hsep2", "col_left", "col_right", "remove_col")) |
| 299 |
|
| 300 |
opts = add_opts(c("hsep3", "undo", "redo"), opts) |
| 301 |
|
| 302 |
opts = add_opts(c("hsep4", "alignment"), opts) |
| 303 |
|
| 304 |
if (!is.null(allowReadOnly) && allowReadOnly) |
| 305 |
opts = add_opts(c("hsep5", "make_read_only"), opts) |
| 306 |
else |
| 307 |
opts = remove_opts(c("hsep5", "make_read_only")) |
| 308 |
|
| 309 |
if (!is.null(allowComments) && allowComments) |
| 310 |
opts = add_opts(c("hsep6", "commentsAddEdit", "commentsRemove"), opts) |
| 311 |
else |
| 312 |
opts = remove_opts(c("hsep6", "commentsAddEdit", "commentsRemove")) |
| 313 |
|
| 314 |
if (!is.null(allowCustomBorders) && allowCustomBorders) |
| 315 |
opts = add_opts(c("hsep7", "borders"), opts) |
| 316 |
else |
| 317 |
opts = remove_opts(c("hsep7", "borders")) |
| 318 |
|
| 319 |
sep_ct = 20 |
| 320 |
if (!is.null(customOpts)) { |
| 321 |
opts[[paste0("hsep", sep_ct)]] = list(name = "---------") |
| 322 |
sep_ct = sep_ct + 1 |
| 323 |
opts = modifyList(opts, customOpts) |
| 324 |
} |
| 325 |
|
| 326 |
if (grepl("^hsep", names(opts)[1])) |
| 327 |
opts = opts[-1] |
| 328 |
if (grepl("^hsep", names(opts)[length(opts)])) |
| 329 |
opts = opts[-length(opts)] |
| 330 |
|
| 331 |
hot$x$contextMenu = list(items = opts) |
| 332 |
|
| 333 |
hot |
| 334 |
} |
| 335 |
|
| 336 |
#' Handsontable widget |
| 337 |
#' |
| 338 |
#' Configure multiple columns. |
| 339 |
#' |
| 340 |
#' @param hot rhandsontable object |
| 341 |
#' @param colWidths a scalar or numeric vector of column widths |
| 342 |
#' @param columnSorting logical enabling row sorting. Sorting only alters the |
| 343 |
#' table presentation and the original dataset row order is maintained. |
| 344 |
#' The sorting will be done when a user click on column name |
| 345 |
#' @param manualColumnMove logical enabling column drag-and-drop reordering |
| 346 |
#' @param manualColumnResize logical enabline column width resizing |
| 347 |
#' @param fixedColumnsLeft a scalar indicating the number of columns to |
| 348 |
#' freeze on the left |
| 349 |
#' @param ... passed to hot_col |
| 350 |
#' @examples |
| 351 |
#' library(rhandsontable) |
| 352 |
#' DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 353 |
#' small = letters[1:10], |
| 354 |
#' dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 355 |
#' stringsAsFactors = FALSE) |
| 356 |
#' |
| 357 |
#' rhandsontable(DF) %>% |
| 358 |
#' hot_cols(columnSorting = TRUE) |
| 359 |
#' @seealso \code{\link{hot_col}}, \code{\link{hot_rows}}, \code{\link{hot_cell}} |
| 360 |
#' @export |
| 361 |
hot_cols = function(hot, colWidths = NULL, columnSorting = NULL, |
| 362 |
manualColumnMove = NULL, manualColumnResize = NULL, |
| 363 |
fixedColumnsLeft = NULL, ...) { |
| 364 |
if (!is.null(colWidths)) hot$x$colWidths = colWidths |
| 365 |
|
| 366 |
if (!is.null(columnSorting)) hot$x$columnSorting = columnSorting |
| 367 |
if (!is.null(manualColumnMove)) hot$x$manualColumnMove = manualColumnMove |
| 368 |
if (!is.null(manualColumnResize)) hot$x$manualColumnResize = manualColumnResize |
| 369 |
|
| 370 |
if (!is.null(fixedColumnsLeft)) hot$x$fixedColumnsLeft = fixedColumnsLeft |
| 371 |
|
| 372 |
for (i in seq_len(length(hot$x$columns))) |
| 373 |
hot = hot %>% hot_col(i, ...) |
| 374 |
|
| 375 |
hot |
| 376 |
} |
| 377 |
|
| 378 |
#' Handsontable widget |
| 379 |
#' |
| 380 |
#' Configure single column. |
| 381 |
#' |
| 382 |
#' @param hot rhandsontable object |
| 383 |
#' @param col vector of column names or indices |
| 384 |
#' @param type character specify the data type. Options include: |
| 385 |
#' numeric, date, checkbox, select, dropdown, autocomplete, password, |
| 386 |
#' and handsontable (not implemented yet) |
| 387 |
#' @param format characer specifying column format. See Cell Types at |
| 388 |
#' \href{https://handsontable.com/}{Handsontable.js} for the formatting |
| 389 |
#' options for each data type. Numeric columns are formatted using |
| 390 |
#' \href{https://numbrojs.com}{Numbro.js}. |
| 391 |
#' @param source a vector of choices for select, dropdown and autocomplete |
| 392 |
#' column types |
| 393 |
#' @param strict logical specifying whether values not in the \code{source} |
| 394 |
#' vector will be accepted |
| 395 |
#' @param readOnly logical making the column read-only |
| 396 |
#' @param validator character defining a Javascript function to be used |
| 397 |
#' to validate user input. See \code{hot_validate_numeric} and |
| 398 |
#' \code{hot_validate_character} for pre-build validators. |
| 399 |
#' @param allowInvalid logical specifying whether invalid data will be |
| 400 |
#' accepted. Invalid data cells will be color red. |
| 401 |
#' @param halign character defining the horizontal alignment. Possible |
| 402 |
#' values are htLeft, htCenter, htRight and htJustify |
| 403 |
#' @param valign character defining the vertical alignment. Possible |
| 404 |
#' values are htTop, htMiddle, htBottom |
| 405 |
#' @param renderer character defining a Javascript function to be used |
| 406 |
#' to format column cells. Can be used to implement conditional formatting. |
| 407 |
#' @param copyable logical defining whether data in a cell can be copied using |
| 408 |
#' Ctrl + C |
| 409 |
#' @param dateFormat character defining the date format. See |
| 410 |
#' \href{https://github.com/moment/moment}{Moment.js} for details. |
| 411 |
#' @param default default column value for new rows (NA if not specified; shiny only) |
| 412 |
#' @param language locale passed to \href{https://numbrojs.com}{Numbro.js}; |
| 413 |
#' default is 'en-US'. |
| 414 |
#' @param ... passed to handsontable |
| 415 |
#' @examples |
| 416 |
#' library(rhandsontable) |
| 417 |
#' DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 418 |
#' small = letters[1:10], |
| 419 |
#' dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 420 |
#' stringsAsFactors = FALSE) |
| 421 |
#' |
| 422 |
#' rhandsontable(DF, rowHeaders = NULL) %>% |
| 423 |
#' hot_col(col = "big", type = "dropdown", source = LETTERS) %>% |
| 424 |
#' hot_col(col = "small", type = "autocomplete", source = letters, |
| 425 |
#' strict = FALSE) |
| 426 |
#' @seealso \code{\link{hot_cols}}, \code{\link{hot_rows}}, \code{\link{hot_cell}} |
| 427 |
#' @export |
| 428 |
hot_col = function(hot, col, type = NULL, format = NULL, source = NULL, |
| 429 |
strict = NULL, readOnly = NULL, validator = NULL, |
| 430 |
allowInvalid = NULL, halign = NULL, valign = NULL, |
| 431 |
renderer = NULL, copyable = NULL, dateFormat = NULL, |
| 432 |
default = NULL, language = NULL, ...) { |
| 433 |
cols = hot$x$columns |
| 434 |
if (is.null(cols)) { |
| 435 |
# create a columns list |
| 436 |
warning("rhandsontable column types were previously not defined but are ", |
| 437 |
"now being set to 'text' to support column properties") |
| 438 |
cols = lapply(hot$x$colHeaders, function(x) { |
| 439 |
list(type = "text") |
| 440 |
}) |
| 441 |
} |
| 442 |
|
| 443 |
for (i in col) { |
| 444 |
if (is.character(i)) i = which(hot$x$colHeaders == i) |
| 445 |
|
| 446 |
if (!is.null(type)) cols[[i]]$type = type |
| 447 |
if (!is.null(dateFormat)) cols[[i]]$dateFormat = dateFormat |
| 448 |
if (!is.null(source)) cols[[i]]$source = source |
| 449 |
if (!is.null(strict)) cols[[i]]$strict = strict |
| 450 |
if (!is.null(readOnly)) cols[[i]]$readOnly = readOnly |
| 451 |
if (!is.null(copyable)) cols[[i]]$copyable = copyable |
| 452 |
if (!is.null(default)) cols[[i]]$default = default |
| 453 |
|
| 454 |
if (!is.null(format) || !is.null(language)) cols[[i]]$numericFormat = list() |
| 455 |
if (!is.null(format)) cols[[i]]$numericFormat$pattern = format |
| 456 |
if (!is.null(language)) cols[[i]]$numericFormat$culture = language |
| 457 |
|
| 458 |
if (!is.null(validator)) cols[[i]]$validator = JS(validator) |
| 459 |
if (!is.null(allowInvalid)) cols[[i]]$allowInvalid = allowInvalid |
| 460 |
if (!is.null(renderer)) cols[[i]]$renderer = JS(renderer) |
| 461 |
|
| 462 |
if (!is.null(list(...))) |
| 463 |
cols[[i]] = c(cols[[i]], list(...)) |
| 464 |
|
| 465 |
className = c(halign, valign) |
| 466 |
if (!is.null(className)) { |
| 467 |
cols[[i]]$className = paste0(className, collapse = " ") |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
hot$x$columns = cols |
| 472 |
hot |
| 473 |
} |
| 474 |
|
| 475 |
#' Handsontable widget |
| 476 |
#' |
| 477 |
#' Configure row settings that pertain to the entire table. |
| 478 |
#' Note that hot_rows is not to be confused with \code{\link{hot_row}}. See |
| 479 |
#' \href{https://handsontable.com/}{Handsontable.js} for details. |
| 480 |
#' |
| 481 |
#' @param hot rhandsontable object |
| 482 |
#' @param rowHeights a scalar or numeric vector of row heights |
| 483 |
#' @param fixedRowsTop a scaler indicating the number of rows to |
| 484 |
#' freeze on the top |
| 485 |
#' @examples |
| 486 |
#' library(rhandsontable) |
| 487 |
#' MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 488 |
#' letters[1:5])) |
| 489 |
#' |
| 490 |
#' rhandsontable(MAT, width = 300, height = 150) %>% |
| 491 |
#' hot_cols(colWidths = 100, fixedColumnsLeft = 1) %>% |
| 492 |
#' hot_rows(rowHeights = 50, fixedRowsTop = 1) |
| 493 |
#' @seealso \code{\link{hot_cols}}, \code{\link{hot_cell}} |
| 494 |
#' @export |
| 495 |
hot_rows = function(hot, rowHeights = NULL, fixedRowsTop = NULL) { |
| 496 |
if (!is.null(rowHeights)) hot$x$rowHeights = rowHeights |
| 497 |
if (!is.null(fixedRowsTop)) hot$x$fixedRowsTop = fixedRowsTop |
| 498 |
hot |
| 499 |
} |
| 500 |
|
| 501 |
#' Handsontable widget |
| 502 |
#' |
| 503 |
#' Configure properties of all cells in a given row(s). |
| 504 |
#' Note that hot_row is not to be confused with \code{\link{hot_rows}}. See |
| 505 |
#' \href{https://handsontable.com/}{Handsontable.js} for details. |
| 506 |
#' |
| 507 |
#' @param hot rhandsontable object |
| 508 |
#' @param row numeric vector of row indexes |
| 509 |
#' @param readOnly logical making the row(s) read-only |
| 510 |
#' @examples |
| 511 |
#' library(rhandsontable) |
| 512 |
#' MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 513 |
#' letters[1:5])) |
| 514 |
#' |
| 515 |
#' rhandsontable(MAT, width = 300, height = 150) %>% |
| 516 |
#' hot_row(c(1,3:5), readOnly = TRUE) |
| 517 |
#' @seealso \code{\link{hot_cols}}, \code{\link{hot_cell}}, \code{\link{hot_rows}} |
| 518 |
#' @export |
| 519 |
hot_row = function(hot, row, readOnly = NULL) { |
| 520 |
if ( !is.null(readOnly) ) { |
| 521 |
colDim = hot$x$rDataDim[2] |
| 522 |
for ( i in row ) { |
| 523 |
for ( j in seq_len(colDim) ) { |
| 524 |
hot = hot %>% hot_cell(i, j, readOnly = readOnly) |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
hot |
| 529 |
} |
| 530 |
|
| 531 |
#' Handsontable widget |
| 532 |
#' |
| 533 |
#' Configure single cell. See |
| 534 |
#' \href{https://handsontable.com/}{Handsontable.js} for details. |
| 535 |
#' |
| 536 |
#' @param hot rhandsontable object |
| 537 |
#' @param row numeric row index |
| 538 |
#' @param col column name or index |
| 539 |
#' @param comment character comment to add to cell |
| 540 |
#' @param readOnly logical making the cell read-only |
| 541 |
#' @examples |
| 542 |
#' library(rhandsontable) |
| 543 |
#' DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 544 |
#' small = letters[1:10], |
| 545 |
#' dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 546 |
#' stringsAsFactors = FALSE) |
| 547 |
#' |
| 548 |
#' rhandsontable(DF) %>% |
| 549 |
#' hot_cell(1, 1, comment = "Test comment") %>% |
| 550 |
#' hot_cell(2, 3, readOnly = TRUE) |
| 551 |
#' @seealso \code{\link{hot_cols}}, \code{\link{hot_rows}} |
| 552 |
#' @export |
| 553 |
hot_cell = function(hot, row, col, comment = NULL, readOnly = NULL) { |
| 554 |
# TODO: consider moving comment functionality to hot_comment |
| 555 |
if (is.character(col)) col = which(hot$x$colHeaders == col) |
| 556 |
|
| 557 |
cell = list(row = row - 1, col = col - 1) |
| 558 |
|
| 559 |
if (!is.null(comment)) cell$comment = list(value = comment) |
| 560 |
if (!is.null(readOnly)) cell$readOnly = readOnly |
| 561 |
|
| 562 |
hot$x$cell = c(hot$x$cell, list(cell)) |
| 563 |
|
| 564 |
if (!is.null(comment)) hot = hot %>% hot_table(enableComments = TRUE) |
| 565 |
|
| 566 |
hot |
| 567 |
} |
| 568 |
|
| 569 |
#' Handsontable widget |
| 570 |
#' |
| 571 |
#' Add numeric validation to a column |
| 572 |
#' |
| 573 |
#' @param hot rhandsontable object |
| 574 |
#' @param cols vector of column names or indices |
| 575 |
#' @param min minimum value to accept |
| 576 |
#' @param max maximum value to accept |
| 577 |
#' @param choices a vector of acceptable numeric choices. It will be evaluated |
| 578 |
#' after min and max if specified. |
| 579 |
#' @param exclude a vector of unacceptable numeric values |
| 580 |
#' @param allowInvalid logical specifying whether invalid data will be |
| 581 |
#' accepted. Invalid data cells will be color red. |
| 582 |
#' @examples |
| 583 |
#' library(rhandsontable) |
| 584 |
#' MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 585 |
#' letters[1:5])) |
| 586 |
#' |
| 587 |
#' rhandsontable(MAT * 10) %>% |
| 588 |
#' hot_validate_numeric(col = 1, min = -50, max = 50, exclude = 40) |
| 589 |
#' |
| 590 |
#' rhandsontable(MAT * 10) %>% |
| 591 |
#' hot_validate_numeric(col = 1, choices = c(10, 20, 40)) |
| 592 |
#' @seealso \code{\link{hot_validate_character}} |
| 593 |
#' @export |
| 594 |
hot_validate_numeric = function(hot, cols, min = NULL, max = NULL, |
| 595 |
choices = NULL, exclude = NULL, |
| 596 |
allowInvalid = FALSE) { |
| 597 |
f = "function (value, callback) { |
| 598 |
if (value === null || value === void 0) { |
| 599 |
value = ''; |
| 600 |
} |
| 601 |
if (this.allowEmpty && value === '') { |
| 602 |
return callback(true); |
| 603 |
} else if (value === '') { |
| 604 |
return callback(false); |
| 605 |
} |
| 606 |
let isNumber = /^-?\\d*(\\.|,)?\\d*$/.test(value); |
| 607 |
if (!isNumber) { |
| 608 |
return callback(false); |
| 609 |
} |
| 610 |
if (isNaN(parseFloat(value))) { |
| 611 |
return callback(false); |
| 612 |
} |
| 613 |
%exclude |
| 614 |
%min |
| 615 |
%max |
| 616 |
%choices |
| 617 |
return callback(true); |
| 618 |
}" |
| 619 |
|
| 620 |
if (!is.null(exclude)) |
| 621 |
ex_str = paste0("if ([", |
| 622 |
paste0(paste0("'", exclude, "'"), collapse = ","), |
| 623 |
"].indexOf(value) > -1) { return callback(false); }") |
| 624 |
else |
| 625 |
ex_str = "" |
| 626 |
f = gsub("%exclude", ex_str, f) |
| 627 |
|
| 628 |
if (!is.null(min)) |
| 629 |
min_str = paste0("if (value < ", min, ") { return callback(false); }") |
| 630 |
else |
| 631 |
min_str = "" |
| 632 |
f = gsub("%min", min_str, f) |
| 633 |
|
| 634 |
if (!is.null(max)) |
| 635 |
max_str = paste0("if (value > ", max, ") { return callback(false); }") |
| 636 |
else |
| 637 |
max_str = "" |
| 638 |
f = gsub("%max", max_str, f) |
| 639 |
|
| 640 |
if (!is.null(choices)) |
| 641 |
chcs_str = paste0("if ([", |
| 642 |
paste0(paste0("'", choices, "'"), collapse = ","), |
| 643 |
"].indexOf(value) == -1) { return callback(false); }") |
| 644 |
else |
| 645 |
chcs_str = "" |
| 646 |
f = gsub("%choices", chcs_str, f) |
| 647 |
|
| 648 |
for (x in cols) |
| 649 |
hot = hot %>% hot_col(x, validator = f, |
| 650 |
allowInvalid = allowInvalid) |
| 651 |
|
| 652 |
hot |
| 653 |
} |
| 654 |
|
| 655 |
#' Handsontable widget |
| 656 |
#' |
| 657 |
#' Add numeric validation to a column |
| 658 |
#' |
| 659 |
#' @param hot rhandsontable object |
| 660 |
#' @param cols vector of column names or indices |
| 661 |
#' @param choices a vector of acceptable numeric choices. It will be evaluated |
| 662 |
#' after min and max if specified. |
| 663 |
#' @param allowInvalid logical specifying whether invalid data will be |
| 664 |
#' accepted. Invalid data cells will be color red. |
| 665 |
#' @examples |
| 666 |
#' library(rhandsontable) |
| 667 |
#' DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 668 |
#' small = letters[1:10], |
| 669 |
#' dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 670 |
#' stringsAsFactors = FALSE) |
| 671 |
#' |
| 672 |
#' rhandsontable(DF) %>% |
| 673 |
#' hot_validate_character(col = "big", choices = LETTERS[1:10]) |
| 674 |
#' @seealso \code{\link{hot_validate_numeric}} |
| 675 |
#' @export |
| 676 |
hot_validate_character = function(hot, cols, choices, |
| 677 |
allowInvalid = FALSE) { |
| 678 |
f = "function (value, callback) { |
| 679 |
setTimeout(function() { |
| 680 |
if (typeof(value) != 'string') { |
| 681 |
return callback(false); |
| 682 |
} |
| 683 |
%choices |
| 684 |
return callback(false); |
| 685 |
}, 500) |
| 686 |
}" |
| 687 |
|
| 688 |
ch_str = paste0("if ([", |
| 689 |
paste0(paste0("'", choices, "'"), collapse = ","), |
| 690 |
"].indexOf(value) > -1) { return callback(true); }") |
| 691 |
f = gsub("%choices", ch_str, f) |
| 692 |
|
| 693 |
for (x in cols) |
| 694 |
hot = hot %>% hot_col(x, validator = f, |
| 695 |
allowInvalid = allowInvalid) |
| 696 |
|
| 697 |
hot |
| 698 |
} |
| 699 |
|
| 700 |
#' Handsontable widget |
| 701 |
#' |
| 702 |
#' Add heatmap to table. |
| 703 |
#' |
| 704 |
#' @param hot rhandsontable object |
| 705 |
#' @param cols numeric vector of columns to include in the heatmap. If missing |
| 706 |
#' all columns are used. |
| 707 |
#' @param color_scale character vector that includes the lower and upper |
| 708 |
#' colors |
| 709 |
#' @param renderer character defining a Javascript function to be used |
| 710 |
#' to determine the cell colors. If missing, |
| 711 |
#' \code{rhandsontable:::renderer_heatmap} is used. |
| 712 |
#' @examples |
| 713 |
#' MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 714 |
#' letters[1:5])) |
| 715 |
#' |
| 716 |
#'rhandsontable(MAT) %>% |
| 717 |
#' hot_heatmap() |
| 718 |
#' @export |
| 719 |
hot_heatmap = function(hot, cols, color_scale = c("#ED6D47", "#17F556"), |
| 720 |
renderer = NULL) { |
| 721 |
hot$x$isHeatmap = TRUE |
| 722 |
|
| 723 |
if (is.null(renderer)) { |
| 724 |
renderer = renderer_heatmap(color_scale) |
| 725 |
} |
| 726 |
|
| 727 |
if (missing(cols)) |
| 728 |
cols = seq_along(hot$x$colHeaders) |
| 729 |
for (x in hot$x$colHeaders[cols]) |
| 730 |
hot = hot %>% hot_col(x, renderer = renderer) |
| 731 |
|
| 732 |
hot |
| 733 |
} |
| 734 |
|
| 735 |
# Used by hot_heatmap |
| 736 |
renderer_heatmap = function(color_scale) { |
| 737 |
renderer = gsub("\n", "", " |
| 738 |
function (instance, td, row, col, prop, value, cellProperties) { |
| 739 |
|
| 740 |
Handsontable.renderers.NumericRenderer.apply(this, arguments); |
| 741 |
heatmapScale = chroma.scale(['%s1', '%s2']); |
| 742 |
|
| 743 |
if (instance.heatmap[col]) { |
| 744 |
mn = instance.heatmap[col].min; |
| 745 |
mx = instance.heatmap[col].max; |
| 746 |
pt = (parseInt(value, 10) - mn) / (mx - mn); |
| 747 |
|
| 748 |
td.style.backgroundColor = heatmapScale(pt).hex(); |
| 749 |
} |
| 750 |
} |
| 751 |
") |
| 752 |
renderer = gsub("%s1", color_scale[1], renderer) |
| 753 |
renderer = gsub("%s2", color_scale[2], renderer) |
| 754 |
renderer |
| 755 |
} |
| 756 |
|
| 757 |
#' Handsontable widget |
| 758 |
#' |
| 759 |
#' Shiny bindings for rhandsontable |
| 760 |
#' |
| 761 |
#' @param outputId output variable to read from |
| 762 |
#' @param width,height must be a valid CSS unit in pixels |
| 763 |
#' or a number, which will be coerced to a string and have \code{"px"} appended. |
| 764 |
#' @seealso \code{\link{renderRHandsontable}} |
| 765 |
#' @export |
| 766 |
rHandsontableOutput <- function(outputId, width = "100%", height = "100%"){ |
| 767 |
htmlwidgets::shinyWidgetOutput(outputId, 'rhandsontable', width, height, |
| 768 |
package = 'rhandsontable') |
| 769 |
} |
| 770 |
|
| 771 |
#' Handsontable widget |
| 772 |
#' |
| 773 |
#' Shiny bindings for rhandsontable |
| 774 |
#' |
| 775 |
#' @param expr an expression that generates an rhandsontable. |
| 776 |
#' @param env the environment in which to evaluate \code{expr}. |
| 777 |
#' @param quoted is \code{expr} a quoted expression (with \code{quote()})? This |
| 778 |
#' is useful if you want to save an expression in a variable. |
| 779 |
#' @seealso \code{\link{rHandsontableOutput}}, \code{\link{hot_to_r}} |
| 780 |
#' @export |
| 781 |
renderRHandsontable <- function(expr, env = parent.frame(), quoted = FALSE) { |
| 782 |
if (!quoted) { expr <- substitute(expr) } # force quoted |
| 783 |
htmlwidgets::shinyRenderWidget(expr, rHandsontableOutput, env, quoted = TRUE) |
| 784 |
} |
| 785 |
|
| 786 |
#' Handsontable widget |
| 787 |
#' |
| 788 |
#' Convert handsontable data to R object. Can be used in a \code{shiny} app |
| 789 |
#' to convert the input json to an R dataset. |
| 790 |
#' |
| 791 |
#' @param ... passed to \code{rhandsontable:::toR} |
| 792 |
#' @seealso \code{\link{rHandsontableOutput}} |
| 793 |
#' @export |
| 794 |
hot_to_r = function(...) { |
| 795 |
if (is.null(list(...)[[1]])) return(NULL) |
| 796 |
do.call(toR, ...) |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
#' Handsontable widget |
| 802 |
#' |
| 803 |
#' Set data inside a Handsontable instance without recreating the widget. Send the new values as a vector of rows, a vector of columns, and a vector of values. If different length vectors are supplied then the shorter ones are recycled to match the length of the longest. |
| 804 |
#' |
| 805 |
#' @param id The id of the table to interact with. |
| 806 |
#' @param row Integer vector of row indexes. |
| 807 |
#' @param col Integer vector the column indexes. |
| 808 |
#' @param val Vector of values to set at each row-col pair. |
| 809 |
#' @param session The session that is associated with your shiny server function. The table is only interactive when used in shiny so we only use set_data when the table is in shiny. |
| 810 |
#' @param zero_indexed Default FALSE. Set to TRUE if you are supplying row and col indexes that are already 0-based. |
| 811 |
#' @export |
| 812 |
set_data = function(id, row, col, val, session, zero_indexed=F) { |
| 813 |
# make sure rows and cols are integers |
| 814 |
row <- as.integer(row) |
| 815 |
col <- as.integer(col) |
| 816 |
|
| 817 |
# javacript is zero-based indexed while R is 1-based |
| 818 |
# we assume the R user is using 1-based so we subtract 1 from rows and cols |
| 819 |
# if the user provides positions that are 0-based then we skip this |
| 820 |
if( !zero_indexed ){ |
| 821 |
row <- row - 1 |
| 822 |
col <- col - 1 |
| 823 |
} |
| 824 |
|
| 825 |
# make sure the provided rows and cols are finite, non-negative values |
| 826 |
stopifnot(exprs = { all(c(row,col) >= 0, is.finite(c(row, col))) } ) |
| 827 |
|
| 828 |
# use recycling to ensure equal length vectors |
| 829 |
vec_length <- max(length(row), length(col), length(val)) |
| 830 |
row <- rep(row, length.out=vec_length) |
| 831 |
col <- rep(col, length.out=vec_length) |
| 832 |
val <- rep(val, length.out=vec_length) |
| 833 |
|
| 834 |
# send the list of data out to the message handler |
| 835 |
session$sendCustomMessage('handler_setDataAtCell', |
| 836 |
list('id' = id, |
| 837 |
'size'= length(val), |
| 838 |
'row' = row, |
| 839 |
'col' = col, |
| 840 |
'val' = val)) |
| 841 |
} |