在Targets工作流中使用带标签的代码块时,Quarto会遇到重复的代码块标签

前端开发 2026-07-08

我在我的 targets 工作流中使用 tar_quarto() 来创建一个报告,但无论是Quarto还是Knitr都会报错,提示在 .qmd 文件中存在重复的代码块标签。其实我只有一个标签,肯定不是重复的。我应该如何解决?这是一个bug吗?

下面给出一个更简化、可复现的示例。

为了我的可复现示例,我在一台运行macOS Tahoe 26.5.1、搭载ARM Apple Silicon芯片的Apple电脑上使用 r 4.60targets 1.12.0modelsummary 2.6.0tarchetypes 0.14.1quarto 1.5.1。原始问题发生在一台运行Windows的远程桌面服务器上,使用相同的 r 和软件包版本。

targets文件:

# Load packages
library(targets)
library(tarchetypes)
# Target options:
tar_option_set(
  packages = c("tibble", "modelsummary")
)
# Target list
list(
  tar_target(
    name = data,
    command = tibble(x = rnorm(100), y = rnorm(100))
  ),
  tar_target(
    name = model,
    command = lm(y ~ x, data = data)
  ),
  tar_quarto(
    name = report,
    path = "report.qmd",
    quiet = FALSE
  )
)

The .qmd 文件:

--- 
title: "Duplicate chunk label where there is none" 
format: html
--- 
The following creates an error when embedded within a targets project.

```{r}

library(targets) 
tar_load_globals() 
tar_load(model) 

Table @tbl-modelsummary shows a model summary

#| label: tbl-modelsummary
#| tbl-cap: "模型摘要" modelsummary(model) 

带有回溯信息的错误消息:

 tar_source(): these files do not exist: R
 + report dispatched


 processing file: report.qmd
 1/4                   
 2/4 [unnamed-chunk-1] 
 Error:
 ! Could not parse knitr report report.qmd to detect dependencies: Duplicate chunk label 'tbl-modelsummary', which has been used for the

chunk: modelsummary(model)

 Quitting from report.qmd:7-12 [unnamed-chunk-1]
 Execution halted
 ✖ report errored
 ✖ errored pipeline [1.5s, 0 completed, 2 skipped]
 Error:
 ! Error in tar_make():
   ! Error running quarto CLI from R.
 Caused by error:
 ✖ Error returned by quarto CLI.
   -----------------------------

processing file: report.qmd 1/4 2/4 [unnamed-chunk-1] Error:
! Could not parse knitr report report.qmd to detect dependencies: Duplicate chunk label 'tbl-modelsummary', which has been used for the chunk: modelsummary(model) Quitting from report.qmd:7-12 [unnamed-chunk-1] Execution halted Caused by error in processx::run(): ! System command 'quarto' failed See https://books.ropensci.org/targets/debugging.html

Hide Traceback ▆ 1. └─targets::tar_make() 2. └─targets:::callr_outer(...) 3. ├─targets:::if_any(...) 4. └─targets:::callr_error(traced_condition = out, fun = fun) 5. └─targets::tar_throw_run(message, class = class(traced_condition$condition)) 6. └─targets::tar_error(...) 7. └─rlang::abort(message = message, class = class, call = tar_envir_base)


## 解决方案

我在几个项目中也遇到过这个问题,并找到了一个变通方法。请在 `.qmd` 文件的顶部添加以下代码块,以允许重复的代码块标签。

#| include: false
options(knitr.duplicate.label = "allow")

```

另一种,可能不那么理想的变通方法,是移除所有的代码块标签。

一个观察:在macOS上使用最新版的软件包时,我能够重现此问题,而在Arch Linux上则不会重现。

Kenji,方便在 https://github.com/ropensci/targets/issues 上提交一个错误报告吗?你可以直接使用你帖子中的示例来作为报告的材料。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章