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 3 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
29 changes: 23 additions & 6 deletions R/session/vsc.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ 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.show_arrow_table = session$data$showArrowTable,
vsc.view = setting(session$viewers$viewColumn$view, Disable = FALSE),
vsc.helpPanel = setting(session$viewers$viewColumn$helpPanel, Disable = FALSE)
))
Expand Down Expand Up @@ -340,13 +342,12 @@ if (show_view) {
)
}

dataview_table <- function(data) {
dataview_table <- function(data, .nrow) {
if (is.matrix(data)) {
data <- as.data.frame.matrix(data)
}

if (is.data.frame(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,21 @@ 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))) {
if (missing(title)) {
sub <- substitute(x)
title <- deparse(sub, nlines = 1)
}
if (getOption("vsc.show_arrow_table", FALSE) && inherits(x, "ArrowTabular")) {
.nrow <- nrow(x)
if (row_limit != 0 && row_limit < .nrow) {
title <- sprintf("%s (Limited to %s rows)", title, row_limit)
eitsupi marked this conversation as resolved.
Show resolved Hide resolved
.nrow <- row_limit
x <- utils::head(x, n = .nrow)
}
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,7 +449,13 @@ if (show_view) {
}
}
if (is.data.frame(x) || is.matrix(x)) {
data <- dataview_table(x)
.nrow <- nrow(x)
if (row_limit != 0 && row_limit < .nrow) {
title <- sprintf("%s (Limited to %s rows)", title, row_limit)
eitsupi marked this conversation as resolved.
Show resolved Hide resolved
.nrow <- row_limit
x <- utils::head(x, n = .nrow)
}
data <- dataview_table(x, .nrow)
file <- tempfile(tmpdir = tempdir, fileext = ".json")
jsonlite::write_json(data, file, na = "string", null = "null", auto_unbox = TRUE)
request("dataview", source = "table", type = "json",
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,16 @@
"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.data.showArrowTable": {
eitsupi marked this conversation as resolved.
Show resolved Hide resolved
"type": "boolean",
"default": false,
"markdownDescription": "Show a Apache Arrow table as a data frame in the data viewer. Convert a `ArrowTabular` class object to a `data.frame` and then display it. Changes the option `vsc.show_arrow_table` 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