Skip to content

Commit a4d66ce

Browse files
committed
Add QUARTO_KNITR_RSCRIPT_ARGS environment variable
to pass some flags to Rscript run by Quarto for knitr engine. This can be useful in specific situation like e.g `--max-connections=258` available in R 4.4, or `--vanilla` Args should be passed a comma separated list of flags. This is for expert use and there is not check done on the argument being support by RScript or not.
1 parent 2a83abb commit a4d66ce

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

src/execute/rmd.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,19 @@ async function callR<T>(
272272
wd: cwd,
273273
});
274274

275+
// QUARTO_KNITR_RSCRIPT_ARGS allows to pass additional arguments to Rscript as comma separated values
276+
// e.g. QUARTO_KNITR_RSCRIPT_ARGS="--vanilla,--no-init-file,--max-connections=258"
277+
const rscriptArgs = Deno.env.get("QUARTO_KNITR_RSCRIPT_ARGS") || "";
278+
const rscriptArgsArray = rscriptArgs.split(",").filter((a) =>
279+
a.trim() !== ""
280+
);
281+
275282
try {
276283
const result = await execProcess(
277284
{
278285
cmd: [
279286
await rBinaryPath("Rscript"),
287+
...rscriptArgsArray,
280288
resourcePath("rmd/rmd.R"),
281289
],
282290
cwd,

tests/docs/knitr/rscript-args.qmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Rscript args passed to quarto
3+
format: markdown_strict
4+
---
5+
6+
```{r}
7+
#| echo: false
8+
args <- commandArgs(trailingOnly = FALSE)
9+
# keep only flags
10+
args <- args[grep("^--", args)]
11+
args
12+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* cache.test.ts
3+
*
4+
* Copyright (C) 2023 Posit Software, PBC
5+
*/
6+
import { fileLoader, setEnvVar, restoreEnvVar } from "../../utils.ts";
7+
import { testRender } from "../render/render.ts";
8+
import { ensureFileRegexMatches, noErrorsOrWarnings } from "../../verify.ts";
9+
10+
// Default case should not error
11+
const rscriptArgsDoc = fileLoader()("knitr/rscript-args.qmd", "markdown_strict");
12+
testRender(rscriptArgsDoc.input, "markdown_strict", true, [
13+
noErrorsOrWarnings,
14+
]);
15+
16+
// Set special flag through env var
17+
let rscriptArgs: string | undefined;
18+
testRender(rscriptArgsDoc.input, "markdown_strict", true, [
19+
noErrorsOrWarnings,
20+
ensureFileRegexMatches(rscriptArgsDoc.output.outputPath, [
21+
/"--vanilla"/, /"--max-connection=258"/]
22+
),
23+
],
24+
{
25+
setup: async () => {
26+
rscriptArgs = setEnvVar("QUARTO_KNITR_RSCRIPT_ARGS", "--vanilla,--max-connections=258");
27+
},
28+
teardown: async () => {
29+
restoreEnvVar("QUARTO_KNITR_RSCRIPT_ARGS", rscriptArgs);
30+
},
31+
});

tests/smoke/render/render-required.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { testQuartoCmd } from "../../test.ts";
9-
import { docs } from "../../utils.ts";
9+
import { docs, setEnvVar, restoreEnvVar } from "../../utils.ts";
1010
import { printsMessage } from "../../verify.ts";
1111

1212
const input = docs("quarto-required.qmd");
@@ -27,8 +27,7 @@ testQuartoCmd(
2727
{
2828
setup: async () => {
2929
// Save current version of QUARTO_VERSION_REQUIREMENT env var and set it to a value that will not be satisfied
30-
oldVersionRequirement = Deno.env.get("QUARTO_VERSION_REQUIREMENT");
31-
Deno.env.set("QUARTO_VERSION_REQUIREMENT", "< 0.0.0");
30+
oldVersionRequirement = setEnvVar("QUARTO_VERSION_REQUIREMENT", "< 0.0.0");
3231
// Mock Deno.exit to throw an error instead of exiting
3332
// Otherwise we would not check the error assertion
3433
originalDenoExit = Deno.exit;
@@ -38,11 +37,7 @@ testQuartoCmd(
3837
},
3938
teardown: async () => {
4039
// Restore QUARTO_VERSION_REQUIREMENT
41-
if (oldVersionRequirement) {
42-
Deno.env.set("QUARTO_VERSION_REQUIREMENT", oldVersionRequirement);
43-
} else {
44-
Deno.env.delete("QUARTO_VERSION_REQUIREMENT");
45-
}
40+
restoreEnvVar("QUARTO_VERSION_REQUIREMENT", oldVersionRequirement);
4641
// Restore Deno.exit
4742
Deno.exit = originalDenoExit;
4843
},

tests/utils.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export function outputForInput(
114114
if (baseFormat === "revealjs") {
115115
outputExt = "html";
116116
}
117-
if (["commonmark", "gfm", "markdown"].some((f) => f === baseFormat)) {
117+
if (["commonmark", "gfm", "markdown", "markdown_strict"].some((f) => f === baseFormat)) {
118118
outputExt = "md";
119119
}
120120
if (baseFormat === "csljson") {
@@ -190,3 +190,17 @@ export function fileLoader(...path: string[]) {
190190
export function quartoDevCmd(): string {
191191
return Deno.build.os === "windows" ? "quarto.cmd" : "quarto";
192192
}
193+
194+
export function setEnvVar(name: string, value: string): string | undefined {
195+
const originalValue = Deno.env.get(name);
196+
Deno.env.set(name, value);
197+
return originalValue;
198+
}
199+
200+
export function restoreEnvVar(name: string, originalValue: string | undefined): void {
201+
if (originalValue !== undefined) {
202+
Deno.env.set(name, originalValue);
203+
} else {
204+
Deno.env.delete(name);
205+
}
206+
}

0 commit comments

Comments
 (0)