solarchemist
solarchemist

Reputation: 468

How can I stop rmarkdown::render() from Rmd files inside project's renv library?

I have a Hugo blog which is compiled using the default blogdown::build_site() route.

I decided to give renv:: a try, because some of my posts are both plot-heavy and prone to depend on old package versions, so why not make sure future complete site rebuilds won't cause problems, right? With that in mind, I decided that the best way to use renv was to make each blog post (in /content/post/) its own renv project. Due to the global library caching used by renv, this shouldn't even cause much of a storage penalty.

Now the blog won't compile because rmarkdown::render() (which is called by build_site() for each blog post) seems to traverse the entire tree of each blog post looking for Rmd files, and of course discovering some inside the renv library folder, happily attempting to compile them -- and predictably failing.

So my question is

This problem seems trivial (we simply have to stop render() from traversing the /renv/ directory), but as far as I can tell, this interplay between blogdown and renv seems unexplored (I've found only some tangentially related questions, so I thought I could look for advice here first.

My site is built using Rscript -e "blogdown::build_site(local=FALSE)" (not using the button in RStudio). Once this script starts to render Rmd files inside the renv library tree, it eventually fails:

$ Rscript -e "blogdown::build_site(local=FALSE)"
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/blogdown/resources/2015-07-23-r-rmarkdown.Rmd
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/bookdown/rstudio/templates/project/resources/01-intro.Rmd
[WARNING] This document format requires a nonempty <title> element.
  Defaulting to '01-intro.utf8' as the title.
  To specify a title, use 'title' in metadata or --metadata title="...".
Warning message:
The label(s) intro, methods not found
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/bookdown/rstudio/templates/project/resources/02-literature.Rmd
[WARNING] This document format requires a nonempty <title> element.
  Defaulting to '02-literature.utf8' as the title.
  To specify a title, use 'title' in metadata or --metadata title="...".
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/bookdown/rstudio/templates/project/resources/03-method.Rmd
[WARNING] This document format requires a nonempty <title> element.
  Defaulting to '03-method.utf8' as the title.
  To specify a title, use 'title' in metadata or --metadata title="...".
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/bookdown/rstudio/templates/project/resources/04-application.Rmd
[WARNING] This document format requires a nonempty <title> element.
  Defaulting to '04-application.utf8' as the title.
  To specify a title, use 'title' in metadata or --metadata title="...".
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/bookdown/rstudio/templates/project/resources/05-summary.Rmd
[WARNING] This document format requires a nonempty <title> element.
  Defaulting to '05-summary.utf8' as the title.
  To specify a title, use 'title' in metadata or --metadata title="...".
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/bookdown/rstudio/templates/project/resources/06-references.Rmd
[WARNING] This document format requires a nonempty <title> element.
  Defaulting to '06-references.utf8' as the title.
  To specify a title, use 'title' in metadata or --metadata title="...".
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/bookdown/rstudio/templates/project/resources/index.Rmd
Rendering content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/cli/doc/pluralization.Rmd
Quitting from lines 15-15 (./../man/chunks/pluralization.Rmd)
Error in file(con, "r") : cannot open the connection
Calls: local ... call_block -> lapply -> FUN -> knit -> readLines -> file
In addition: Warning message:
In file(con, "r") :
  cannot open file './../man/chunks/pluralization.Rmd': No such file or directory
Execution halted
Error in render_page(f) :
  Failed to render 'content/post/2016-11-18-notes-inorganic-chemistry/renv/library/R-3.6/x86_64-pc-linux-gnu/cli/doc/pluralization.Rmd'
Calls: <Anonymous> -> build_rmds -> render_page
Execution halted

Here's my sessioninfo:

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

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

loaded via a namespace (and not attached):
 [1] compiler_3.6.2  htmltools_0.4.0 tools_3.6.2     rstudioapi_0.11
 [5] yaml_2.2.1      Rcpp_1.0.4.6    rmarkdown_2.1   knitr_1.28     
 [9] digest_0.6.25   xfun_0.13       packrat_0.5.0   rlang_0.4.5    
[13] renv_0.9.3      evaluate_0.14  

Upvotes: 3

Views: 508

Answers (1)

solarchemist
solarchemist

Reputation: 468

As for the second part of my question, the blogdown::build_site() source code provides clear answers.

build_site() calls list_rmds(), which does the following (copied verbatim from source, easy enough to follow along):

list_rmds = function(dir, check = FALSE) {
  files = list.files(dir, rmd_pattern, recursive = TRUE, full.names = TRUE)
  # exclude Rmd that starts with _ (preserve these names for, e.g., child docs)
  # but include _index.Rmd/.md
  files = files[!grepl('^_', basename(files)) | grepl('^_index[.]', basename(files))]
  # do not allow special characters in filenames so dependency names are more
  # predictable, e.g. foo_files/
  if (check) bookdown:::check_special_chars(files)
  files
}

As can be seen it lists all Rmd files, recursively, and then excludes Rmd files beginning with _.

Based on this, it is obvious why renv:: renders Rmd files inside the renv directory. It's also clear that using packrat:: instead wouldn't make any difference.

A way around this is of course to use a custom site build script, as mentioned in the blogdown docs, but I still wonder if the combination of blogdown and renv should not be supported by the default approach (assuming other people like to put each blogpost in its own renv project).

Upvotes: 4

Related Questions