This is a set of wrapper scripts and a configuration guide to allow you to have a persistent R session on a server when using the Visual Studio Code R Extension.
Current caveats:
- Only one R session
- (workaround: use vscode workspace settings, and keep a copy of
R-vscode-remote.sh
in the project workspace, configured with a particularR_BINARY
and uniqueR_TMUX_SESSION_NAME
)
- (workaround: use vscode workspace settings, and keep a copy of
- R version chosen via an environment variable (
R_BINARY
) - No
radian
(seem to have issues)
Copy R-vscode-remote.sh
and R-kill-vscode-remote.sh
to your remote machine (I put them in ~/bin
, on PATH
).
Ensure they are executable (chmod +x R-vscode-remote.sh R-kill-vscode-remote.sh
).
mkdir -p ~/bin
wget -O ~/bin/R-vscode-remote.sh https://gist.githubusercontent.com/pansapiens/088f599be11c6f3c63a91c051a7b2fea/raw/e7e33bb657770863b81f73b76c7e0db3190ad657/R-vscode-remote.sh
wget -O ~/bin/R-kill-vscode-remote.sh https://gist.githubusercontent.com/pansapiens/088f599be11c6f3c63a91c051a7b2fea/raw/e7e33bb657770863b81f73b76c7e0db3190ad657/R-kill-vscode-remote.sh
chmod +X R-vscode-remote.sh R-kill-vscode-remote.sh
# You may wish to add this to ~/.bashrc
#export PATH=$PATH:$HOME/bin
To choose an R version, edit R_BINARY
near the top of R-vscode-remote.sh
(or set the R_BINARY
in your ~/.bashrc
).
Install some support packages in R (eg in your renv
):
renv::install(c("languageserver", "httpgd", "ManuelHentschel/vscDebugger"))
Or if using a conda environment with associated R packages:
conda install -y r-httpuv r-languageserver
# compiles with conda's own gcc compiler
Rscript -e 'install.packages("vscDebugger", repos = "https://manuelhentschel.r-universe.dev")'
# or if you trust a random 3rd-party R package ...
# conda install -y tttpob::r-vscdebugger
To your .RProfile
, add:
# ~/.RProfile, or a project specific renv .RProfile
# Add this below any renv and workflowr initialization code
Sys.setenv(TERM_PROGRAM="vscode")
if (interactive() && Sys.getenv("RSTUDIO") == "") {
source(file.path(Sys.getenv(if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"), ".vscode-R", "init.R"))
}
# options(vsc.rstudioapi = TRUE)
options(vsc.viewer = "Beside")
If you are using renv
, this needs to be done per-project for each renv
where you want to use this wrapper.
Install the vscode-R extension and R Debugger Extension if you don't have them.
Configure the vscode R extension:
- R > Rterm: Linux (
r.rterm.linux
):~/bin/R-vscode-remote.sh
<- path to your copy of R-vscode-remote.sh
Some of these may be optional:
- R > Plot > Use Httpgd (
r.plot.useHttpgd
):true
- R > Plot > Defaults: Full Window Mode (
r.plot.defaults.fullWindowMode
):true
- R > Plot > Timing: Refresh Interval (
r.plot.timing.refreshInterval
): 50 (ms) - adjust higher if plots have issues updating - R > Lsp Enabled (
r.lsp.enabled
):true
- R > Lsp: Use_stdio (
r.lsp.use_stdio
):true
- R > Remove Leading Comments (
r.removeLeadingComments
):true
- R > Rterm Send Delay (
r.rtermSendDelay
): 15 (ms) - R > Session: Use Web Server (
r.session.useWebServer
):true
- R > Use Renv Lib Path (
r.useRenvLibPath
):true
(assuming you are usingrenv
)
You may wish to refer to the vscode-R docs for further detail.
Open an RMarkdown document, or open an R Terminal. Run some R code in the R Terminal, or click "Run Chunk" in the R Markdown to send a chunk of code to the R Terminal.
Click on the R logo in the left sidebar (or right click there to enable it) to see the R Environment 'variable browser'.
(TODO: Screenshots here)
Once the R tmux session is running (named vscode-r
by default), R auto-restarts every time you quit it.
To kill the session, run: R-kill-vscode-remote.sh
Problem: My plots aren't appearing, or I don't see any variables in the left hand side R pane.
Solution: Check that the extension has attached to the R interpretor (see bottom right corner of vscode, "R 4.3.1: xxxxx" - click there if it doesn't or type: .vsc.attach()
in the R Terminal).