@@ -16,22 +16,19 @@ r <- callr::r_session$new(
1616
1717r $ 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 ) {
0 commit comments