Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add row limit setting of data viewer and support Apache Arrow Table #945

Merged
merged 6 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions R/session/vsc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
))
Expand Down Expand Up @@ -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)))
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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`.",
Expand Down