2

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  
landau
  • 5,636
  • 1
  • 22
  • 50
Jake
  • 196
  • 1
  • 18
  • Looks like your targets package version is high enough. Are you using the {targets} language engine instead of the {r} language engine for code chunk, e.g. {targets chunkname, tar_interactive = TRUE}? If you use the {r} language engine, Target Markdown will not work. – landau Sep 13 '21 at 20:00
  • Or try upgrading the knitr package to the latest version and then restarting your R session. – landau Sep 13 '21 at 20:07
  • I updated knitr to 1.34, but that does not seem to have helped. I am using the target engine - the only changes I've made from the template Rmarkdown file are to add tar_interactive = TRUE (or FALSE) as I've been troubleshooting. – Jake Sep 13 '21 at 21:14
  • How recently did you upgrade to targets 0.7.0? Have you restarted your R session since then? You may also have to call library(targets) before invoking the engine. – landau Sep 13 '21 at 21:27
  • I installed the package a few days ago and have restarted R, RStudio, and my computer multiple times while troubleshooting. – Jake Sep 13 '21 at 21:44
  • Unfortunately, there is not much more I can do to help this week. I do not have a computer with me, and I am about to go off the grid for the next 6 days. Maybe others can help troubleshoot. – landau Sep 13 '21 at 21:47
  • Now that I am back at a computer, I tried to reproduce the issue, and interactive Target Markdown appears to be working properly on my end. After you load `targets` in the document, what is the output of `knitr::knit_engines$get("targets")`? It should be `function (options) tar_engine_knitr(options)`, not `NULL`. – landau Sep 21 '21 at 00:48
  • I was also away from a computer for a while, so I wasn't able to check until today. I get the output you said I should, not null. I'm not sure if this is meaningful because I haven't used other types of chunks in Rmarkdown before, but the targets chunks don't have the gear symbol, the "run all chunks above" symbol, or the "run current chunk" symbol that the regular r chunks have. – Jake Sep 27 '21 at 17:50
  • 1
    I wonder if it’s an RStudio IDE issue. What happens if you set {targets …, tar_interactive = TRUE} in some code chunks and run the report with rmarkdown::render() outside the IDE, e.g. in the terminal? – landau Sep 27 '21 at 18:23
  • I set {tar_interactive = TRUE} for the chunks example-globals, raw-data, downstream-targets, and fit. Then running rmarkdown::render() both in the terminal and in the RStudio console worked. Running the render() function in the console resulted in the target objects from those aforementioned chunks being added to the global environment as well (i.e. I can call "fit" or "hist" and get the model fit and histogram) – Jake Sep 27 '21 at 18:53
  • Good news then. Does this solve most of the issue? The remaining mystery is why you don’t have the green play button next to each chunk in the RStudio IDE, right? – landau Sep 27 '21 at 18:56
  • Yes this does solve the issue, although it's a little less convenient than running each chunk individually, either way thank you for your help on this confusing issue. I've noticed the green play button doesn't appear if I include python chunks either when I'm working with both R and Python in other projects. It's not an issue per se, because I can still run the code in those chunk in other ways but it is odd. – Jake Sep 27 '21 at 19:17
  • Glad to hear we made progress. Unfortunately I cannot help with those remaining IDE issues. – landau Sep 27 '21 at 19:22

1 Answers1

1

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.

Jake
  • 196
  • 1
  • 18