@@ -8,6 +8,7 @@ dir_watcher <- Sys.getenv("VSCODE_WATCHER_DIR", file.path(homedir, ".vscode-R"))
88request_file <- file.path(dir_watcher , " request.log" )
99request_lock_file <- file.path(dir_watcher , " request.lock" )
1010settings_file <- file.path(dir_watcher , " settings.json" )
11+ request_tcp_connection <- NA
1112user_options <- names(options())
1213
1314logger <- if (getOption(" vsc.debug" , FALSE )) {
@@ -208,9 +209,12 @@ request <- function(command, ...) {
208209 command = command ,
209210 ...
210211 )
211- jsonlite :: write_json(obj , request_file ,
212- auto_unbox = TRUE , null = " null" , force = TRUE )
213- cat(get_timestamp(), file = request_lock_file )
212+ request_target <- if (is.na(request_tcp_connection )) request_file else request_tcp_connection
213+ jsonlite :: write_json(obj , request_target ,
214+ auto_unbox = TRUE , na = " string" , null = " null" , force = TRUE )
215+ if (is.na(request_tcp_connection )) {
216+ cat(get_timestamp(), file = request_lock_file )
217+ }
214218}
215219
216220try_catch_timeout <- function (expr , timeout = Inf , ... ) {
@@ -369,8 +373,12 @@ update_workspace <- function(...) {
369373 loaded_namespaces = loadedNamespaces(),
370374 globalenv = if (show_globalenv ) inspect_env(.GlobalEnv , globalenv_cache ) else NULL
371375 )
372- jsonlite :: write_json(data , workspace_file , force = TRUE , pretty = FALSE )
373- cat(get_timestamp(), file = workspace_lock_file )
376+ if (is.na(request_tcp_connection )) {
377+ jsonlite :: write_json(data , workspace_file , force = TRUE , pretty = FALSE )
378+ cat(get_timestamp(), file = workspace_lock_file )
379+ } else {
380+ request(" updateWorkspace" , workspaceData = data )
381+ }
374382 }, error = message )
375383 TRUE
376384}
@@ -419,6 +427,7 @@ if (use_httpgd && "httpgd" %in% .packages(all.available = TRUE)) {
419427 }
420428 )
421429
430+ # TODO: Make this be available by TCP only as well
422431 update_plot <- function (... ) {
423432 tryCatch({
424433 if (plot_updated && check_null_dev()) {
@@ -595,47 +604,64 @@ if (show_view) {
595604 )
596605 }
597606 }
607+ send_data_request <- function (data , source , type , ... ) {
608+ if (is.na(request_tcp_connection )) {
609+ file <- tempfile(tmpdir = tempdir , ... )
610+ if (type == " json" ) {
611+ jsonlite :: write_json(data , file , na = " string" , null = " null" , auto_unbox = TRUE , force = TRUE )
612+ } else {
613+ writeLines(code , file )
614+ }
615+ request(" dataview" , source = source , type = type ,
616+ title = title , file = file , viewer = viewer , uuid = uuid )
617+ } else {
618+ request(" dataview" , source = source , type = type ,
619+ title = title , data = data , viewer = viewer , uuid = uuid )
620+ }
621+ }
598622 if (is.data.frame(x ) || is.matrix(x )) {
599623 x <- as_truncated_data(x )
600624 data <- dataview_table(x )
601- file <- tempfile(tmpdir = tempdir , fileext = " .json" )
602- jsonlite :: write_json(data , file , na = " string" , null = " null" , auto_unbox = TRUE , force = TRUE )
603- request(" dataview" , source = " table" , type = " json" ,
604- title = title , file = file , viewer = viewer , uuid = uuid )
625+ send_data_request(data , source = " table" , type = " json" , fileext = " .json" )
605626 } else if (is.list(x )) {
606627 tryCatch({
607- file <- tempfile(tmpdir = tempdir , fileext = " .json" )
608- jsonlite :: write_json(x , file , na = " string" , null = " null" , auto_unbox = TRUE , force = TRUE )
609- request(" dataview" , source = " list" , type = " json" ,
610- title = title , file = file , viewer = viewer , uuid = uuid )
628+ send_data_request(x , source = " list" , type = " json" , fileext = " .json" )
611629 }, error = function (e ) {
612- file <- file.path(tempdir , paste0(make.names(title ), " .txt" ))
613630 text <- utils :: capture.output(print(x ))
614- writeLines(text , file )
615- request(" dataview" , source = " object" , type = " txt" ,
616- title = title , file = file , viewer = viewer , uuid = uuid )
631+ send_data_request(text , source = " object" , type = " txt" , paste0(make.names(title )), fileext = " .txt" )
617632 })
618633 } else {
619- file <- file.path(tempdir , paste0(make.names(title ), " .R" ))
620634 if (is.primitive(x )) {
621635 code <- utils :: capture.output(print(x ))
622636 } else {
623637 code <- deparse(x )
624638 }
625- writeLines(code , file )
626- request(" dataview" , source = " object" , type = " R" ,
627- title = title , file = file , viewer = viewer , uuid = uuid )
639+ send_data_request(code , source = " object" , type = " R" , paste0(make.names(title )), fileext = " .R" )
628640 }
629641 }
630642
631643 rebind(" View" , show_dataview , " utils" )
632644}
633645
634- attach <- function () {
646+ attach <- function (host = " 127.0.0.1 " , port = NA ) {
635647 load_settings()
636648 if (rstudioapi_enabled()) {
637649 rstudioapi_util_env $ update_addin_registry(addin_registry )
638650 }
651+ if (! is.na(request_tcp_connection )) {
652+ close(request_tcp_connection )
653+ request_tcp_connection <<- NA
654+ }
655+ if (! is.na(port )) {
656+ request_tcp_connection <<- socketConnection(
657+ host = host ,
658+ port = port ,
659+ blocking = TRUE ,
660+ server = FALSE ,
661+ open = " a+"
662+ )
663+ }
664+ parent <- parent.env(environment())
639665 request(" attach" ,
640666 version = sprintf(" %s.%s" , R.version $ major , R.version $ minor ),
641667 tempdir = tempdir ,
@@ -646,9 +672,9 @@ attach <- function() {
646672 ),
647673 plot_url = if (identical(names(dev.cur()), " httpgd" )) httpgd :: hgd_url(),
648674 server = if (use_webserver ) list (
649- host = host ,
650- port = port ,
651- token = token
675+ host = parent $ host ,
676+ port = parent $ port ,
677+ token = parent $ token
652678 ) else NULL
653679 )
654680}
@@ -929,4 +955,9 @@ print.hsearch <- function(x, ...) {
929955 invisible (NULL )
930956}
931957
932- reg.finalizer(.GlobalEnv , function (e ) .vsc $ request(" detach" ), onexit = TRUE )
958+ reg.finalizer(.GlobalEnv , function (e ) {
959+ .vsc $ request(" detach" )
960+ if (! is.na(request_tcp_connection )) {
961+ close(request_tcp_connection )
962+ }
963+ }, onexit = TRUE )
0 commit comments