Skip to content

Commit 1c1fa99

Browse files
committed
fixes plots not being displayed and adding cleaner solution for plots.
1 parent 4b4ce60 commit 1c1fa99

File tree

4 files changed

+62
-26
lines changed

4 files changed

+62
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ tmp.*
1414
temp.*
1515
tmp
1616
temp
17+
.Rhistory

R/notebook.R

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,19 @@ r <- callr::r_session$new(
1616

1717
r$run(function() {
1818
requireNamespace("jsonlite")
19-
requireNamespace("svglite")
2019

2120
.vscNotebook <- local({
22-
null_dev_id <- c(pdf = 2L)
23-
null_dev_size <- c(7 + pi, 7 + pi)
2421
viewer_file <- NULL
2522
browser_url <- NULL
23+
plot.new.called <- F
24+
25+
set_plot_new <- function() {
26+
plot.new.called <<- T
27+
}
28+
setHook("before.plot.new", set_plot_new)
29+
setHook("before.grid.newpage", set_plot_new)
2630

2731
options(
28-
device = function(...) {
29-
pdf(NULL,
30-
width = null_dev_size[[1L]],
31-
height = null_dev_size[[2L]],
32-
bg = "white")
33-
dev.control(displaylist = "enable")
34-
},
3532
viewer = function(url, ...) {
3633
viewer_file <<- url
3734
},
@@ -43,27 +40,31 @@ r$run(function() {
4340
}
4441
)
4542

46-
check_null_dev <- function() {
47-
identical(dev.cur(), null_dev_id) &&
48-
identical(dev.size(), null_dev_size)
49-
}
50-
5143
evaluate <- function(id, expr) {
44+
plot_dir <- tempdir()
45+
plot_file <- file.path(plot_dir, "plot%03d.svg")
46+
47+
svg(plot_file, width = 12, height = 8)
48+
5249
viewer_file <<- NULL
5350
browser_url <<- NULL
51+
5452
res <- tryCatch({
5553
expr <- parse(text = expr)
5654
out <- withVisible(eval(expr, globalenv()))
57-
if (check_null_dev()) {
58-
record <- recordPlot()
59-
plot_file <- tempfile(fileext = ".svg")
60-
svglite::svglite(plot_file, width = 12, height = 8)
61-
replayPlot(record)
62-
graphics.off()
55+
56+
text <- utils::capture.output(print(out$value, view = TRUE))
57+
58+
dev.off()
59+
graphics.off()
60+
61+
if (plot.new.called) {
62+
plot.new.called <<- F
63+
6364
list(
6465
id = id,
6566
type = "plot",
66-
result = plot_file
67+
result = list.files(plot_dir, pattern = ".*\\.svg", full.names = T)
6768
)
6869
} else if (!is.null(viewer_file)) {
6970
list(
@@ -80,20 +81,20 @@ r$run(function() {
8081
} else if (out$visible) {
8182
if (inherits(out$value, "data.frame")) {
8283
text <- utils::capture.output(knitr::kable(out$value, format = "html"))
83-
} else {
84-
text <- utils::capture.output(print(out$value, view = TRUE))
8584
}
8685

8786
list(
8887
id = id,
8988
type = "text",
90-
result = paste0(text, collapse = "\n")
89+
result = paste0(text, collapse = "\n"),
90+
tempdir = plot_dir
9191
)
9292
} else {
9393
list(
9494
id = id,
9595
type = "text",
96-
result = ""
96+
result = "",
97+
tempdir = plot_dir
9798
)
9899
}
99100
}, error = function(e) {

samples/test1.rmd

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Somehow we need to render a external index.html
2+
```{r}
3+
library(tibble)
4+
5+
df = data.frame(a = c(1,2,3,4), b = c(10, 20, 13, 1))
6+
7+
8+
library(ggplot2)
9+
library(plotly)
10+
11+
12+
```
13+
```{r}
14+
ggplot(df, aes(a, b)) + geom_point()
15+
16+
```
17+
```{r}
18+
plot(df)
19+
20+
```
21+
Something goes wrong here internally with paged_table
22+
```{r}
23+
rmarkdown::paged_table(df)
24+
```
25+
```{r}
26+
knitr::kable(df, format = "html")
27+
```
28+
```{r}
29+
df
30+
```

src/notebook.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
188188
);
189189
}
190190

191+
lookupNotebook(uri: vscode.Uri): RNotebook {
192+
return this.notebooks.get(uri.toString())
193+
}
194+
191195
async openNotebook(uri: vscode.Uri): Promise<vscode.NotebookData> {
192196
const content = (await vscode.workspace.fs.readFile(uri)).toString();
193197
const lines = content.split(/\r?\n/);

0 commit comments

Comments
 (0)