Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for Shiny for R apps that use ui.R and friends #39

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,14 @@ function isShinyAppUsername(filename: string, language: string): boolean {
return true;
}

if (language === "r") {
return isShinyAppRPart(filename);
}

return false;
}

export function isShinyAppRPart(filename: string): boolean {
filename = path.basename(filename);
return ["ui.r", "server.r", "global.r"].includes(filename.toLowerCase());
}
32 changes: 19 additions & 13 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PythonExtension } from "@vscode/python-extension";
import * as vscode from "vscode";
import { join as path_join } from "path";
import { join as path_join, dirname as path_dirname } from "path";
import * as fs from "fs";
import { isShinyAppRPart } from "./extension";
import { openBrowser, openBrowserWhenReady } from "./net-utils";
import {
envVarsForShell as envVarsForTerminal,
Expand Down Expand Up @@ -130,11 +131,13 @@ export async function pyDebugApp(): Promise<void> {

/* Shiny for R --------------------------------------------------------- */
export async function rRunApp(): Promise<void> {
const path = getActiveEditorFile();
if (!path) {
const pathFile = getActiveEditorFile();
if (!pathFile) {
return;
}

const path = isShinyAppRPart(pathFile) ? path_dirname(pathFile) : pathFile;

const port = await getAppPort("run", "r");
// TODO: Is this needed for Shiny for R too?
// const autoreloadPort = await getAutoreloadPort("run");
Expand Down Expand Up @@ -164,8 +167,8 @@ export async function rRunApp(): Promise<void> {
if (!rscriptBinPath) {
vscode.window.showErrorMessage(
"Could not find R. Is R installed on your system?" +
"If R is installed, please make sure your PATH " +
"environment variable is configured correctly."
"If R is installed, please make sure your PATH " +
"environment variable is configured correctly."
);
return;
}
Expand Down Expand Up @@ -279,7 +282,7 @@ async function checkForPythonExtension(): Promise<boolean> {

const response = await vscode.window.showErrorMessage(
"The Python extension is required to run Shiny apps. " +
"Please install it and try again.",
"Please install it and try again.",
"Show Python extension",
"Not now"
);
Expand Down Expand Up @@ -311,7 +314,7 @@ async function getSelectedPythonInterpreter(): Promise<string | false> {
if (!resolvedEnv) {
vscode.window.showErrorMessage(
"Unable to find Python interpreter. " +
'Please use the "Python: Select Interpreter" command, and try again.'
'Please use the "Python: Select Interpreter" command, and try again.'
);
return false;
}
Expand Down Expand Up @@ -341,7 +344,7 @@ function getExtensionPath(): string | undefined {
}

async function getRBinPath(bin: string): Promise<string> {
return getRPathFromEnv(bin) || await getRPathFromWindowsReg(bin) || "";
return getRPathFromEnv(bin) || (await getRPathFromWindowsReg(bin)) || "";
}

function getRPathFromEnv(bin: string = "R"): string {
Expand Down Expand Up @@ -373,15 +376,18 @@ async function getRPathFromWindowsReg(bin: string = "R"): Promise<string> {
try {
const key = new winreg({
hive: winreg.HKLM,
key: '\\Software\\R-Core\\R',
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', bin + ".exe");
key.get("InstallPath", (err, result) =>
err === null ? c(result) : e(err)
)
);
rPath = path_join(item.value, "bin", bin + ".exe");
rPath = fs.existsSync(rPath) ? rPath : "";
} catch (e) {
rPath = '';
rPath = "";
}

return rPath;
}
}
Loading