Skip to content

Commit afd27ef

Browse files
committed
Capture viewer, page_viewer, and browser request
1 parent 5cac531 commit afd27ef

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

R/notebook.R

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ local({
1010

1111
null_dev_id <- c(pdf = 2L)
1212
null_dev_size <- c(7 + pi, 7 + pi)
13+
viewer_file <- NULL
14+
browser_url <- NULL
1315

1416
options(
1517
device = function(...) {
@@ -18,6 +20,18 @@ local({
1820
height = null_dev_size[[2L]],
1921
bg = "white")
2022
dev.control(displaylist = "enable")
23+
},
24+
viewer = function(url, ...) {
25+
message("viewer: ", url)
26+
viewer_file <<- url
27+
},
28+
page_viewer = function(url, ...) {
29+
message("page_viewer: ", url)
30+
viewer_file <<- url
31+
},
32+
browser = function(url, ...) {
33+
message("browser: ", url)
34+
browser_url <<- url
2135
}
2236
)
2337

@@ -38,10 +52,12 @@ local({
3852
line <- readLines(con, n = 1)
3953
request <- jsonlite::fromJSON(line)
4054
cat(sprintf("[%s]\n%s\n", request$time, request$expr))
55+
viewer_file <- NULL
56+
browser_url <- NULL
4157
str <- tryCatch({
4258
expr <- parse(text = request$expr)
4359
out <- withVisible(eval(expr, globalenv()))
44-
text <- utils::capture.output(print(out$value))
60+
text <- utils::capture.output(print(out$value, view = TRUE))
4561
if (check_null_dev()) {
4662
record <- recordPlot()
4763
plot_file <- tempfile(fileext = ".svg")
@@ -52,6 +68,16 @@ local({
5268
type = "plot",
5369
result = plot_file
5470
)
71+
} else if (!is.null(viewer_file)) {
72+
res <- list(
73+
type = "viewer",
74+
result = viewer_file
75+
)
76+
} else if (!is.null(browser_url)) {
77+
res <- list(
78+
type = "browser",
79+
result = browser_url
80+
)
5581
} else if (out$visible) {
5682
res <- list(
5783
type = "text",

src/notebook.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,22 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
320320
}
321321
}];
322322
break;
323+
case 'viewer':
324+
cell.outputs = [{
325+
outputKind: vscode.CellOutputKind.Rich,
326+
data: {
327+
'application/json': output,
328+
}
329+
}];
330+
break;
331+
case 'browser':
332+
cell.outputs = [{
333+
outputKind: vscode.CellOutputKind.Rich,
334+
data: {
335+
'application/json': output,
336+
}
337+
}];
338+
break;
323339
case 'error':
324340
throw new Error(output.result);
325341
}

0 commit comments

Comments
 (0)