diff --git a/R/session/vsc.R b/R/session/vsc.R index 8f0505a9b..af1d25fed 100644 --- a/R/session/vsc.R +++ b/R/session/vsc.R @@ -31,6 +31,7 @@ load_settings <- function() { vsc.browser = setting(session$viewers$viewColumn$browser, Disable = FALSE), vsc.viewer = setting(session$viewers$viewColumn$viewer, Disable = FALSE), vsc.page_viewer = setting(session$viewers$viewColumn$pageViewer, Disable = FALSE), + vsc.row_limit = session$data$rowLimit, vsc.view = setting(session$viewers$viewColumn$view, Disable = FALSE), vsc.helpPanel = setting(session$viewers$viewColumn$helpPanel, Disable = FALSE) )) @@ -346,7 +347,7 @@ if (show_view) { } if (is.data.frame(data)) { - nrow <- nrow(data) + .nrow <- nrow(data) colnames <- colnames(data) if (is.null(colnames)) { colnames <- sprintf("V%d", seq_len(ncol(data))) @@ -357,14 +358,14 @@ if (show_view) { rownames <- rownames(data) rownames(data) <- NULL } else { - rownames <- seq_len(nrow) + rownames <- seq_len(.nrow) } colnames <- c("(row)", colnames) fields <- sprintf("x%d", seq_along(colnames)) data <- c(list(" " = rownames), .subset(data)) names(data) <- fields class(data) <- "data.frame" - attr(data, "row.names") <- .set_row_names(nrow) + attr(data, "row.names") <- .set_row_names(.nrow) columns <- .mapply(get_column_def, list(colnames, fields, data), NULL @@ -379,11 +380,25 @@ if (show_view) { } show_dataview <- function(x, title, uuid = NULL, - viewer = getOption("vsc.view", "Two")) { + viewer = getOption("vsc.view", "Two"), + row_limit = abs(getOption("vsc.row_limit", 0))) { + as_truncated_data <- function(.data) { + .nrow <- nrow(.data) + if (row_limit != 0 && row_limit < .nrow) { + title <<- sprintf("%s (limited to %d/%d)", title, row_limit, .nrow) + .data <- utils::head(.data, n = row_limit) + } + return(.data) + } + if (missing(title)) { sub <- substitute(x) title <- deparse(sub, nlines = 1) } + if (inherits(x, "ArrowTabular")) { + x <- as_truncated_data(x) + x <- as.data.frame(x) + } if (is.environment(x)) { all_names <- ls(x) is_promise <- rlang::env_binding_are_lazy(x, all_names) @@ -438,6 +453,7 @@ if (show_view) { } } if (is.data.frame(x) || is.matrix(x)) { + x <- as_truncated_data(x) data <- dataview_table(x) file <- tempfile(tmpdir = tempdir, fileext = ".json") jsonlite::write_json(data, file, na = "string", null = "null", auto_unbox = TRUE) diff --git a/package.json b/package.json index c7712aa3c..b89a8e2e2 100644 --- a/package.json +++ b/package.json @@ -1448,6 +1448,11 @@ "default": true, "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`." }, + "r.session.data.rowLimit": { + "type": "integer", + "default": 0, + "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`." + }, "r.session.viewers.viewColumn": { "type": "object", "markdownDescription": "Which view column should R-related webviews be displayed? Requires `#r.sessionWatcher#` to be set to `true`.",