Repository navigation
Run rstudioapi::jobRunScript() in a new background console - #1460
Merged
Merged
Conversation
juliasilge
marked this pull request as ready for review
November 2, 2026 00:37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
rstudioapi::jobRunScript()in a new background console positron#17042rstudioapi::jobRunScript()now works in Positron. ark asks Positron to start a new R console session in the background, and that session runs the script withsource(). The caller's console stays free while the script runs. For example,targets::tar_make(as_job = TRUE)now runs the pipeline in its own console.Before this change, the call failed with "Function runScriptJob not found in RStudio", because ark did not define
.rs.api.runScriptJob().Changes
execute_code_in_new_sessionfrontend method to the UI comm.crates/amalthea/src/comm/ui_comm.rsis generated from the Positron contract..ps.ui.executeCodeInNewSession(code, sessionName). It parsescodefirst. If the code does not parse, it stops with an error in the caller's session, and no session starts. It returns the ID of the new session..rs.api.runScriptJob()with the same arguments as in RStudio, so that thecallFun()of rstudioapi accepts it:path,name,encoding, andworkingDirmust each be a single string.nameisNULLor empty, the session name is the file name. IfworkingDirisNULL, the working directory is the folder of the script. The working directory must exist.importEnv = TRUEand a non-emptyexportEnvare not supported. Each one gives a warning, and the job still runs.The new session runs this code:
{ setwd("/path/to/working/dir") source("/path/to/script.R", encoding = "unknown") }ark makes each value with
deparse(), so quotes and backslashes in paths are safe. The braces make one expression. Positron can split console input into statements and continue after an error. Without the braces, a failedsetwd()would letsource()run in the wrong folder.Why not
setWorkingDirectory()I originally wanted to use
ILanguageRuntimeSession.setWorkingDirectory()from Positron, but it is not a good fit here:setwd()directly to the kernel, outside the console queue. It does not wait for the result, and it continues after an error. The order ofsetwd()andsource()running would then depend on timing.\and"whiledeparse()escapes all special characters..Rprofileand renv still use the workspace folder. A real fix needs a working directory at session start, and Positron does not have this option yet.Known limitations
.Rprofileor renv. ark needs the reply to return the session ID and to report start errors..ps.ui.*functions need an active execute request, so a call from alater::later()callback fails. This limit is the same for all.ps.ui.*functions currently.jobAdd(),jobGetState(), and more) are not part of this change.Testing
crates/ark/tests/integration/rstudioapi.rscover the request that ark sends (with a path that contains a space and a quote),nameandworkingDir, relative paths, a missing file or folder, bad argument types, the warnings, and code that does not parse. Tests that callrstudioapi::jobRunScript()skip when rstudioapi is not installed. Tests that call.rs.api.runScriptJob()directly always run.cargo nextest run -p ark --test integration -E 'test(rstudioapi::)'. They need nextest, because each test needs its own R session.\\?\prefix thatcanonicalize()adds on Windows. I did not run the tests on Windows, so so we'll want to especially check the Windows CI result.foo.RwithSys.sleep(10); print(getwd()).rstudioapi::jobRunScript("foo.R").foo.Rshows in the session list, and the original console stays selected. You can type in the original console while the script runs, and the new console prints the folder of the script.Positron Release Notes
New Features
rstudioapi::jobRunScript()now runs the script in a new R console in the background, so the current console stays free. As a result,targets::tar_make(as_job = TRUE)now works (Multisession console: add support to programmatically launch new console sessions positron#6720).Bug Fixes