| 1 |
## ----echo = FALSE, warning = FALSE-------------------------------------------- |
| 2 |
library(rhandsontable) |
| 3 |
library(knitr) |
| 4 |
|
| 5 |
opts_knit$set(warning = FALSE, error = FALSE, message = FALSE, cache = FALSE, |
| 6 |
fig.width=7, fig.height=3) |
| 7 |
|
| 8 |
## ----------------------------------------------------------------------------- |
| 9 |
DF = data.frame(integer = 1:10, |
| 10 |
numeric = rnorm(10), |
| 11 |
logical = rep(TRUE, 10), |
| 12 |
character = LETTERS[1:10], |
| 13 |
factor = factor(letters[1:10], levels = letters[10:1], |
| 14 |
ordered = TRUE), |
| 15 |
factor_allow = factor(letters[1:10], levels = letters[10:1], |
| 16 |
ordered = TRUE), |
| 17 |
date = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 18 |
stringsAsFactors = FALSE) |
| 19 |
|
| 20 |
rhandsontable(DF, width = 600, height = 300) %>% |
| 21 |
hot_col("factor_allow", allowInvalid = TRUE) |
| 22 |
|
| 23 |
## ----------------------------------------------------------------------------- |
| 24 |
DF_na = data.frame(integer = c(NA, 2:10), |
| 25 |
logical = c(NA, rep(TRUE, 9)), |
| 26 |
character = c(NA, LETTERS[1:9]), |
| 27 |
factor = c(NA, factor(letters[1:9])), |
| 28 |
date = c(NA, seq(from = Sys.Date(), by = "days", |
| 29 |
length.out = 9)), |
| 30 |
stringsAsFactors = FALSE) |
| 31 |
|
| 32 |
DF_na$factor_ch = as.character(DF_na$factor) |
| 33 |
DF_na$date_ch = c(NA, as.character(seq(from = Sys.Date(), by = "days", |
| 34 |
length.out = 9))) |
| 35 |
|
| 36 |
rhandsontable(DF_na, width = 550, height = 300) |
| 37 |
|
| 38 |
## ----------------------------------------------------------------------------- |
| 39 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 40 |
small = letters[1:10], |
| 41 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 42 |
stringsAsFactors = FALSE) |
| 43 |
|
| 44 |
# try updating big to a value not in the dropdown |
| 45 |
rhandsontable(DF, rowHeaders = NULL, width = 550, height = 300) %>% |
| 46 |
hot_col(col = "big", type = "dropdown", source = LETTERS) %>% |
| 47 |
hot_col(col = "small", type = "autocomplete", source = letters, |
| 48 |
strict = FALSE) |
| 49 |
|
| 50 |
## ----------------------------------------------------------------------------- |
| 51 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 52 |
small = letters[1:10], |
| 53 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 54 |
stringsAsFactors = FALSE) |
| 55 |
|
| 56 |
rhandsontable(DF, width = 550, height = 300) %>% |
| 57 |
hot_col("small", "password") |
| 58 |
|
| 59 |
## ----------------------------------------------------------------------------- |
| 60 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 61 |
small = letters[1:10], |
| 62 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 63 |
stringsAsFactors = FALSE) |
| 64 |
|
| 65 |
DF$chart = c(sapply(1:5, |
| 66 |
function(x) jsonlite::toJSON(list(values=rnorm(10), |
| 67 |
options = list(type = "bar")))), |
| 68 |
sapply(1:5, |
| 69 |
function(x) jsonlite::toJSON(list(values=rnorm(10), |
| 70 |
options = list(type = "line"))))) |
| 71 |
|
| 72 |
rhandsontable(DF, rowHeaders = NULL, width = 550, height = 300) %>% |
| 73 |
hot_col("chart", renderer = htmlwidgets::JS("renderSparkline")) |
| 74 |
|
| 75 |
## ----fig.height = 5, fig.width = 8-------------------------------------------- |
| 76 |
DF = data.frame( |
| 77 |
title = c( |
| 78 |
"<a href='http://www.amazon.com/Professional-JavaScript-Developers-Nicholas-Zakas/dp/1118026691'>Professional JavaScript for Web Developers</a>", |
| 79 |
"<a href='http://shop.oreilly.com/product/9780596517748.do'>JavaScript: The Good Parts</a>", |
| 80 |
"<a href='http://shop.oreilly.com/product/9780596805531.do'>JavaScript: The Definitive Guide</a>" |
| 81 |
), |
| 82 |
desc = c( |
| 83 |
"This <a href='http://bit.ly/sM1bDf'>book</a> provides a developer-level introduction along with more advanced and useful features of <b>JavaScript</b>.", |
| 84 |
"This book provides a developer-level introduction along with <b>more advanced</b> and useful features of JavaScript.", |
| 85 |
"<em>JavaScript: The Definitive Guide</em> provides a thorough description of the core <b>JavaScript</b> language and both the legacy and standard DOMs implemented in web browsers." |
| 86 |
), |
| 87 |
comments = c( |
| 88 |
"I would rate it ★★★★☆", |
| 89 |
"This is the book about JavaScript", |
| 90 |
"I've never actually read it, but the <a href='http://shop.oreilly.com/product/9780596805531.do'>comments</a> are highly <strong>positive</strong>." |
| 91 |
), |
| 92 |
cover = c( |
| 93 |
"http://ecx.images-amazon.com/images/I/51bRhyVTVGL._SL50_.jpg", |
| 94 |
"http://ecx.images-amazon.com/images/I/51gdVAEfPUL._SL50_.jpg", |
| 95 |
"http://ecx.images-amazon.com/images/I/51VFNL4T7kL._SL50_.jpg" |
| 96 |
), |
| 97 |
stringsAsFactors = FALSE |
| 98 |
) |
| 99 |
|
| 100 |
rhandsontable(DF, allowedTags = "<em><b><strong><a><big>", |
| 101 |
width = 800, height = 450, rowHeaders = FALSE) %>% |
| 102 |
hot_cols(colWidths = c(200, 200, 200, 80)) %>% |
| 103 |
hot_col(1:2, renderer = "html") %>% |
| 104 |
hot_col(1:3, renderer = htmlwidgets::JS("safeHtmlRenderer")) %>% |
| 105 |
hot_col(4, renderer = " |
| 106 |
function(instance, td, row, col, prop, value, cellProperties) { |
| 107 |
var escaped = Handsontable.helper.stringify(value), |
| 108 |
img; |
| 109 |
|
| 110 |
if (escaped.indexOf('http') === 0) { |
| 111 |
img = document.createElement('IMG'); |
| 112 |
img.src = value; |
| 113 |
|
| 114 |
Handsontable.dom.addEvent(img, 'mousedown', function (e){ |
| 115 |
e.preventDefault(); // prevent selection quirk |
| 116 |
}); |
| 117 |
|
| 118 |
Handsontable.dom.empty(td); |
| 119 |
td.appendChild(img); |
| 120 |
} |
| 121 |
else { |
| 122 |
// render as text |
| 123 |
Handsontable.renderers.TextRenderer.apply(this, arguments); |
| 124 |
} |
| 125 |
|
| 126 |
return td; |
| 127 |
}") |
| 128 |
|
| 129 |
## ----------------------------------------------------------------------------- |
| 130 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 131 |
small = letters[1:10], |
| 132 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 133 |
stringsAsFactors = FALSE) |
| 134 |
|
| 135 |
col_highlight = 2 |
| 136 |
row_highlight = c(5, 7) |
| 137 |
|
| 138 |
rhandsontable(DF, col_highlight = col_highlight, |
| 139 |
row_highlight = row_highlight, |
| 140 |
width = 550, height = 300) %>% |
| 141 |
hot_cols(renderer = " |
| 142 |
function(instance, td, row, col, prop, value, cellProperties) { |
| 143 |
Handsontable.renderers.TextRenderer.apply(this, arguments); |
| 144 |
|
| 145 |
tbl = this.HTMLWidgets.widgets[0] |
| 146 |
|
| 147 |
hcols = tbl.params.col_highlight |
| 148 |
hcols = hcols instanceof Array ? hcols : [hcols] |
| 149 |
hrows = tbl.params.row_highlight |
| 150 |
hrows = hrows instanceof Array ? hrows : [hrows] |
| 151 |
|
| 152 |
if (hcols.includes(col) && hrows.includes(row)) { |
| 153 |
td.style.background = 'red'; |
| 154 |
} |
| 155 |
else if (hcols.includes(col)) { |
| 156 |
td.style.background = 'lightgreen'; |
| 157 |
} |
| 158 |
else if (hrows.includes(row)) { |
| 159 |
td.style.background = 'pink'; |
| 160 |
} |
| 161 |
|
| 162 |
return td; |
| 163 |
}") |
| 164 |
|
| 165 |
## ----------------------------------------------------------------------------- |
| 166 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 167 |
small = letters[1:10], |
| 168 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 169 |
stringsAsFactors = FALSE) |
| 170 |
|
| 171 |
rhandsontable(DF, width = 550, height = 300) %>% |
| 172 |
hot_context_menu(allowRowEdit = FALSE, allowColEdit = FALSE) |
| 173 |
|
| 174 |
## ---- warning = FALSE--------------------------------------------------------- |
| 175 |
MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 176 |
letters[1:5])) |
| 177 |
|
| 178 |
rhandsontable(MAT, width = 550, height = 300) %>% |
| 179 |
hot_context_menu( |
| 180 |
customOpts = list( |
| 181 |
csv = list(name = "Download to CSV", |
| 182 |
callback = htmlwidgets::JS( |
| 183 |
"function (key, options) { |
| 184 |
var csv = csvString(this); |
| 185 |
|
| 186 |
var link = document.createElement('a'); |
| 187 |
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + |
| 188 |
encodeURIComponent(csv)); |
| 189 |
link.setAttribute('download', 'data.csv'); |
| 190 |
|
| 191 |
document.body.appendChild(link); |
| 192 |
link.click(); |
| 193 |
document.body.removeChild(link); |
| 194 |
}")))) |
| 195 |
|
| 196 |
## ---- warning = FALSE--------------------------------------------------------- |
| 197 |
DF = data.frame(val = 1:10, |
| 198 |
bool = TRUE, |
| 199 |
big = LETTERS[1:10], |
| 200 |
small = factor(letters[1:10]), |
| 201 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 202 |
stringsAsFactors = FALSE) |
| 203 |
|
| 204 |
rhandsontable(DF, search = TRUE, width = 550, height = 300) %>% |
| 205 |
hot_context_menu( |
| 206 |
customOpts = list( |
| 207 |
search = list(name = "Search", |
| 208 |
callback = htmlwidgets::JS( |
| 209 |
"function (key, options) { |
| 210 |
var srch = prompt('Search criteria'); |
| 211 |
|
| 212 |
this.search.query(srch); |
| 213 |
this.render(); |
| 214 |
}")))) |
| 215 |
|
| 216 |
## ----------------------------------------------------------------------------- |
| 217 |
DF = data.frame(int = 1:10, float = rnorm(10), cur = rnorm(10) * 1E5, |
| 218 |
lrg = rnorm(10) * 1E8, pct = rnorm(10)) |
| 219 |
|
| 220 |
rhandsontable(DF, width = 550, height = 300) %>% |
| 221 |
hot_col("float", format = "0.0") %>% |
| 222 |
hot_col("cur", format = "$0,0.00") %>% |
| 223 |
hot_col("lrg", format = "0a") %>% |
| 224 |
hot_col("pct", format = "0%") |
| 225 |
|
| 226 |
## ----------------------------------------------------------------------------- |
| 227 |
DF = data.frame(dollar = rnorm(10), euro = rnorm(10), yen = rnorm(10)) |
| 228 |
|
| 229 |
rhandsontable(DF * 1000, width = 550, height = 300) %>% |
| 230 |
hot_col("dollar", format = "$0,000.00", language = "en-US") %>% |
| 231 |
hot_col("euro", format = "0,000.00 $", language = "de-DE") %>% |
| 232 |
hot_col("yen", format = "$0,000.00", language = "ja-JP") |
| 233 |
|
| 234 |
## ----------------------------------------------------------------------------- |
| 235 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 236 |
small = letters[1:10], |
| 237 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 238 |
stringsAsFactors = FALSE) |
| 239 |
|
| 240 |
rhandsontable(DF, readOnly = TRUE, width = 550, height = 300) %>% |
| 241 |
hot_col("val", readOnly = FALSE) |
| 242 |
|
| 243 |
## ----------------------------------------------------------------------------- |
| 244 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 245 |
small = letters[1:10], |
| 246 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 247 |
stringsAsFactors = FALSE) |
| 248 |
|
| 249 |
rhandsontable(DF, width = 550, height = 300) %>% |
| 250 |
hot_cols(columnSorting = TRUE) |
| 251 |
|
| 252 |
## ----------------------------------------------------------------------------- |
| 253 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 254 |
small = letters[1:10], |
| 255 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 256 |
stringsAsFactors = FALSE) |
| 257 |
|
| 258 |
# click on a cell to see the highlighting |
| 259 |
rhandsontable(DF, width = 550, height = 300) %>% |
| 260 |
hot_table(highlightCol = TRUE, highlightRow = TRUE) |
| 261 |
|
| 262 |
## ----fig.height = 6, fig.width = 6-------------------------------------------- |
| 263 |
MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 264 |
letters[1:5])) |
| 265 |
|
| 266 |
rhandsontable(MAT, width = 600, height = 600) %>% |
| 267 |
hot_cols(colWidths = 100) %>% |
| 268 |
hot_rows(rowHeights = 50) |
| 269 |
|
| 270 |
## ----fig.height = 6, fig.width = 6-------------------------------------------- |
| 271 |
rhandsontable(mtcars, rowHeaderWidth = 200) |
| 272 |
|
| 273 |
## ----fig.height = 6, fig.width = 6-------------------------------------------- |
| 274 |
MAT = matrix(rnorm(30), nrow = 10, dimnames = list(LETTERS[1:10], |
| 275 |
letters[1:3])) |
| 276 |
|
| 277 |
rhandsontable(MAT, width = 600, height = 300, stretchH = "all") |
| 278 |
|
| 279 |
## ----------------------------------------------------------------------------- |
| 280 |
MAT = matrix(rnorm(26 * 26), nrow = 26, dimnames = list(LETTERS, letters)) |
| 281 |
|
| 282 |
# scroll through the table to see the fixed row and column |
| 283 |
rhandsontable(MAT, width = 550, height = 300) %>% |
| 284 |
hot_cols(fixedColumnsLeft = 1) %>% |
| 285 |
hot_rows(fixedRowsTop = 1) |
| 286 |
|
| 287 |
## ----------------------------------------------------------------------------- |
| 288 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 289 |
small = letters[1:10], |
| 290 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 291 |
stringsAsFactors = FALSE) |
| 292 |
|
| 293 |
rhandsontable(DF, width = 550, height = 300) %>% |
| 294 |
hot_cell(1, 1, "Test comment") |
| 295 |
|
| 296 |
## ----------------------------------------------------------------------------- |
| 297 |
MAT_comments = matrix(ncol = ncol(DF), nrow = nrow(DF)) |
| 298 |
MAT_comments[1, 1] = "Test comment" |
| 299 |
MAT_comments[2, 2] = "Another test comment" |
| 300 |
|
| 301 |
rhandsontable(DF, comments = MAT_comments, width = 550, height = 300) |
| 302 |
|
| 303 |
## ----------------------------------------------------------------------------- |
| 304 |
MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 305 |
letters[1:5])) |
| 306 |
|
| 307 |
rhandsontable(MAT, width = 550, height = 300) %>% |
| 308 |
hot_table(customBorders = list(list( |
| 309 |
range = list(from = list(row = 1, col = 1), |
| 310 |
to = list(row = 2, col = 2)), |
| 311 |
top = list(width = 2, color = "red"), |
| 312 |
left = list(width = 2, color = "red"), |
| 313 |
bottom = list(width = 2, color = "red"), |
| 314 |
right = list(width = 2, color = "red")))) |
| 315 |
|
| 316 |
## ----------------------------------------------------------------------------- |
| 317 |
MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 318 |
letters[1:5])) |
| 319 |
|
| 320 |
rhandsontable(MAT * 10, width = 550, height = 300) %>% |
| 321 |
hot_validate_numeric(col = 1, min = -50, max = 50, exclude = 40) |
| 322 |
|
| 323 |
rhandsontable(MAT * 10, width = 550, height = 300) %>% |
| 324 |
hot_validate_numeric(col = 1, choices = c(10, 20, 40)) |
| 325 |
|
| 326 |
## ----------------------------------------------------------------------------- |
| 327 |
DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10], |
| 328 |
small = letters[1:10], |
| 329 |
dt = seq(from = Sys.Date(), by = "days", length.out = 10), |
| 330 |
stringsAsFactors = FALSE) |
| 331 |
|
| 332 |
rhandsontable(DF, width = 550, height = 300) %>% |
| 333 |
hot_validate_character(col = "big", choices = LETTERS[1:10]) |
| 334 |
|
| 335 |
## ----------------------------------------------------------------------------- |
| 336 |
MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 337 |
letters[1:5])) |
| 338 |
|
| 339 |
# try to update any cell to 0 |
| 340 |
rhandsontable(MAT * 10, width = 550, height = 300) %>% |
| 341 |
hot_cols(validator = " |
| 342 |
function (value, callback) { |
| 343 |
setTimeout(function(){ |
| 344 |
callback(value != 0); |
| 345 |
}, 1000) |
| 346 |
}", |
| 347 |
allowInvalid = FALSE) |
| 348 |
|
| 349 |
## ---- fig.width = 8----------------------------------------------------------- |
| 350 |
MAT = matrix(runif(100, -1, 1), nrow = 10, |
| 351 |
dimnames = list(LETTERS[1:10], LETTERS[1:10])) |
| 352 |
diag(MAT) = 1 |
| 353 |
MAT[upper.tri(MAT)] = MAT[lower.tri(MAT)] |
| 354 |
rhandsontable(MAT, readOnly = TRUE, width = 750, height = 300) %>% |
| 355 |
hot_cols(renderer = " |
| 356 |
function (instance, td, row, col, prop, value, cellProperties) { |
| 357 |
Handsontable.renderers.TextRenderer.apply(this, arguments); |
| 358 |
if (row == col) { |
| 359 |
td.style.background = 'lightgrey'; |
| 360 |
} else if (col > row) { |
| 361 |
td.style.background = 'grey'; |
| 362 |
td.style.color = 'grey'; |
| 363 |
} else if (value < -0.75) { |
| 364 |
td.style.background = 'pink'; |
| 365 |
} else if (value > 0.75) { |
| 366 |
td.style.background = 'lightgreen'; |
| 367 |
} |
| 368 |
}") |
| 369 |
|
| 370 |
## ----------------------------------------------------------------------------- |
| 371 |
MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10], |
| 372 |
letters[1:5])) |
| 373 |
|
| 374 |
rhandsontable(MAT, width = 550, height = 300) %>% |
| 375 |
hot_heatmap() |
| 376 |
|
| 377 |
## ----------------------------------------------------------------------------- |
| 378 |
MAT = matrix(rnorm(10000 * 100), nrow = 100, dimnames= list(1:100, 1:10000)) |
| 379 |
|
| 380 |
rhandsontable(MAT, width = 550, height = 550) |
| 381 |
|