你如何使用 `targets` 包的交互式 R Markdown 功能

How do you use the interactive R Markdown abilities of the `targets` package

我正在尝试使用 R 中 targets 包提供的交互式 Markdown 功能(更多信息,请点击此处:https://books.ropensci.org/targets/markdown.html)。我正在使用模板 R Markdown 文件,它构建了一个用于分析 airquality 数据集的管道。在手册的 3.6 Globals 节中,它说我可以 运行 some-globals 目标块 tar_interactive = TRUE 并且应该得到消息:#> Run code and assign objects to the environment。但是,这条消息没有出现(no message appears)。

在遵循 3.7 目标定义 部分时,会出现类似的问题。当我 运行 具有交互模式的 raw-data 块(tar_interactive = TRUE)时,手册说目标的 R 命令 运行s,执行了一些检查,并显示相同的消息(#> Run code and assign objects to the environment) 应该出现。此外,它表示 return 值在内存中可用,普通的 R 代码块可以读取该对象。提供的示例是带有 head(raw_data) 的 R 代码块。同样,当我 运行 raw-data 块并且内存中没有对象 raw_data 可供 head(raw_data) 读取时,消息 Run code and assign... 不会出现。相反,这是当我 运行 raw-data 块:

时显示在 R Studio 的 R 控制台中的输出
> tar_target(raw_data, airquality)
<tar_stem> 
  name: raw_data 
  command:
    airquality 
  format: rds 
  iteration method: vector 
  error mode: stop 
  memory mode: persistent 
  storage mode: main 
  retrieval mode: main 
  deployment mode: worker 
  priority: 0 
  resources:
    list() 
  cue:
    mode: thorough
    command: TRUE
    depend: TRUE
    format: TRUE
    iteration: TRUE
    file: TRUE 
  packages:
    biglm
    dplyr
    ggplot2
    readr
    tidyr 
  library:
    NULL

所以交互模式似乎没有按照手册中的描述工作。只要所有目标块都设置为 tar_interactive = FALSE,我就能够成功编织模板文档。起初我想也许我必须在非交互模式下编织文档然后,因为目标将在 targets 设置的缓存中,我可以在交互模式下访问对象,但是那也不起作用(如果它确实起作用,它似乎会破坏首先在交互模式下对管道进行原型设计的目的)。我错过了什么吗?

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] targets_0.7.0

loaded via a namespace (and not attached):
 [1] igraph_1.2.6      knitr_1.30        magrittr_2.0.1    tidyselect_1.1.0  munsell_0.5.0     colorspace_1.4-1 
 [7] R6_2.5.0          rlang_0.4.10      fansi_0.4.2       dplyr_1.0.7       tools_4.0.3       grid_4.0.3       
[13] data.table_1.13.6 gtable_0.3.0      xfun_0.21         utf8_1.1.4        cli_2.5.0         DBI_1.1.0        
[19] withr_2.4.2       ellipsis_0.3.2    yaml_2.2.1        digest_0.6.27     assertthat_0.2.1  tibble_3.1.2     
[25] lifecycle_1.0.0   crayon_1.4.1      processx_3.5.2    purrr_0.3.4       callr_3.7.0       ggplot2_3.3.5    
[31] codetools_0.2-16  ps_1.5.0          vctrs_0.3.8       glue_1.4.2        compiler_4.0.3    pillar_1.6.1     
[37] generics_0.1.0    scales_1.1.1      pkgconfig_2.0.3  

我的问题是因为在某些时候我在工具 > 全局选项 > R Markdown 菜单中取消选中“显示所有 R Markdown 文档的内联输出”,这已经删除了小绿色“运行”来自非 r 引擎代码块的箭头。我已经 运行 一行一行地使用每一行代码。这没有正确使用目标引擎。重新选中该框 return 编辑 运行 箭头并单击 运行 箭头允许目标引擎正确地 运行 代码和 return 预期输出。

用户 landau 帮助我确定在没有绿色 运行 箭头的情况下,在控制台中调用 rmarkdown::render() 将用于 运行 使用目标引擎的目标块。