rtempquarto

Compile quarto document in temporary directory in R


I want to compile a template quarto report in a temporary directory. In the real case, the template is in an R package, and therefore has to compiled in a temporary directory. But I get the following error:

# This will create a file named "template.qmd" in the current folder.
# Used in: the "template.qmd" is a parametrized report and in a package. The report
# will be compiled in a temporary directly which will be cleaned afterwards.

f <- c("---", "title: \"Test\"", "format: html", "---", "", "## Set a variable",
  "", "```{r}", "OK <- FALSE", "```", "", "## Running Code depending on value of `OK`",
  "", "```{r}", "#| eval: !expr OK", "print(\"OK is TRUE - otherwise you won't see anything here.\")",
  "```", "")
writeLines(f, "template.qmd")

name <- "Test_Report"

output_format <- "html"
output_file <- paste0(name, ".html")

tmpdir <- tempfile()
dir.create(tmpdir)
template <-  file.path(tmpdir, "template.qmd")

file.copy(
  file.path(".", "template.qmd"),
  template
)


report <- quarto::quarto_render(
  input = template,
  output_format = output_format,
  output_file = output_file
)

# Here the error occurs:
#
# processing file: template.qmd
# |..............                                                        |  20%
# ordinary text without R code
#
# |............................                                          |  40%
# label: unnamed-chunk-1
# |..........................................                            |  60%
# ordinary text without R code
#
# |........................................................              |  80%
# label: unnamed-chunk-2 (with options)
# List of 1
# $ eval: logi FALSE
#
# |......................................................................| 100%
# ordinary text without R code
#
#
# output file: template.knit.md
#
# pandoc --output ../../../../../../../Users/rainerkrug/tmp/test/Test_Report.html
# to: html
# standalone: true
# section-divs: true
# html-math-method: mathjax
# wrap: none
# default-image-extension: png
#
# metadata
# document-css: false
# link-citations: true
# date-format: long
# lang: en
# title: Test
#
# pandoc: ../../../../../../../Users/rainerkrug/tmp/test/Test_Report.html: openFile: does not exist (No such file or directory)
# Error in `processx::run(quarto_bin, args, echo = TRUE)`:
#   ! System command 'quarto' failed
# ---
#   Exit status: 1
# stdout & stderr: <printed>
#   ---
#   Type .Last.error to see the more details.

# And I would like to continuye as follows:

file.copy(
  file.path(tmpdir, output_file),
  file.path(".", output_file),
)
unlink(tmpdir)

Session Info:

> sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9      ps_1.7.2        digest_0.6.31   later_1.3.0     R6_2.5.1       
 [6] jsonlite_1.8.4  evaluate_0.19   rlang_1.0.6     cli_3.6.0       rstudioapi_0.14
[11] rmarkdown_2.19  tools_4.2.2     yaml_2.3.6      xfun_0.36       fastmap_1.1.0  
[16] compiler_4.2.2  processx_3.8.0  htmltools_0.5.4 knitr_1.41      quarto_1.2     
> 

Is this a bug in quarto, or am I doing something obvious wrong?


Solution

  • There seems to be a bug in quarto / panda on Mac, as it works on Ubuntu. The problem is the argument output_file = output_file. As soon as it is present, the Mac implementation fails. I solved it wit the following:

    f <- c("---", "title: \"Test\"", "format: html", "---", "", "## Set a variable",
           "", "```{r}", "OK <- FALSE", "```", "", "## Running Code depending on value of `OK`",
           "", "```{r}", "#| eval: !expr OK", "print(\"OK is TRUE - otherwise you won't see anything here.\")",
           "```", "")
    writeLines(f, "template.qmd")
    
    name <- "Test_Report"
    
    output_format <- "html"
    output_file <- paste0(name, ".html")
    
    
    tmpdir <- tempfile()
    dir.create(tmpdir)
    template <- file.path(tmpdir, paste0(name, ".qmd"))
    
    file.copy(
      file.path(".", "template.qmd"),
      template
    )
    
    
    report <- quarto::quarto_render(
      input = template,
      output_format = output_format
    )
    
    
    fn <- c(paste0(name, ".html"), paste0(name, "_files"))
    
    file.copy(
      file.path(tmpdir, fn),
      file.path("."),
      recursive = TRUE
    )
    
    unlink(tmpdir)