【问题标题】:How do you use the interactive R Markdown abilities of the `targets` package您如何使用“targets”包的交互式 R Markdown 功能
【发布时间】:2021-09-13 17:47:54
【问题描述】:

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

当遵循 3.7 目标定义节时,会出现类似的问题。当我在 (tar_interactive = TRUE) 上以交互模式运行 raw-data 块时,手册说目标的 R 命令运行,执行了一些检查,并且应该出现相同的消息 (#> Run code and assign objects to the environment)。此外,它表示返回值在内存中可用,并且普通的 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  

【问题讨论】:

  • 看起来您的目标包版本足够高。您是否使用 {targets} 语言引擎而不是 {r} 语言引擎来处理代码块,例如{目标块名,tar_interactive = TRUE}?如果您使用 {r} 语言引擎,Target Markdown 将不起作用。
  • 或者尝试将 knitr 包升级到最新版本,然后重新启动您的 R 会话。
  • 我将 knitr 更新到 1.34,但这似乎没有帮助。我正在使用目标引擎 - 我对模板 Rmarkdown 文件所做的唯一更改是添加 tar_interactive = TRUE(或 FALSE),因为我一直在进行故障排除。
  • 您最近多久升级到目标 0.7.0?从那以后您是否重新启动了 R 会话?您可能还必须在调用引擎之前调用 library(targets)。
  • 我想知道这是否是 RStudio IDE 问题。如果您在某些代码块中设置 {targets ..., tar_interactive = TRUE} 并在 IDE 外部使用 rmarkdown::render() 运行报告会发生什么,例如在终端?

标签: r r-markdown target targets-r-package


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多