@@ -9,7 +9,24 @@ request_file <- file.path(dir_watcher, "request.log")
9
9
request_lock_file <- file.path(dir_watcher , " request.lock" )
10
10
settings_file <- file.path(dir_watcher , " settings.json" )
11
11
request_tcp_connection <- NA
12
+ request_is_attached <- FALSE
13
+ before_attach_options <- list ()
14
+ options_when_connected_list <- list ()
15
+ options_when_connected <- function (... ) {
16
+ l <- list (... )
17
+ mapply(function (option , value ) {
18
+ options_when_connected_list [[option ]] <<- value
19
+ }, names(l ), l )
20
+ }
21
+ before_attach_hooks <- list ()
22
+ hooks_when_connected_list <- list ()
23
+ hook_when_connected <- function (hook , cb ) {
24
+ hooks_when_connected_list [[hook ]] <<- cb
25
+ }
12
26
user_options <- names(options())
27
+ created_devices <- c()
28
+ View_impl <- NULL
29
+ old_view_impl <- View
13
30
14
31
logger <- if (getOption(" vsc.debug" , FALSE )) {
15
32
function (... ) cat(... , " \n " , sep = " " )
@@ -64,7 +81,7 @@ load_settings <- function() {
64
81
load_settings()
65
82
66
83
if (is.null(getOption(" help_type" ))) {
67
- options (help_type = " html" )
84
+ options_when_connected (help_type = " html" )
68
85
}
69
86
70
87
use_webserver <- isTRUE(getOption(" vsc.use_webserver" , FALSE ))
@@ -389,10 +406,11 @@ removeTaskCallback("vsc.plot")
389
406
use_httpgd <- identical(getOption(" vsc.use_httpgd" , FALSE ), TRUE )
390
407
show_plot <- ! identical(getOption(" vsc.plot" , " Two" ), FALSE )
391
408
if (use_httpgd && " httpgd" %in% .packages(all.available = TRUE )) {
392
- options (device = function (... ) {
409
+ options_when_connected (device = function (... ) {
393
410
httpgd :: hgd(
394
411
silent = TRUE
395
412
)
413
+ created_devices <<- append(created_devices , dev.cur())
396
414
.vsc $ request(" httpgd" , url = httpgd :: hgd_url())
397
415
})
398
416
} else if (use_httpgd ) {
@@ -417,12 +435,13 @@ if (use_httpgd && "httpgd" %in% .packages(all.available = TRUE)) {
417
435
}
418
436
}
419
437
420
- options (
438
+ options_when_connected (
421
439
device = function (... ) {
422
440
pdf(NULL ,
423
441
width = null_dev_size [[1L ]],
424
442
height = null_dev_size [[2L ]],
425
443
bg = " white" )
444
+ created_devices <<- append(created_devices , dev.cur())
426
445
dev.control(displaylist = " enable" )
427
446
}
428
447
)
@@ -436,7 +455,9 @@ if (use_httpgd && "httpgd" %in% .packages(all.available = TRUE)) {
436
455
dev_args <- getOption(" vsc.dev.args" )
437
456
do.call(png , c(list (filename = plot_file ), dev_args ))
438
457
on.exit({
458
+ cur_dev <- dev.cur()
439
459
dev.off()
460
+ created_devices <<- created_devices [created_devices != cur_dev ]
440
461
cat(get_timestamp(), file = plot_lock_file )
441
462
if (! is.na(request_tcp_connection )) {
442
463
tryCatch({
@@ -453,12 +474,12 @@ if (use_httpgd && "httpgd" %in% .packages(all.available = TRUE)) {
453
474
TRUE
454
475
}
455
476
456
- setHook (" plot.new" , new_plot , " replace " )
457
- setHook (" grid.newpage" , new_plot , " replace " )
477
+ hook_when_connected (" plot.new" , new_plot )
478
+ hook_when_connected (" grid.newpage" , new_plot )
458
479
459
480
rebind(" .External.graphics" , function (... ) {
460
481
out <- .Primitive(" .External.graphics" )(... )
461
- if (check_null_dev()) {
482
+ if (request_is_attached && check_null_dev()) {
462
483
plot_updated <<- TRUE
463
484
}
464
485
out
@@ -646,17 +667,17 @@ if (show_view) {
646
667
}
647
668
}
648
669
649
- rebind( " View " , show_dataview , " utils " )
670
+ View_impl <- show_dataview
650
671
}
651
672
652
673
attach <- function (host = " 127.0.0.1" , port = NA ) {
674
+ if (request_is_attached ) {
675
+ detach()
676
+ }
653
677
load_settings()
654
678
if (rstudioapi_enabled()) {
655
679
rstudioapi_util_env $ update_addin_registry(addin_registry )
656
680
}
657
- if (! is.na(request_tcp_connection )) {
658
- detach()
659
- }
660
681
if (! is.na(port )) {
661
682
request_tcp_connection <<- socketConnection(
662
683
host = host ,
@@ -682,6 +703,23 @@ attach <- function(host = "127.0.0.1", port = NA) {
682
703
token = parent $ token
683
704
) else NULL
684
705
)
706
+ if (! request_is_attached ) {
707
+ options_name <- names(options_when_connected_list )
708
+ before_attach_options <<- setNames(lapply(options_name , function (option ) getOption(option )), options_name )
709
+
710
+ hooks_name <- names(hooks_when_connected_list )
711
+ before_attach_hooks <<- setNames(lapply(hooks_name , function (hook_name ) getHook(hook_name )), hooks_name )
712
+
713
+ old_view_impl <<- View
714
+ if (! is.null(View_impl )) {
715
+ rebind(" View" , View_impl , " utils" )
716
+ }
717
+ }
718
+ do.call(options , options_when_connected_list )
719
+ mapply(function (hook_name , cb ) {
720
+ setHook(hook_name , cb , " replace" )
721
+ }, hooks_name , hooks_when_connected_list )
722
+ request_is_attached <<- TRUE
685
723
}
686
724
687
725
detach <- function () {
@@ -690,6 +728,25 @@ detach <- function() {
690
728
close(request_tcp_connection )
691
729
request_tcp_connection <<- NA
692
730
}
731
+ if (request_is_attached ) {
732
+ # restore previous options
733
+ options_name <- names(options_when_connected_list )
734
+ do.call(options , setNames(before_attach_options [options_name ], options_name ))
735
+
736
+ # restore previous hooks
737
+ hooks_name <- names(hooks_when_connected_list )
738
+ mapply(function (hook_name , cbs ) {
739
+ setHook(hook_name , cbs , " replace" )
740
+ }, hooks_name , before_attach_hooks [hooks_name ])
741
+
742
+ if (! is.null(View_impl )) {
743
+ rebind(" View" , old_view_impl , " utils" )
744
+ }
745
+
746
+ lapply(created_devices , function (dev ) dev.off(dev ))
747
+ created_devices <<- c()
748
+ }
749
+ request_is_attached <<- FALSE
693
750
}
694
751
695
752
path_to_uri <- function (path ) {
@@ -827,7 +884,7 @@ show_page_viewer <- function(url, title = NULL, ...,
827
884
show_webview(url = url , title = title , ... , viewer = viewer )
828
885
}
829
886
830
- options (
887
+ options_when_connected (
831
888
browser = show_browser ,
832
889
viewer = show_viewer ,
833
890
page_viewer = show_page_viewer
@@ -882,7 +939,7 @@ if (rstudioapi_enabled()) {
882
939
rstudioapi_env <- new.env(parent = rstudioapi_util_env )
883
940
source(file.path(dir_init , " rstudioapi_util.R" ), local = rstudioapi_util_env )
884
941
source(file.path(dir_init , " rstudioapi.R" ), local = rstudioapi_env )
885
- setHook (
942
+ hook_when_connected (
886
943
packageEvent(" rstudioapi" , " onLoad" ),
887
944
function (... ) {
888
945
rstudioapi_util_env $ rstudioapi_patch_hook(rstudioapi_env )
@@ -900,7 +957,7 @@ if (rstudioapi_enabled()) {
900
957
901
958
print.help_files_with_topic <- function (h , ... ) {
902
959
viewer <- getOption(" vsc.helpPanel" , " Two" )
903
- if (! identical(FALSE , viewer ) && length(h ) > = 1 && is.character(h )) {
960
+ if (request_is_attached && ! identical(FALSE , viewer ) && length(h ) > = 1 && is.character(h )) {
904
961
file <- h [1 ]
905
962
path <- dirname(file )
906
963
dirpath <- dirname(path )
@@ -921,7 +978,7 @@ print.help_files_with_topic <- function(h, ...) {
921
978
922
979
print.hsearch <- function (x , ... ) {
923
980
viewer <- getOption(" vsc.helpPanel" , " Two" )
924
- if (! identical(FALSE , viewer ) && length(x ) > = 1 ) {
981
+ if (request_is_attached && ! identical(FALSE , viewer ) && length(x ) > = 1 ) {
925
982
requestPath <- paste0(
926
983
" /doc/html/Search?pattern=" ,
927
984
tools ::: escapeAmpersand(x $ pattern ),
0 commit comments