Skip to content

Commit 2dbc444

Browse files
committed
Set explicitly R working directory expected by Quarto scripts
Some unexpected workflows (like using `setwd()` in `~/.Rprofile`) may change the R working directory to be different that the R process `cwd` set with `Deno.run()` Related to #2646
1 parent 0b46497 commit 2dbc444

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/execute/rmd.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ async function callR<T>(
197197
): Promise<T> {
198198
// establish cwd for execute (the current dir if there is an renv
199199
// otherwise the project dir if specified)
200-
const cwd = withinActiveRenv() ? undefined : projectDir;
200+
const cwd = withinActiveRenv() ? undefined : projectDir ?? Deno.cwd();
201201

202202
// create a temp file for writing the results
203203
const resultsFile = Deno.makeTempFileSync(
@@ -208,6 +208,7 @@ async function callR<T>(
208208
action,
209209
params,
210210
results: resultsFile,
211+
wd: cwd,
211212
});
212213

213214
try {

src/resources/rmd/rmd.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@
137137
request <- jsonlite::parse_json(input, simplifyVector = TRUE)
138138
params <- request$params
139139

140+
# Ensuring expected working dir for Quarto
141+
# R process workding may be changed by some workflows (in ~/.Rprofile)
142+
# https://github.com/quarto-dev/quarto-cli/issues/2646
143+
setwd(request$wd) # no need to reset it as R process is closed by R
144+
140145
# source in helper functions if we have a resourceDir
141146
if (!is.null(params$resourceDir)) {
142147
res_dir <- file.path(params$resourceDir, "rmd")

0 commit comments

Comments
 (0)