If VS Code says R is not attached, it usually means the R extension has not connected to an interactive R session in the integrated terminal. It does not necessarily mean R is missing or your code is broken: check that R is installed, start or select an R terminal, and use the extension’s attach command if you launched R yourself.
What “R not attached” means in VS Code
VS Code is an editor, not an R interpreter. To run code from an R file, the R extension needs a running R process and a way to send commands to it. When the extension is not attached, editor features that depend on that live session—such as sending the current line or selection to R—may be unavailable or may fail to find the right terminal.
This is separate from syntax highlighting. VS Code can color R code and recognize an .R file even when R is not installed, because those editor behaviors do not require a running R process. Likewise, installing R does not automatically guarantee that VS Code knows where it is or that the extension has connected to it.
“Attached” can also mean different things in different situations. In ordinary use, the R extension is connected to a terminal running R so it can send code there. That is not the same as attaching a debugger to a process, and it does not mean the editor has opened an R project or loaded every package. First identify which feature is failing: starting R, sending code, language assistance, or debugging.
How the R extension connects to a session
The commonly used VS Code extension named R provides commands for creating an R terminal and, depending on the extension version and setup, connecting to an existing R terminal. Once a compatible session is available, commands such as sending a line, selection, or whole file can pass text from the editor to that session. The R process evaluates the code and prints output in the terminal.
There are several separate pieces in this workflow:
- R itself: The language runtime must be installed on the computer or otherwise available in the environment where VS Code runs.
- The R extension: It provides editor commands and coordinates communication with the R terminal.
- The terminal: This is where the interactive R process runs. It can be created by the extension or, in supported setups, started separately and then selected or attached.
- Optional language tools: The R language server can provide completion, diagnostics, and other code intelligence. It is distinct from the interactive R session used to execute code.
Because those components are independent, a problem in one does not prove that the others are broken. For example, completion may be unavailable while code still runs, or the R terminal may work even though the language server reports an error.
Check the installation and start an R terminal
Begin with the simplest test. Open VS Code’s integrated terminal and try to start R by entering R. If the shell reports that the command cannot be found, VS Code cannot launch R by that command. Install R for your operating system if necessary, or configure the extension with the correct path to the R executable. On Windows, the executable is commonly named R.exe; on macOS and Linux, it is commonly named R. The exact location depends on how R was installed.
If R starts, you should see its startup text and an interactive prompt, usually shown as >. Type a simple expression such as 1 + 1 and press Enter. If you get the result, the runtime itself is working. Quit that test session with q() if needed, then use the Command Palette in VS Code and search for the R extension’s command to create an R terminal. Command names can change between extension releases, so searching for “R: Create” or “R: Attach” is more reliable than relying on a remembered menu label.
If the extension starts R in a terminal but editor commands still do not work, check that the terminal is the one the extension expects to use. Multiple terminals can be open at once, and a shell prompt, a Python session, and an R session are not interchangeable. Select the terminal that visibly contains the R prompt before trying to send code.
Find the cause with a short troubleshooting sequence
Work through these checks in order rather than changing several settings at once. After each change, test with a small expression so you can tell whether it helped.
- Confirm the file type. Open or create a file ending in
.R, and check the language mode in the VS Code status bar. If the file is being treated as plain text or another language, R-specific commands may not behave as expected. - Confirm that R runs outside the editor workflow. Try
Rin the integrated terminal. If that fails, resolve the installation or executable path before troubleshooting attachment. - Create a fresh R terminal. Run the extension’s create-terminal command from the Command Palette. A stale terminal can contain a closed session, an error message, or a different program.
- Test from the editor. Put the cursor on a simple line such as
1 + 1and use the extension’s send-line command. Check whether the command reaches the R prompt and produces a result. - Try the attach command if R was started manually. Use the R extension’s attach-related command and select the correct active terminal if prompted. A normal terminal running R may not be recognized automatically.
- Reload VS Code after changing settings or installing R. Use the window reload command, then create a new terminal. Existing terminals do not always inherit changes made after they started.
- Inspect extension and terminal errors. Open the Output panel and choose the R-related output channel if available. Read the first relevant error, not just the final “not attached” message.
This sequence also helps separate a launch failure from an attachment failure. If no R prompt appears, focus on the installation, path, or startup configuration. If the prompt is visible but code cannot be sent, focus on the extension’s terminal selection, attachment command, and extension status.
Check executable paths and terminal settings
When R works in one application but not in VS Code, the two programs may be using different environments. VS Code might have been opened from a desktop shortcut that does not inherit the same PATH as a shell where R works. Alternatively, R may be installed in a nonstandard directory. The extension’s settings can specify the R executable or terminal command; consult the extension’s current documentation for the exact setting name and supported format for your operating system.
In VS Code, open Settings and search for r.rterm or search for R terminal settings provided by the extension. User settings apply across workspaces, while workspace settings can override them for one project. If a path is configured, make sure it points to the actual executable, not merely the folder containing it. Avoid copying a path format from a different operating system.
Terminal profiles and shells can matter too. A custom shell may change environment variables, startup behavior, or the way commands are interpreted. If R starts and immediately exits, temporarily test with a standard terminal profile and a clean R terminal. Also check whether a startup file such as .Rprofile is running code that errors or quits the session. A startup error can make the extension appear unable to attach even though it did launch R.
For remote development, check the machine where the extension is running. In an SSH, container, or Windows Subsystem for Linux workspace, installing R only on the local desktop may not be enough: the remote environment needs its own usable R installation, and the relevant extension may need to be installed or enabled in that remote context. Confirm which side owns the terminal before changing local paths.
Know what attachment does not fix
Connecting the editor to R does not install packages, choose a project library, or guarantee that a script has the data it expects. If the extension is attached and a line reaches the terminal but returns an error such as “there is no package called …,” that is a package installation or library issue, not an attachment issue. If the code runs but cannot find a file, check the working directory with getwd() and review how the project was opened.
Similarly, the R language server is not the interactive R terminal. The language server may be unavailable because it is not installed, cannot start, or has a separate configuration problem. That can affect completion, hover information, or diagnostics without preventing basic execution. Conversely, a working language server does not prove that an interactive R session is attached.
Debugging is another separate feature. Sending a line to an R prompt is ordinary interactive execution; stepping through code with breakpoints requires debugger support and appropriate configuration. Do not install or configure a debugger merely because the extension cannot find its terminal.
Common fixes and when to use them
- R is not installed: Install a current R distribution for your operating system, restart VS Code, and verify that the executable starts from the integrated terminal.
- R works only in another application: Set the extension’s R executable path or adjust the environment so VS Code can find it. Restart VS Code and create a new R terminal.
- A manually started session is ignored: Use the extension’s attach command, choose the terminal containing the R prompt, or create a terminal through the extension instead.
- The terminal is stuck or points to a dead session: Close that terminal and create a fresh one. Repeatedly sending code to a terminated process will not restore the connection.
- Only one project fails: Inspect workspace-specific settings and project startup files. A workspace override can differ from your normal user settings.
- Only completion or diagnostics fail: Troubleshoot the language server separately; do not assume the interactive R attachment is broken if code still executes.
Updating the extension can help if a known compatibility issue has been fixed, but updating is not a substitute for checking the terminal and executable path. If the problem began immediately after an update, note the extension version, VS Code version, operating system, and the exact error. Those details make an issue report or support request much more useful.
Related
Frequently Asked Questions
Why does VS Code say R is not attached when R is installed?
Installation and attachment are separate. VS Code may not know the executable path, may have no active R terminal, or may not be connected to a manually started session. Verify that R starts in the integrated terminal, then create or attach to a session using the R extension.
How do I attach an existing R session in VS Code?
Open the Command Palette and search for an R extension command containing “Attach.” If the command is available, run it and select the terminal that has an active R prompt. The exact label and behavior can depend on the extension version; creating a new R terminal through the extension is a useful test if attachment is unavailable.
Can I use VS Code for R without the R extension?
You can edit an R file without it, and you can run R commands by typing them directly into a terminal where R is installed. The extension adds convenient editor-to-terminal commands and R-focused editing features. Without it, you will generally need to manage code execution manually.
Why does code run in the terminal but autocomplete does not work?
Execution and autocomplete use different components. The interactive session may be attached correctly while the R language server is missing, still starting, or reporting its own error. Check the R-related Output channel and the extension’s documentation for language-server requirements.
Does “R not attached” mean I need to install Radian?
No. Radian is an optional alternative R console used by some setups, not a requirement for the basic R extension workflow. First test the standard R executable and terminal. Consider an alternative console only if you specifically want its features and have configured the extension to use it.