You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R临时目录编译Quarto模板文档报错,请求排查原因

在临时目录编译Quarto模板报告报错问题排查

我需要在临时目录编译一个Quarto模板报告(实际场景中该模板位于R包内,因此必须在临时目录中编译),但执行相关代码时出现错误。请问这是Quarto的bug,还是我存在明显的操作失误?

测试代码

# 会在当前文件夹创建"template.qmd"文件
# 实际场景:该模板是参数化报告且位于R包内,需在临时目录编译后清理临时目录

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
)

# 后续计划执行的操作:
file.copy(
  file.path(tmpdir, output_file),
  file.path(".", output_file),
)
unlink(tmpdir)

报错信息

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.

会话信息

> 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     
> 

问题原因与解决方案

这不是Quarto的bug,是对参数路径解析逻辑理解偏差导致的操作失误:

问题根源

quarto_render的output_file参数仅指定输出文件名,默认输出路径是当前工作目录,而非输入文件所在的临时目录。当输入文件在临时目录、当前工作目录为其他位置时,Quarto会用相对路径解析输出位置,导致pandoc试图写入一个不存在的层级路径,最终报错。

两种解决方案

  1. 指定输出目录到临时目录
    修改quarto_render调用,新增output_dir参数明确输出位置:

    report <- quarto::quarto_render(
      input = template,
      output_format = output_format,
      output_file = output_file,
      output_dir = tmpdir  # 指定输出到临时目录
    )
    
  2. 切换工作目录到临时目录编译
    先切换到临时目录执行编译,完成后切回原工作目录:

    original_wd <- getwd()
    setwd(tmpdir)
    report <- quarto::quarto_render(
      input = "template.qmd",
      output_format = output_format,
      output_file = output_file
    )
    setwd(original_wd)
    

内容的提问来源于stack exchange,提问作者Rainer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 00:00:57