Skip to content

Commit

Permalink
Add QUARTO_KNITR_RSCRIPT_ARGS environment variable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cderv committed Nov 7, 2024
1 parent 2a83abb commit a4d66ce
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/execute/rmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,19 @@ async function callR<T>(
wd: cwd,
});

// QUARTO_KNITR_RSCRIPT_ARGS allows to pass additional arguments to Rscript as comma separated values
// e.g. QUARTO_KNITR_RSCRIPT_ARGS="--vanilla,--no-init-file,--max-connections=258"
const rscriptArgs = Deno.env.get("QUARTO_KNITR_RSCRIPT_ARGS") || "";
const rscriptArgsArray = rscriptArgs.split(",").filter((a) =>
a.trim() !== ""
);

try {
const result = await execProcess(
{
cmd: [
await rBinaryPath("Rscript"),
...rscriptArgsArray,
resourcePath("rmd/rmd.R"),
],
cwd,
Expand Down
12 changes: 12 additions & 0 deletions tests/docs/knitr/rscript-args.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Rscript args passed to quarto
format: markdown_strict
---

```{r}
#| echo: false
args <- commandArgs(trailingOnly = FALSE)
# keep only flags
args <- args[grep("^--", args)]
args
```
31 changes: 31 additions & 0 deletions tests/smoke/knitr/rscript-args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* cache.test.ts
*
* Copyright (C) 2023 Posit Software, PBC
*/
import { fileLoader, setEnvVar, restoreEnvVar } from "../../utils.ts";
import { testRender } from "../render/render.ts";
import { ensureFileRegexMatches, noErrorsOrWarnings } from "../../verify.ts";

// Default case should not error
const rscriptArgsDoc = fileLoader()("knitr/rscript-args.qmd", "markdown_strict");
testRender(rscriptArgsDoc.input, "markdown_strict", true, [
noErrorsOrWarnings,
]);

// Set special flag through env var
let rscriptArgs: string | undefined;
testRender(rscriptArgsDoc.input, "markdown_strict", true, [
noErrorsOrWarnings,
ensureFileRegexMatches(rscriptArgsDoc.output.outputPath, [
/"--vanilla"/, /"--max-connection=258"/]
),
],
{
setup: async () => {
rscriptArgs = setEnvVar("QUARTO_KNITR_RSCRIPT_ARGS", "--vanilla,--max-connections=258");
},
teardown: async () => {
restoreEnvVar("QUARTO_KNITR_RSCRIPT_ARGS", rscriptArgs);
},
});
11 changes: 3 additions & 8 deletions tests/smoke/render/render-required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { testQuartoCmd } from "../../test.ts";
import { docs } from "../../utils.ts";
import { docs, setEnvVar, restoreEnvVar } from "../../utils.ts";
import { printsMessage } from "../../verify.ts";

const input = docs("quarto-required.qmd");
Expand All @@ -27,8 +27,7 @@ testQuartoCmd(
{
setup: async () => {
// Save current version of QUARTO_VERSION_REQUIREMENT env var and set it to a value that will not be satisfied
oldVersionRequirement = Deno.env.get("QUARTO_VERSION_REQUIREMENT");
Deno.env.set("QUARTO_VERSION_REQUIREMENT", "< 0.0.0");
oldVersionRequirement = setEnvVar("QUARTO_VERSION_REQUIREMENT", "< 0.0.0");
// Mock Deno.exit to throw an error instead of exiting
// Otherwise we would not check the error assertion
originalDenoExit = Deno.exit;
Expand All @@ -38,11 +37,7 @@ testQuartoCmd(
},
teardown: async () => {
// Restore QUARTO_VERSION_REQUIREMENT
if (oldVersionRequirement) {
Deno.env.set("QUARTO_VERSION_REQUIREMENT", oldVersionRequirement);
} else {
Deno.env.delete("QUARTO_VERSION_REQUIREMENT");
}
restoreEnvVar("QUARTO_VERSION_REQUIREMENT", oldVersionRequirement);
// Restore Deno.exit
Deno.exit = originalDenoExit;
},
Expand Down
16 changes: 15 additions & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function outputForInput(
if (baseFormat === "revealjs") {
outputExt = "html";
}
if (["commonmark", "gfm", "markdown"].some((f) => f === baseFormat)) {
if (["commonmark", "gfm", "markdown", "markdown_strict"].some((f) => f === baseFormat)) {
outputExt = "md";
}
if (baseFormat === "csljson") {
Expand Down Expand Up @@ -190,3 +190,17 @@ export function fileLoader(...path: string[]) {
export function quartoDevCmd(): string {
return Deno.build.os === "windows" ? "quarto.cmd" : "quarto";
}

export function setEnvVar(name: string, value: string): string | undefined {
const originalValue = Deno.env.get(name);
Deno.env.set(name, value);
return originalValue;
}

export function restoreEnvVar(name: string, originalValue: string | undefined): void {
if (originalValue !== undefined) {
Deno.env.set(name, originalValue);
} else {
Deno.env.delete(name);
}
}

0 comments on commit a4d66ce

Please sign in to comment.