diff --git a/package.json b/package.json index 71267c7c8..dacfe9780 100644 --- a/package.json +++ b/package.json @@ -508,7 +508,7 @@ "r.helpPanel.rpath": { "type": "string", "default": "", - "markdownDescription": "Path to an R executable. Defaults to `r.rterm.XXX`. Must be \"vanilla\" R, not radian etc.! " + "markdownDescription": "Path to an R executable. Must be \"vanilla\" R, not radian etc.! Will be read from registry or path if not set." }, "r.helpPanel.helpProvider": { "type": "string", diff --git a/src/extension.ts b/src/extension.ts index 7c174e7ef..5333e8d83 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -12,7 +12,7 @@ import { createRTerm, deleteTerminal, runSelectionInTerm, runTextInTerm } from './rTerminal'; import { getWordOrSelection, surroundSelection } from './selection'; import { attachActive, deploySessionWatcher, globalenv, showPlotHistory, startRequestWatcher } from './session'; -import { config, ToRStringLiteral, getRpath } from './util'; +import { config, ToRStringLiteral, getRpath, getRpathFromSystem } from './util'; import { launchAddinPicker, trackLastActiveTextEditor } from './rstudioapi'; import { RMarkdownCodeLensProvider, RMarkdownCompletionItemProvider, runCurrentChunk, runAboveChunks } from './rmarkdown'; @@ -50,7 +50,7 @@ export async function activate(context: ExtensionContext) { const rExtension = new RExtension(); // get the "vanilla" R path from config - let rPath = config().get('helpPanel.rPath', '') || await getRpath(); + let rPath = config().get('helpPanel.rpath', '') || await getRpathFromSystem(); if(rPath.match(/^[^'"].* .*[^'"]$/)){ rPath = `"${rPath}"`; } diff --git a/src/util.ts b/src/util.ts index 2f8b27116..eaf138471 100644 --- a/src/util.ts +++ b/src/util.ts @@ -29,6 +29,31 @@ function getRfromEnvPath(platform: string) { return ''; } +export async function getRpathFromSystem() { + + let rpath: string = ''; + const platform: string = process.platform; + + if ( platform === 'win32') { + // Find path from registry + try { + const key = new winreg({ + hive: winreg.HKLM, + key: '\\Software\\R-Core\\R', + }); + const item: winreg.RegistryItem = await new Promise((c, e) => + key.get('InstallPath', (err, result) => err === null ? c(result) : e(err))); + rpath = path.join(item.value, 'bin', 'R.exe'); + } catch (e) { + rpath = ''; + } + } + + rpath ||= getRfromEnvPath(platform); + + return rpath; +} + export async function getRpath() { let rpath: string = ''; @@ -36,36 +61,23 @@ export async function getRpath() { if ( platform === 'win32') { rpath = config().get('rterm.windows'); - if (rpath === '') { - // Find path from registry - try { - const key = new winreg({ - hive: winreg.HKLM, - key: '\\Software\\R-Core\\R', - }); - const item: winreg.RegistryItem = await new Promise((c, e) => - key.get('InstallPath', (err, result) => err === null ? c(result) : e(err))); - rpath = path.join(item.value, 'bin', 'R.exe'); - } catch (e) { - rpath = ''; - } - } } else if (platform === 'darwin') { rpath = config().get('rterm.mac'); } else if (platform === 'linux') { rpath = config().get('rterm.linux'); } + + rpath ||= await getRpathFromSystem(); - if (rpath === '') { - rpath = getRfromEnvPath(platform); - } if (rpath !== '') { return rpath; } + window.showErrorMessage(`${process.platform} can't use R`); return undefined; } + export function ToRStringLiteral(s: string, quote: string) { if (s === undefined) { return 'NULL';