vscode-R
vscode-R copied to clipboard
Unable to attach active R terminal: `could not find function ".vsc.attach"`
Describe the bug
I updated R to the most recent version (4.2.0). As a consequence, I had to reinstall several packages, including languageserver
. After this I am no longer able to attach the active R terminal to the active VS Code session. Instead, when I click the "R" button in the bottom right corner, I get the error Error in .vsc.attach() : could not find function ".vsc.attach"
I tried reinstalling the R extension in VS Code as well, without any luck.
To Reproduce Steps to reproduce the behavior:
- Upgrade R to version 4.2'
- Install package
languageserver
- Reinstall R extension (REditorSupport)
- Start new R terminal
- Click on R button in the bottom left corner
- See error
Do you want to fix by self?
If it's just a tweak of settings, I might be able to do it myself.
setting.json (I doubt it's related, but I can't say for sure.)
"r.rterm.linux": "/opt/anaconda3/bin/radian",
// R command line options (i.e: --vanilla)
"r.rterm.option": [--no-save, --no-restore],
// Keeping focus when running
"r.source.focus": "editor",
// Use active terminal for all commands, rather than creating a new R terminal
"r.alwaysUseActiveTerminal": false,
// Use bracketed paste mode
"r.bracketedPaste": false,
// Enable R session watcher
"r.sessionWatcher": true,
// Delay in milliseconds before sending each line to rterm (only applies if r.bracketedPaste is false)
"r.rtermSendDelay": 8,
Expected behavior Start R kernel and and attaching to current session. R Help, plots, etc. opens in tab.
Screenshots
Environment (please complete the following information):
- OS: Ubuntu 20.04.4 LTS
- VSCode Version: 1.66.2
- R Version: 4.2.0
- vscode-R version: 2.4.0
Additional context
In addition to reinstalling languageserver
, a wide number of packages had to be updated/reinstalled. In addition, when starting a new R terminal in R, it started and Anaconda's (much) older R version. I had to copy the R executable from /bin/R
to opt/anaconda3/envs/renv/bin/R
to load the newly updated R version.
What happens if radian is not used? R installed with conda?
What happens if radian is not used? R installed with conda?
I get the same error. No, I installed from the official CRAN repositories through apt
.
Is there ~/.vscode-R/init.R
file in your home directory?
If present, what is written?
If the file references another file, does the referenced file exist?
Yes, ~/.vscode-R/init.R
references another file /home/user/.vscode/extensions/reditorsupport.r-2.4.0/R/session/init.R
.
Contents ~/.vscode-R/init.R
:
local(source("/home/user/.vscode/extensions/reditorsupport.r-2.4.0/R/session/init.R", chdir = TRUE, local = TRUE))
Contents ~/.vscode/extensions/reditorsupport.r-2.4.0/R/session/init.R
:
# This file is executed with its containing directory as wd
# Remember the working directory (should be extension subfolder that contains this script)
dir_init <- getwd()
# This function is run at the beginning of R's startup sequence
# Code that is meant to be run at the end of the startup should go in `init_last`
init_first <- function() {
# return early if not a vscode term session
if (
!interactive()
|| Sys.getenv("RSTUDIO") != ""
|| Sys.getenv("TERM_PROGRAM") != "vscode"
) {
return()
}
# check requried packages
required_packages <- c("jsonlite", "rlang")
missing_packages <- required_packages[
!vapply(required_packages, requireNamespace,
logical(1L), quietly = TRUE)
]
if (length(missing_packages)) {
message(
"VSCode R Session Watcher requires ",
toString(missing_packages), ". ",
"Please install manually in order to use VSCode-R."
)
} else {
# Initialize vsc utils after loading other default packages
assign(".First.sys", init_last, envir = globalenv())
}
}
old.First.sys <- .First.sys
# Overwrite for `.First.sys`
# Is used to make sure that all default packages are loaded first
# Will be assigned to and called from the global environment,
# Will be run with wd being the user's working directory (!)
init_last <- function() {
old.First.sys()
# cleanup previous version
removeTaskCallback("vscode-R")
options(vscodeR = NULL)
.vsc.name <- "tools:vscode"
if (.vsc.name %in% search()) {
detach(.vsc.name, character.only = TRUE)
}
# Source vsc utils in new environmeent
.vsc <- new.env()
source(file.path(dir_init, "vsc.R"), local = .vsc)
# attach functions that are meant to be called by the user/vscode
exports <- local({
.vsc <- .vsc
.vsc.attach <- .vsc$attach
.vsc.view <- .vsc$show_dataview
.vsc.browser <- .vsc$show_browser
.vsc.viewer <- .vsc$show_viewer
.vsc.page_viewer <- .vsc$show_page_viewer
View <- .vsc.view
environment()
})
attach(exports, name = .vsc.name, warn.conflicts = FALSE)
# overwrite S3 bindings from other packages
suppressWarnings({
if (!identical(getOption("vsc.helpPanel", "Two"), FALSE)) {
# Overwrite print function for results of `?`
.vsc$.S3method(
"print",
"help_files_with_topic",
.vsc$print.help_files_with_topic
)
# Overwrite print function for results of `??`
.vsc$.S3method(
"print",
"hsearch",
.vsc$print.hsearch
)
}
# Further S3 overwrites can go here
# ...
})
# remove this function from globalenv()
suppressWarnings(
rm(".First.sys", envir = globalenv())
)
# Attach to vscode
exports$.vsc.attach()
invisible()
}
init_first()
Then, as a workaround, the following configuration may work by loading init.R
immediately after R is started.
https://github.com/REditorSupport/vscode-R/wiki/R-Session-watcher#advanced-usage-for-self-managed-r-sessions
Thanks, for the tip! Adding the following code snippet
source(file.path(Sys.getenv(
if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"
), ".vscode-R", "init.R"))
at the beginning of "~/.Rprofile
the solved the problem. =)
@pehkawn The init.R
should only be sourced in interactive sessions. I updated the wiki:
if (interactive() && Sys.getenv("RSTUDIO") == "") {
source(file.path(Sys.getenv(if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"), ".vscode-R", "init.R"))
}
If the R session is non-interactive or is an RStudio session, then it should not source init.R
.
Thanks! I've updated my user .Rprofile
with your changes.
@pehkawn , can this be closed then or is it still an active issue ?
it works, so much thks
finally works, thx a lot
@renkun-ken , I think this can be closed now
Sorry for the late reply.
Yesterday I encountered the same issue when trying to start an R terminal in an existing workspace or by just opening the folder. However, it the issue was solved by opening a command line terminal and starting radian from the command line. After this the kernel will attach as expected, also later when I open an R terminal from the terminal menu.
What didn't work:
- Open folder/project with existing files
- Open "Terminal>New Terminal"
- Choose "R Terminal" from bottom right drop-down menu. Will not attach R session in active terminal.
What worked:
-
Open "Terminal>New Terminal" before opening a folder or workspace.
-
Choosing "R Terminal" from bottom right drop-down menu will attach R session correctly.
-
Open folder/project with existing files
-
Open "Terminal>New Terminal"
-
Start radian from shell in the new terminal (
% radian
)..vsc.attach
now attaches R session to active terminal correctly. -
Choosing "R Terminal" from bottom right drop-down menu will now also attach R session correctly.
It's also necessary to add these lines to ~/.Rprofile
. It's an easy fix, but nice if it wasn't necessary.
I am experiencing a problem similar to the one discussed in this issue. Due to certain circumstances, I want to use R with "r.alwaysUseActiveTerminal": true. Actually, I am using a supercomputer environment (Grid Engine) and need to use R while securing computing resources. When I launch R/Radian in that environment, the indicator in the bottom right corner of VS code shows "R: (not attached)." Even if I manually execute .vsc.attach() as told in this issue, it returns an error "Error in .vsc.attach(): could not find function ".vsc.attach".
I have confirmed that I can execute .vsc.attach() without any problems/error messages when "r.alwaysUseActiveTerminal": false", and the the bottom right indicates R is successfully attached. This might be off-topic, but I would really appreciate your help.
I suddenly see this in my r-base
devcontainer. Used to work fine about 3 weeks ago.
"customizations": {
"vscode": {
"extensions": [
"REditorSupport.r"
]
}
}
Today, I experienced the same issue mentioned in my previous post after running routine updates to my system, which included updating VS Code. Again, .vsc.attach()
did not attach the current R session. Updating R packages didn´t help.
In order to get it to work, I had to do the following steps:
- Close the current workspace,
- Open a new terminal.
.vsc.attach()
now correctly attached the current R session. - Open the folder with the current project, while the (correctly attached) R session running in terminal. (Note that simply reopening the saved workspace did not work. Neither did opening the project folder prior to opening a new terminal and attaching the R kernel.)
- Overwrite the old workspace ("File > Save Workspace As...").
- Close and reopen the freshly saved Workspace. The R session is now correctly attached.
Needless to say, but having to undertake these steps whenever there's an update shouldn't be unnecessary.
I've recently updated to R version 4.4.0 (2024-04-24) -- "Puppy Cup", Platform: aarch64-apple-darwin23.4.0 (64-bit), and faced the same issue. I didn't need to close the currently open workspace, but only launching a new R terminal clicking on the + symbol and selecting R terminal here:
And saving the workspace. The next time I opened the workspace, the terminal attached correctly.
Does anybody what this is working? Thanks!