Skip to content

Commit 25b3dda

Browse files
authored
Add row limit setting of data viewer and support Apache Arrow Table (#945)
* add row limit setting of data viewer * add setting to convert Apache Arrow Table to a data frame and display it in the data viewer * fix * remove the show_arrow_table option * refactoring: functionize the processing to trunc data * add the overall number of rows to the title
1 parent 9275ca1 commit 25b3dda

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

R/session/vsc.R

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ load_settings <- function() {
3131
vsc.browser = setting(session$viewers$viewColumn$browser, Disable = FALSE),
3232
vsc.viewer = setting(session$viewers$viewColumn$viewer, Disable = FALSE),
3333
vsc.page_viewer = setting(session$viewers$viewColumn$pageViewer, Disable = FALSE),
34+
vsc.row_limit = session$data$rowLimit,
3435
vsc.view = setting(session$viewers$viewColumn$view, Disable = FALSE),
3536
vsc.helpPanel = setting(session$viewers$viewColumn$helpPanel, Disable = FALSE)
3637
))
@@ -346,7 +347,7 @@ if (show_view) {
346347
}
347348

348349
if (is.data.frame(data)) {
349-
nrow <- nrow(data)
350+
.nrow <- nrow(data)
350351
colnames <- colnames(data)
351352
if (is.null(colnames)) {
352353
colnames <- sprintf("V%d", seq_len(ncol(data)))
@@ -357,14 +358,14 @@ if (show_view) {
357358
rownames <- rownames(data)
358359
rownames(data) <- NULL
359360
} else {
360-
rownames <- seq_len(nrow)
361+
rownames <- seq_len(.nrow)
361362
}
362363
colnames <- c("(row)", colnames)
363364
fields <- sprintf("x%d", seq_along(colnames))
364365
data <- c(list(" " = rownames), .subset(data))
365366
names(data) <- fields
366367
class(data) <- "data.frame"
367-
attr(data, "row.names") <- .set_row_names(nrow)
368+
attr(data, "row.names") <- .set_row_names(.nrow)
368369
columns <- .mapply(get_column_def,
369370
list(colnames, fields, data),
370371
NULL
@@ -379,11 +380,25 @@ if (show_view) {
379380
}
380381

381382
show_dataview <- function(x, title, uuid = NULL,
382-
viewer = getOption("vsc.view", "Two")) {
383+
viewer = getOption("vsc.view", "Two"),
384+
row_limit = abs(getOption("vsc.row_limit", 0))) {
385+
as_truncated_data <- function(.data) {
386+
.nrow <- nrow(.data)
387+
if (row_limit != 0 && row_limit < .nrow) {
388+
title <<- sprintf("%s (limited to %d/%d)", title, row_limit, .nrow)
389+
.data <- utils::head(.data, n = row_limit)
390+
}
391+
return(.data)
392+
}
393+
383394
if (missing(title)) {
384395
sub <- substitute(x)
385396
title <- deparse(sub, nlines = 1)
386397
}
398+
if (inherits(x, "ArrowTabular")) {
399+
x <- as_truncated_data(x)
400+
x <- as.data.frame(x)
401+
}
387402
if (is.environment(x)) {
388403
all_names <- ls(x)
389404
is_promise <- rlang::env_binding_are_lazy(x, all_names)
@@ -438,6 +453,7 @@ if (show_view) {
438453
}
439454
}
440455
if (is.data.frame(x) || is.matrix(x)) {
456+
x <- as_truncated_data(x)
441457
data <- dataview_table(x)
442458
file <- tempfile(tmpdir = tempdir, fileext = ".json")
443459
jsonlite::write_json(data, file, na = "string", null = "null", auto_unbox = TRUE)

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,11 @@
14481448
"default": true,
14491449
"markdownDescription": "Emulate the RStudio API for addin support and other {rstudioapi} calls. Changes the option `vsc.rstudioapi` in R. Requires `#r.sessionWatcher#` to be set to `true`."
14501450
},
1451+
"r.session.data.rowLimit": {
1452+
"type": "integer",
1453+
"default": 0,
1454+
"markdownDescription": "The maximum number of rows to be displayed in the data viewer. `0` means no limit. Changes the option `vsc.row_limit` in R. Requires `#r.sessionWatcher#` to be set to `true`."
1455+
},
14511456
"r.session.viewers.viewColumn": {
14521457
"type": "object",
14531458
"markdownDescription": "Which view column should R-related webviews be displayed? Requires `#r.sessionWatcher#` to be set to `true`.",

0 commit comments

Comments
 (0)