@@ -231,7 +231,17 @@ request <- function(command, ...) {
231231 auto_unbox = TRUE , na = " string" , null = " null" , force = TRUE )
232232 if (is.na(request_tcp_connection )) {
233233 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+ }
234243 }
244+ FALSE
235245}
236246
237247try_catch_timeout <- function (expr , timeout = Inf , ... ) {
@@ -463,7 +473,14 @@ if (use_httpgd && "httpgd" %in% .packages(all.available = TRUE)) {
463473 tryCatch({
464474 plot_file_content <- readr :: read_file_raw(plot_file )
465475 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+ }
467484 }, error = message )
468485 }
469486 })
@@ -646,7 +663,7 @@ if (show_view) {
646663 title = title , data = data , viewer = viewer , uuid = uuid )
647664 }
648665 }
649- if (is.data.frame(x ) || is.matrix(x )) {
666+ should_default_view <- if (is.data.frame(x ) || is.matrix(x )) {
650667 x <- as_truncated_data(x )
651668 data <- dataview_table(x )
652669 send_data_request(data , source = " table" , type = " json" , fileext = " .json" )
@@ -665,6 +682,9 @@ if (show_view) {
665682 }
666683 send_data_request(code , source = " object" , type = " R" , paste0(make.names(title )), fileext = " .R" )
667684 }
685+ if (should_default_view ) {
686+ old_view_impl(x , title )
687+ }
668688 }
669689
670690 View_impl <- show_dataview
@@ -722,12 +742,14 @@ attach <- function(host = "127.0.0.1", port = NA) {
722742 request_is_attached <<- TRUE
723743}
724744
725- detach <- function () {
745+ detach <- function (including_request_detach = TRUE ) {
746+ if (including_request_detach ) {
726747 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+ }
731753 if (request_is_attached ) {
732754 # restore previous options
733755 options_name <- names(options_when_connected_list )
@@ -795,7 +817,7 @@ show_browser <- function(url, title = url, ...,
795817 request_browser(url = url , title = title , ... , viewer = FALSE )
796818 } else {
797819 path <- sub(" ^file\\ ://" , " " , url )
798- if (file.exists(path )) {
820+ should_default_browser <- if (file.exists(path )) {
799821 path <- normalizePath(path , " /" , mustWork = TRUE )
800822 if (grepl(" \\ .html?$" , path , ignore.case = TRUE )) {
801823 message(
@@ -811,6 +833,10 @@ show_browser <- function(url, title = url, ...,
811833 } else {
812834 stop(" File not exists" )
813835 }
836+ if (should_default_browser ) {
837+ browser <- before_attach_options [[" browser" ]]
838+ utils :: browseURL(url , browser = browser )
839+ }
814840 }
815841}
816842
@@ -868,7 +894,16 @@ show_viewer <- function(url, title = NULL, ...,
868894 title <- deparse(expr , nlines = 1 )
869895 }
870896 }
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+ }
872907}
873908
874909show_page_viewer <- function (url , title = NULL , ... ,
@@ -881,7 +916,16 @@ show_page_viewer <- function(url, title = NULL, ...,
881916 title <- deparse(expr , nlines = 1 )
882917 }
883918 }
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+ }
885929}
886930
887931options_when_connected(
@@ -969,7 +1013,9 @@ print.help_files_with_topic <- function(h, ...) {
9691013 basename(file ),
9701014 " .html"
9711015 )
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+ }
9731019 } else {
9741020 utils ::: print.help_files_with_topic(h , ... )
9751021 }
@@ -1008,7 +1054,9 @@ print.hsearch <- function(x, ...) {
10081054 )
10091055 }
10101056 )
1011- request(command = " help" , requestPath = requestPath , viewer = viewer )
1057+ if (request(command = " help" , requestPath = requestPath , viewer = viewer )) {
1058+ utils ::: print.hsearch(x , ... )
1059+ }
10121060 } else {
10131061 utils ::: print.hsearch(x , ... )
10141062 }
@@ -1026,8 +1074,6 @@ print.hsearch <- function(x, ...) {
10261074}
10271075
10281076reg.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...
10311077 tryCatch({
10321078 detach()
10331079 }, error = function (e ) NULL )
0 commit comments