R workspace keep empty when used in VScode
I tried to build R environment in VScode. But the workspace region still showed nothing when I created R dataframe.
What I've done: I created a r_env environment on Linux server for R analysis. I've installed R packages including httpgd, languageserver and radian. The versions are as below: r-base 4.4.3 h9c32bc2_0 conda-forge r-httpgd 2.0.4 r44h93ab643_0 conda-forge radian 0.6.13 pypi_0 pypi packageVersion('languageserver') [1] ‘0.3.16’
My settings.json is as below: { "r.alwaysUseActiveTerminal": true, "r.bracketedPaste": true, "r.rterm.option": [ "--r-binary=/mnt/data/userdata/zhangliyin/anaconda3/envs/r_env/bin/R", "--no-site-file" ], "r.plot.useHttpgd": true, "r.rterm.linux": "/mnt/data/userdata/zhangliyin/anaconda3/envs/r_env/bin/radian", "r.rpath.linux": "/mnt/data/userdata/zhangliyin/anaconda3/envs/r_env/bin/R", "r.workspaceViewer.removeHiddenItems": true, "r.workspaceViewer.showObjectSize": true }
I read some of similar issues and tried mentioned solutions, but the problem still remains. I would really appreciate your help! Thanks a lot!
I have had some similar problems. I could fix it when connecting to my local linux via WSL. I only partially solved the issue when connecting to the linux server, but the problem is that the server I use is divided into login and compute nodes. If yours is not the same way, then the following might help you:
Try switching "r.alwaysUseActiveTerminal": true to "r.alwaysUseActiveTerminal": false. When running codes, it will open a new "R Interactive" terminal. It should attach properly this way. If your server is divided into login and compute, then be careful, because it is likely launching the R terminal on the login node.
Another thing I noted from your post is that you installed httpgd via conda ("r-httpgd"), but languageserver seems to be installed via install.packages(). I guess this could create some problems as well, maybe not for Reditorsupport, but it will certainly be a problem if you ever try sharing your environment with others. Thus, I recommend installing all R packages via conda. Try removing the languageserver from within R and then install r-languageserver from conda-forge
@lgn-almeida Hi, may I ask what kinds of ways you use R in your local linux via WSL, through system-level R or R in conda env?
I have some problems in setting radian as the default console in vscode-R when using R in conda env (also, radian was installed in the conda env). If I set "r.rterm.linux" with directory of radian (like ${userHome}/../envs/
Hi @Shijie-Jian. I also use R through conda, and in one of my environments, I do use radian. I can share with you my configs and a few things to check on your end.
First, I would navigate to your project folder in WSL (i.e., via the command line), load your conda environment and start radian. See if radian starts up properly via the command line. If it does load properly, then the fix must be on the VS Code side; otherwise, your Radian/conda installation might have a problem.
For VScode, here is how I use it:
First, I navigate to my project's root folder in WSL (cd projects/project1), then activate the conda env (conda activate myenv), and start VScode via the terminal (code .). This will launch VSCode on the project's root folder. In the project root, I create a ".vscode" folder, and inside of it I create a settings.json file with this inside.
Here is my folder structure:
project_root
│
├── .vscode
│ └── settings.json
│
├── codes
│ └── draft1.R
│
├── data
│ └── some_data.csv
│
└── figures
└── figure1.png
I prefer to keep a .vscode folder and a settings.json per project as I can configure them independently (for instance, use a different conda env per project), but you can also set this globally and edit the global settings.json file.
Here is the content of settings.json:
{
"r.rterm.linux": "/home/lgn/miniconda3/envs/R_4.5.1/bin/radian",
"r.rpath.linux": "/home/lgn/miniconda3/envs/R_4.5.1/bin/R",
"terminal.integrated.env.linux": {
"R_HOME": "/home/lgn/miniconda3/envs/R_4.5.1/lib/R"
},
"r.bracketedPaste": true,
"r.alwaysUseActiveTerminal": false,
"r.plot.useHttpgd": true,
"r.sessionWatcher": true
}
It’s important to start VS Code from a terminal where your conda environment is already activated (for example, by running conda activate myenv and then launching VS Code with code .).
Doing this ensures that all terminals opened within VS Code automatically load the same conda environment.
If instead you open VS Code via the GUI (e.g., from the Start Menu) and then navigate to your project folder and activate the conda environment manually in one terminal, only that specific terminal will have the environment active.
To use radian in that terminal, you’ll need to set the following in your VS Code settings:
"r.alwaysUseActiveTerminal": true
Otherwise, VS Code will launch a new R terminal (r.term) that won’t have your conda environment active, meaning radian won’t be available.
In my setup, when I activate conda, the environment name is appended to my shell prompt. For example:
- Conda not active: lgn:~/projects/$
- Conda active: (R_4.5.1) lgn:~/projects/$
However, if I activate conda in the terminal before launching VS Code, the active environment name no longer appears in the terminal prompt inside VS Code. To fix this, I added the following lines to my bash configuration file (located in my home directory): File: ~/.bashrc
if [ -n "$CONDA_DEFAULT_ENV" ]; then
conda activate "$CONDA_DEFAULT_ENV"
fi
Hope this helps!
Thanks for your reply. When I followed your instruction "activate conda first, then launch vs code", the active conda env in the terminal (I used bash instead) inside vs code is base env, I found it okey as it just need to activate specific env manually. Also, I want to mentioned that there is useful setting to link radian (within a conda env) in terminal to R Session watcher of vscode-R extension (in order to show used variables and their value). (https://github.com/REditorSupport/vscode-R/wiki/R-Session-watcher) (Advanced usage (for self-managed R [sessions)) Just modified .Rprofile file (created if it does not exist) in the project-root directory with setting described in the website above, and restart radian.