'R not attached in VS Code on Win10

I am recently switching from RStudio to VS Code. I have installed R extension in VS Code, but when I open VS Code and R terminal, R cannot be loaded. I followed coip's method but still cannot get R loaded: enter image description here

############################################################################## I followed coip's suggstion and R is successfully activated. In another of my laptop (Win), R could be activated without any issue. But when I check the settings.json file, there is no such specification as r.rterm.windows. I want to ask why my laptop could successfuly load R in VS Code without having to include those code snippet suggested by coip?

Below is the settings.json in my laptop:

{
    "workbench.colorTheme": "Default Dark+",
    "python.formatting.provider": "yapf",
    "security.workspace.trust.untrustedFiles": "open",
    "python.defaultInterpreterPath": "C:\\Users\\Patrick Wen\\AppData\\Local\\Programs\\Python\\Python310\\python.exe",
    "terminal.integrated.enableMultiLinePasteWarning": false,
    "files.associations": {
        "*.rmd": "markdown"
    }
}


Solution 1:[1]

If that is your complete settings.json, you're missing common parameters such as r.rpath.windows and the --r-binary under r.rterm.option. I believe adding those, with the same path to R that you currently have for r.rpath.windows should fix your issue.

Here is a typical setup that you can copy and paste into your JSON settings, which you can access via View > Command Palette > Preferences: Open Settings (JSON):

{
// R Options
"r.rterm.windows": "C:\\Program Files\\R\\R-4.1.2\\bin\\R.exe",
"r.rpath.windows": "C:\\Program Files\\R\\R-4.1.2\\bin\\R.exe",
"r.lsp.path": "C:\\Program Files\\R\\R-4.1.2\\bin\\R.exe",
"r.lsp.debug": true,
"r.lsp.diagnostics": true,
"r.alwaysUseActiveTerminal": true,
"r.rterm.option": [
    "--no-save",
    "--no-restore",
    "--r-binary=C:\\Program Files\\R\\R-4.1.2\\bin\\R.exe"
],
"r.bracketedPaste": true,
"r.sessionWatcher": true
}

Note: some of those are just preferences (e.g. r.bracketedPaste).

So you can try to add all of the above or some of the above, save your JSON settings, and relaunch VSCode, until the problem is fixed.

If that doesn't work, let's try setting up an integrated profile, by adding the following to your settings.json:

"terminal.integrated.profiles.windows": {
   "PowerShell": {
       "source": "PowerShell",
       "icon": "terminal-powershell"
   },
   "Command Prompt": {
       "path": [
          "${env:windir}\\Sysnative\\cmd.exe",
          "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
   },
   "R": {
       "path": [
           "C:\\Program Files\\R\\R-4.1.2\\bin\\R.exe"
       ]
   },
},  

You can also try making R your default terminal by adding the following line above too:

"terminal.integrated.defaultProfile.windows": "R",

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1