@@ -231,7 +231,17 @@ request <- function(command, ...) {
231
231
auto_unbox = TRUE , na = " string" , null = " null" , force = TRUE )
232
232
if (is.na(request_tcp_connection )) {
233
233
cat(get_timestamp(), file = request_lock_file )
234
+ } else {
235
+ response <- readLines(request_tcp_connection , n = 1 )
236
+ if (length(response ) == 0 ) {
237
+ # If the server ends up the connection
238
+ detach(including_request_detach = FALSE )
239
+ return (TRUE )
240
+ } else if (length(response ) != 1 && response != " req_finished" ) {
241
+ stop(paste(" Error in connection: Malform response: \n " , paste(response , collapse = " \n " ), " \n " ))
242
+ }
234
243
}
244
+ FALSE
235
245
}
236
246
237
247
try_catch_timeout <- function (expr , timeout = Inf , ... ) {
@@ -463,7 +473,14 @@ if (use_httpgd && "httpgd" %in% .packages(all.available = TRUE)) {
463
473
tryCatch({
464
474
plot_file_content <- readr :: read_file_raw(plot_file )
465
475
format <- " image/png" # right now only this format is supported
466
- request(" plot" , format = format , plot_base64 = jsonlite :: base64_enc(plot_file_content ))
476
+ if (request(" plot" , format = format , plot_base64 = jsonlite :: base64_enc(plot_file_content ))) {
477
+ cat(stderr(),
478
+ paste(
479
+ " The connection to VSCode was disconnected, and it was detected only now after plotting." ,
480
+ " Please run the plot task again."
481
+ )
482
+ )
483
+ }
467
484
}, error = message )
468
485
}
469
486
})
@@ -646,7 +663,7 @@ if (show_view) {
646
663
title = title , data = data , viewer = viewer , uuid = uuid )
647
664
}
648
665
}
649
- if (is.data.frame(x ) || is.matrix(x )) {
666
+ should_default_view <- if (is.data.frame(x ) || is.matrix(x )) {
650
667
x <- as_truncated_data(x )
651
668
data <- dataview_table(x )
652
669
send_data_request(data , source = " table" , type = " json" , fileext = " .json" )
@@ -665,6 +682,9 @@ if (show_view) {
665
682
}
666
683
send_data_request(code , source = " object" , type = " R" , paste0(make.names(title )), fileext = " .R" )
667
684
}
685
+ if (should_default_view ) {
686
+ old_view_impl(x , title )
687
+ }
668
688
}
669
689
670
690
View_impl <- show_dataview
@@ -722,12 +742,14 @@ attach <- function(host = "127.0.0.1", port = NA) {
722
742
request_is_attached <<- TRUE
723
743
}
724
744
725
- detach <- function () {
745
+ detach <- function (including_request_detach = TRUE ) {
746
+ if (including_request_detach ) {
726
747
request(" detach" )
727
- if (! is.na(request_tcp_connection )) {
728
- close(request_tcp_connection )
729
- request_tcp_connection <<- NA
730
- }
748
+ }
749
+ if (! is.na(request_tcp_connection )) {
750
+ close(request_tcp_connection )
751
+ request_tcp_connection <<- NA
752
+ }
731
753
if (request_is_attached ) {
732
754
# restore previous options
733
755
options_name <- names(options_when_connected_list )
@@ -795,7 +817,7 @@ show_browser <- function(url, title = url, ...,
795
817
request_browser(url = url , title = title , ... , viewer = FALSE )
796
818
} else {
797
819
path <- sub(" ^file\\ ://" , " " , url )
798
- if (file.exists(path )) {
820
+ should_default_browser <- if (file.exists(path )) {
799
821
path <- normalizePath(path , " /" , mustWork = TRUE )
800
822
if (grepl(" \\ .html?$" , path , ignore.case = TRUE )) {
801
823
message(
@@ -811,6 +833,10 @@ show_browser <- function(url, title = url, ...,
811
833
} else {
812
834
stop(" File not exists" )
813
835
}
836
+ if (should_default_browser ) {
837
+ browser <- before_attach_options [[" browser" ]]
838
+ utils :: browseURL(url , browser = browser )
839
+ }
814
840
}
815
841
}
816
842
@@ -868,7 +894,16 @@ show_viewer <- function(url, title = NULL, ...,
868
894
title <- deparse(expr , nlines = 1 )
869
895
}
870
896
}
871
- show_webview(url = url , title = title , ... , viewer = viewer )
897
+ should_default_viewer <- show_webview(url = url , title = title , ... , viewer = viewer )
898
+ if (should_default_viewer ) {
899
+ # Reference: https://rstudio.github.io/rstudio-extensions/rstudio_viewer.html
900
+ viewer <- before_attach_options [[" viewer" ]]
901
+ if (! is.null(viewer )) {
902
+ viewer(url )
903
+ } else {
904
+ utils :: browseURL(url )
905
+ }
906
+ }
872
907
}
873
908
874
909
show_page_viewer <- function (url , title = NULL , ... ,
@@ -881,7 +916,16 @@ show_page_viewer <- function(url, title = NULL, ...,
881
916
title <- deparse(expr , nlines = 1 )
882
917
}
883
918
}
884
- show_webview(url = url , title = title , ... , viewer = viewer )
919
+ should_default_page_viewer <- show_webview(url = url , title = title , ... , viewer = viewer )
920
+ if (should_default_page_viewer ) {
921
+ # Reference: https://rstudio.github.io/rstudio-extensions/rstudio_viewer.html
922
+ page_viewer <- before_attach_options [[" page_viewer" ]]
923
+ if (! is.null(page_viewer )) {
924
+ page_viewer(url )
925
+ } else {
926
+ utils :: browseURL(url )
927
+ }
928
+ }
885
929
}
886
930
887
931
options_when_connected(
@@ -969,7 +1013,9 @@ print.help_files_with_topic <- function(h, ...) {
969
1013
basename(file ),
970
1014
" .html"
971
1015
)
972
- request(command = " help" , requestPath = requestPath , viewer = viewer )
1016
+ if (request(command = " help" , requestPath = requestPath , viewer = viewer )) {
1017
+ utils ::: print.help_files_with_topic(h , ... )
1018
+ }
973
1019
} else {
974
1020
utils ::: print.help_files_with_topic(h , ... )
975
1021
}
@@ -1008,7 +1054,9 @@ print.hsearch <- function(x, ...) {
1008
1054
)
1009
1055
}
1010
1056
)
1011
- request(command = " help" , requestPath = requestPath , viewer = viewer )
1057
+ if (request(command = " help" , requestPath = requestPath , viewer = viewer )) {
1058
+ utils ::: print.hsearch(x , ... )
1059
+ }
1012
1060
} else {
1013
1061
utils ::: print.hsearch(x , ... )
1014
1062
}
@@ -1026,8 +1074,6 @@ print.hsearch <- function(x, ...) {
1026
1074
}
1027
1075
1028
1076
reg.finalizer(.GlobalEnv , function (e ) {
1029
- # TODO: When exiting radian by EOF("CTRL+D") when coonecting to vsc by TCP,
1030
- # the TCP connection is getting closed before we're able to call detach...
1031
1077
tryCatch({
1032
1078
detach()
1033
1079
}, error = function (e ) NULL )
0 commit comments