rr-markdowntargettargets-r-package

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


I am attempting to use the interactive Markdown functionality provided by the targets package in R (more information here: https://books.ropensci.org/targets/markdown.html). I'm using the template R Markdown file, which builds a pipeline for analyzing the airquality dataset. In section 3.6 Globals of the manual, it says that I can run the some-globals target chunk with tar_interactive = TRUE and should get the message: #> Run code and assign objects to the environment. However, this message does not appear (no message appears).

When following section 3.7 Target definitions, a similar issue occurs. When I run the raw-data chunk with interactive mode on (tar_interactive = TRUE), the manual says that the target's R command runs, some checks are performed, and the same message (#> Run code and assign objects to the environment) should appear. Furthermore, it says that the return value is available in memory and an ordinary R code chunk can read said object. The example provided is an R code chunk with head(raw_data). Again, the message Run code and assign... does not appear when I run the raw-data chunk and no object raw_data is available in memory for head(raw_data) to read. Instead, this is the output that shows up in the R console in R Studio when I run the raw-data chunk:

> 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

So the interactive mode does not appear to be working as described in the manual. I am able to knit the template document successfully so long as all the targets chunks are set with tar_interactive = FALSE. At first I thought that maybe I had to knit the document in the non-interactive mode and then, because the targets would be in the cache set up by targets, I could access the objects in the interactive mode, but that does not work either (and if it did work, it seems it would defeat the purpose of prototyping the pipeline in interactive mode first). Am I missing something?

> 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  

Solution

  • My issue was caused because at some point I had unchecked "Show output inline for all R Markdown documents" in the Tools > Global Options > R Markdown menu, which had removed the little green "Run" arrow from the non-r engine code chunks. I had resorted to running each line of code one-by-one. This did not properly use the targets engine. Rechecking that box returned the run arrow and clicking the run arrow allowed the targets engine to properly run the code and return the expected output.

    User landau helped me identify that calling rmarkdown::render() in the console would serve to run the target chunks using the targets engine in the absence of the green run arrow.