Skip to content

Commit 6017b0d

Browse files
feat(plot): auto backend tries jgd first when installed
In auto mode, the JGD socket server starts eagerly and R-side hooks try jgd > httpgd > standard based on package availability. Users who install the jgd R package get interactive plots with zero config. r.plot.useHttpgd: true still forces httpgd for backward compatibility. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1ac9b1f commit 6017b0d

5 files changed

Lines changed: 14 additions & 15 deletions

File tree

R/profile.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ local({
2525
})
2626

2727
if (requireNamespace("sess", quietly = TRUE)) {
28-
plot_backend <- Sys.getenv("SESS_PLOT_BACKEND", "standard")
28+
plot_backend <- Sys.getenv("SESS_PLOT_BACKEND", "auto")
2929
sess::connect(
3030
use_rstudioapi = as.logical(Sys.getenv("SESS_RSTUDIOAPI", "TRUE")),
31-
use_httpgd = (plot_backend == "httpgd"),
32-
use_jgd = (plot_backend == "jgd")
31+
use_httpgd = (plot_backend %in% c("auto", "httpgd")),
32+
use_jgd = (plot_backend %in% c("auto", "jgd"))
3333
)
3434
}

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,8 +1841,7 @@
18411841
"r.plot.useHttpgd": {
18421842
"type": "boolean",
18431843
"default": false,
1844-
"markdownDescription": "Use the httpgd-based plot viewer instead of the base VSCode-R plot viewer.\n\nRequires the `httpgd` R package version 1.2.0 or later.",
1845-
"markdownDeprecationMessage": "Use `#r.plot.backend#` instead."
1844+
"markdownDescription": "Use the httpgd-based plot viewer instead of the base VSCode-R plot viewer.\n\nRequires the `httpgd` R package version 1.2.0 or later.\n\n**Deprecated:** Use `#r.plot.backend#` instead. When `#r.plot.backend#` is `auto`, setting this to `true` forces httpgd."
18461845
},
18471846
"r.plot.backend": {
18481847
"type": "string",
@@ -1854,12 +1853,12 @@
18541853
"jgd"
18551854
],
18561855
"markdownEnumDescriptions": [
1857-
"Automatic: uses httpgd if `#r.plot.useHttpgd#` is true, otherwise standard",
1856+
"Automatic: tries JGD first (if installed), then httpgd, then standard. Respects `#r.plot.useHttpgd#` if set.",
18581857
"Standard static plot viewer (PNG/SVG)",
18591858
"httpgd-based interactive plot viewer (requires `httpgd` R package)",
18601859
"JGD-based interactive plot viewer (requires `jgd` R package)"
18611860
],
1862-
"markdownDescription": "Select the plot backend.\n\nWhen set to `auto`, the backend is determined by the legacy `#r.plot.useHttpgd#` setting."
1861+
"markdownDescription": "Select the plot backend.\n\nWhen set to `auto`, the best available backend is used (JGD if installed, then httpgd, then standard). Setting `#r.plot.useHttpgd#` to `true` forces httpgd."
18631862
},
18641863
"r.plot.jgd.historyLimit": {
18651864
"type": "number",

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<apiImp
203203
// initialize plot manager
204204
globalPlotManager = plotViewer.initializePlotManager();
205205

206-
// Set JGD_SOCKET env var for R child processes when using JGD backend
207-
if (plotViewer.resolveBackend() === 'jgd') {
206+
// Set JGD_SOCKET env var for R child processes when JGD server is running
207+
if (plotViewer.resolveBackend() === 'jgd' || plotViewer.resolveBackend() === 'auto') {
208208
const jgdVars = (globalPlotManager as plotViewer.CommonPlotManager).getJgdEnvVars();
209209
for (const [key, value] of Object.entries(jgdVars)) {
210210
context.environmentVariableCollection.replace(key, value);

src/plotViewer/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { JgdManager } from './jgdViewer';
88
import { extensionContext } from '../extension';
99
import { config } from '../util';
1010

11-
export function resolveBackend(): 'standard' | 'httpgd' | 'jgd' {
11+
export function resolveBackend(): 'auto' | 'standard' | 'httpgd' | 'jgd' {
1212
const explicit = config().get<string>('plot.backend', 'auto');
1313
if (explicit !== 'auto') return explicit as 'standard' | 'httpgd' | 'jgd';
1414
if (config().get<boolean>('plot.useHttpgd', false)) return 'httpgd';
15-
return 'standard';
15+
return 'auto';
1616
}
1717

1818
const commands = [
@@ -56,8 +56,8 @@ export class CommonPlotManager implements PlotManager {
5656

5757
get activeViewer(): PlotViewer | undefined {
5858
const backend = resolveBackend();
59-
if (backend === 'jgd') {
60-
return this.jgdManager.getViewer() || this.standardPlotViewer;
59+
if (backend === 'jgd' || backend === 'auto') {
60+
return this.jgdManager.getViewer() || this.httpgdManager.getRecentViewer() || this.standardPlotViewer;
6161
}
6262
return this.httpgdManager.getRecentViewer() || this.standardPlotViewer;
6363
}
@@ -128,7 +128,7 @@ export function initializePlotManager(): PlotManager {
128128
manager.initialize();
129129

130130
const backend = resolveBackend();
131-
if (backend === 'jgd') {
131+
if (backend === 'jgd' || backend === 'auto') {
132132
manager.jgdManager.start();
133133
}
134134

src/rTerminal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export async function makeTerminalOptions(): Promise<vscode.TerminalOptions> {
204204
SESS_USE_HTTPGD: backend === 'httpgd' ? 'TRUE' : 'FALSE',
205205
SESS_PLOT_BACKEND: backend,
206206
};
207-
if (backend === 'jgd') {
207+
if (backend === 'jgd' || backend === 'auto') {
208208
const jgdVars = (globalPlotManager as CommonPlotManager)?.getJgdEnvVars() ?? {};
209209
Object.assign(termOptions.env, jgdVars);
210210
}

0 commit comments

Comments
 (0)