3

我有一个使用默认blogdown::build_site()路由编译的 Hugo 博客。

我决定renv::试一试,因为我的一些帖子既情节重又容易依赖旧的软件包版本,所以为什么不确保将来完整的站点重建不会造成问题,对吧?考虑到这一点,我决定最好的使用方法renv是让每篇博文(在 中/content/post/)成为自己的renv项目。由于 使用的全局库缓存renv,这甚至不会造成太多的存储损失。

现在博客将无法编译,因为rmarkdown::render()build_site()每篇博客文章都会调用它)似乎遍历每篇博客文章的整个树以查找 Rmd 文件,当然在renv库文件夹中发现了一些文件,并愉快地尝试编译它们——并且可以预见地失败。

所以我的问题是

  • 将每篇博客文章置于自己的renv环境中是否违反了惯例(我才刚刚开始学习 renv 和朋友们)?
  • 如果不是,为什么build_site()不玩renv好呢?我错过了什么吗?还是我发现了一个真正的问题?还是我应该放手并切换到手动 blogdown 站点构建方法

这个问题似乎微不足道(我们只需要停止render()遍历/renv/目录),但据我所知,blogdown 和 renv 之间的这种相互作用似乎尚未探索(我只发现了一些切线相关的 问题,所以我想我可以寻找建议先在这里。

我的网站是使用Rscript -e "blogdown::build_site(local=FALSE)"(不使用 RStudio 中的按钮)构建的。一旦这个脚本开始在renv库树中渲染 Rmd 文件,它最终会失败:

$ 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

这是我的会话信息:

> 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  
4

1 回答 1

4

至于我的问题的第二部分,blogdown::build_site() 源代码提供了明确的答案。

build_site()调用list_rmds(),它执行以下操作(从源逐字复制,很容易理解):

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
}

可以看出,它递归地列出所有Rmd 文件,然后排除以 . 开头的 Rmd 文件_

基于此,很明显为什么要renv::在 renv 目录中渲染 Rmd 文件。同样清楚的是,使用packrat::instead 不会有任何区别。

解决此问题的一种方法当然是使用自定义站点构建脚本,如 blogdown 文档中所述,但我仍然想知道默认方法是否不应该支持 blogdown 和 renv 的组合(假设其他人喜欢将每个 blogpost在它自己的 renv 项目中)。

于 2020-04-21T21:29:56.587 回答