Skip to content

Commit 95cc434

Browse files
committed
Pass .libPaths() as R_LIBS to quarto subprocess
1 parent ec42247 commit 95cc434

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ Imports:
2626
rstudioapi,
2727
tools,
2828
utils,
29+
withr,
2930
yaml
3031
Suggests:
3132
curl,
3233
knitr,
3334
rsconnect (>= 0.8.26),
3435
testthat (>= 3.1.7),
35-
withr,
3636
xfun
3737
VignetteBuilder:
3838
quarto

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- `quarto_path()` now correctly return `NULL` when no quarto is found in the PATH (thanks, @jeroen, #220, #221).
1212

13+
- `.libPaths()` from the calling R session will now be passed by default to all call to quarto as a subprocess. This should solve issue with **pkgdown** or when building vignettes (thanks, )
14+
1315
# quarto 1.4.4
1416

1517
- `quarto_preview()` now looks at `quarto preview` log to browse to the correct url when inside RStudio viewer (thanks, @aronatkins, #167).

R/quarto.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ quarto_version <- function() {
4848
}
4949

5050
#' @importFrom processx run
51-
quarto_run <- function(args = character(), quarto_bin = find_quarto(), echo = FALSE, echo_cmd = getOption("quarto.echo_cmd", FALSE), ..., .call = rlang::caller_env()) {
51+
quarto_run <- function(args = character(), quarto_bin = find_quarto(), echo = FALSE, libpaths = .libPaths(), echo_cmd = getOption("quarto.echo_cmd", FALSE), ..., .call = rlang::caller_env()) {
52+
opt_in_libpath <- getOption("quarto.use_libpaths", TRUE)
53+
if (isTRUE(opt_in_libpath) && !is.null(libpaths)) {
54+
withr::local_envvar(list(
55+
R_LIBS = paste(libpaths, collapse = .Platform$path.sep)
56+
))
57+
}
5258
res <- tryCatch(
5359
{
5460
processx::run(quarto_bin, args = args, echo = echo, error_on_status = TRUE, echo_cmd = echo_cmd, ...)

R/render.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#' advanced usage and useful for CLI arguments which are not yet mirrored in a
4747
#' dedicated parameter of this \R function. See `quarto render --help` for options.
4848
#' @param pandoc_args Additional command line arguments to pass on to Pandoc.
49+
#' @param libpaths A character vector of library paths to use for the R session run by Quarto.
50+
#' If `NULL`, no library paths will be pass to quarto subprocess and defaults R one will be used.
51+
#' Setting `options(quarto.use_libpaths = FALSE)` will disable this behavior and
52+
#' never pass library paths to quarto subprocess.
4953
#' @param as_job Render as an RStudio background job. Default is `"auto"`,
5054
#' which will render individual documents normally and projects as
5155
#' background jobs. Use the `quarto.render_as_job` \R option to control
@@ -88,6 +92,7 @@ quarto_render <- function(input = NULL,
8892
profile = NULL,
8993
quarto_args = NULL,
9094
pandoc_args = NULL,
95+
libpaths = .libPaths(),
9196
as_job = getOption("quarto.render_as_job", "auto")) {
9297
# get quarto binary
9398
quarto_bin <- find_quarto()
@@ -188,7 +193,7 @@ quarto_render <- function(input = NULL,
188193
}
189194

190195
# run quarto
191-
quarto_run(args, echo = TRUE, quarto_bin = quarto_bin)
196+
quarto_run(args, echo = TRUE, quarto_bin = quarto_bin, libpaths = libpaths)
192197

193198
# no return value
194199
invisible(NULL)

man/quarto_render.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-quarto.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,26 @@ test_that("quarto.quiet options controls echo and overwrite function argument",
8888
expect_output(quarto_render(qmd, quiet = TRUE))
8989
})
9090
})
91+
92+
test_that("quarto sees same libpaths as main process", {
93+
skip_if_no_quarto()
94+
skip_on_cran()
95+
qmd <- local_qmd_file(c("```{r}", "#| echo: false", ".libPaths()", "```"))
96+
tmp_lib <- withr::local_tempdir("tmp_libpath")
97+
withr::local_libpaths(tmp_lib, action = "prefix")
98+
withr::local_dir(dirname(qmd))
99+
out <- "out.md"
100+
# .libPaths() is known in Quarto render
101+
quarto_render(qmd, output_format = "gfm", output_file = out, quiet = TRUE)
102+
expect_match(readLines(out), basename(tmp_lib), all = FALSE, fixed = TRUE)
103+
# Opting-out globally
104+
withr::with_options(
105+
list(quarto.use_libpaths = FALSE),
106+
quarto_render(qmd, output_format = "gfm", output_file = out, quiet = TRUE)
107+
)
108+
expect_no_match(readLines(out), basename(tmp_lib), fixed = TRUE)
109+
# Opting-out at command
110+
quarto_render(qmd, output_format = "gfm", output_file = out, quiet = TRUE, libpaths = NULL)
111+
expect_no_match(readLines(out), basename(tmp_lib), fixed = TRUE)
112+
113+
})

0 commit comments

Comments
 (0)