@@ -16,22 +16,19 @@ r <- callr::r_session$new(
16
16
17
17
r $ run(function () {
18
18
requireNamespace(" jsonlite" )
19
- requireNamespace(" svglite" )
20
19
21
20
.vscNotebook <- local({
22
- null_dev_id <- c(pdf = 2L )
23
- null_dev_size <- c(7 + pi , 7 + pi )
24
21
viewer_file <- NULL
25
22
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 )
26
30
27
31
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
- },
35
32
viewer = function (url , ... ) {
36
33
viewer_file <<- url
37
34
},
@@ -43,27 +40,31 @@ r$run(function() {
43
40
}
44
41
)
45
42
46
- check_null_dev <- function () {
47
- identical(dev.cur(), null_dev_id ) &&
48
- identical(dev.size(), null_dev_size )
49
- }
50
-
51
43
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
+
52
49
viewer_file <<- NULL
53
50
browser_url <<- NULL
51
+
54
52
res <- tryCatch({
55
53
expr <- parse(text = expr )
56
54
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
+
63
64
list (
64
65
id = id ,
65
66
type = " plot" ,
66
- result = plot_file
67
+ result = list.files( plot_dir , pattern = " .* \\ .svg " , full.names = T )
67
68
)
68
69
} else if (! is.null(viewer_file )) {
69
70
list (
@@ -80,20 +81,20 @@ r$run(function() {
80
81
} else if (out $ visible ) {
81
82
if (inherits(out $ value , " data.frame" )) {
82
83
text <- utils :: capture.output(knitr :: kable(out $ value , format = " html" ))
83
- } else {
84
- text <- utils :: capture.output(print(out $ value , view = TRUE ))
85
84
}
86
85
87
86
list (
88
87
id = id ,
89
88
type = " text" ,
90
- result = paste0(text , collapse = " \n " )
89
+ result = paste0(text , collapse = " \n " ),
90
+ tempdir = plot_dir
91
91
)
92
92
} else {
93
93
list (
94
94
id = id ,
95
95
type = " text" ,
96
- result = " "
96
+ result = " " ,
97
+ tempdir = plot_dir
97
98
)
98
99
}
99
100
}, error = function (e ) {
0 commit comments