批量渲染 Rmarkdown 文件以与 Hugo 一起使用的正确方法

Correct way to batch render Rmarkdown files for use with Hugo

我正在尝试通过在模板 .Rmd 上使用 rmarkdown::render().Rmd 渲染一堆 .html 页面,并通过循环将一些变量传递给它。

for (i in some_vector){
  rmarkdown::render(my_template.Rmd,
                    output_format = "html_document",
                    output_file = ... ,
                    output_dir = ... )
}

模板设置为期望变量并呈现良好。但是,如果我生成我的 .html 文件然后 运行 blogdown::serve_site()

,网站其他部分的其他 .Rmd 页面将不再正确呈现

blogdown::serve_site() 呈现的 .html 文件在 blogdown 自动呈现时似乎有不正确的 header 和缺失的 html 部分。生成的 html 页面仍然保留文件顶部的 yaml header 组件。

例如:

---
title = 
date = 
layout = 
author =
--- 

如果我不批量生成我的文件,而只是在现有站点文件上 运行 serve_site() ,那么我不会遇到任何问题。我怀疑 运行ning rmarkdown::render() 改变了一些 session 参数,但我不知道。我的目标是自己创建必要的页面,然后让 blogdown 在 serve_site() 上呈现其余页面。

有人知道问题出在哪里吗?

所以,我弄清楚了如何生成正确的文件输出。我的问题是由于对 blogdown 的工作原理一无所知。

基本上,在为网站提供服务时,blogdown 会执行以下操作:

  1. 找到 .Rmd 个文件
  2. 转换为 .html 格式的 HUGO 文件
  3. HUGO 将最终的 .html 文件提供到它在 /public
  4. 中的位置

我所做的是跳过这些步骤并通过 rmarkdown::render() 创建最终渲染的 .html 文件,从而创建与 HUGO 在服务最终 [=] 之前执行的主题操作不兼容的文件12=]。

我没有通过循环手动渲染 .Rmd 文件并向其传递变量,而是编辑模板的行并将输出写为单独的 .Rmd 文件:

file.src <- file("~/R/sample_proforma_2.Rmd", 
                 open = "r")
file.lines <- readLines(file.src)
rmd.list <- list()
for(i in c("sample_117" ,"sample_118", "sample_119", "sample_121")){
  tmp.lines <- file.lines
  tmp.lines[4] <- gsub("empty\+title", gsub("_", " ", toupper(i)), tmp.lines[4])
  tmp.lines[6] <- gsub("empty\+title", gsub("_", " ", toupper(i)), tmp.lines[6])
  tmp.lines[9] <- gsub("empty\+circos", toupper(i), tmp.lines[9])
  tmp.lines[15] <- gsub("empty\+maf", i, tmp.lines[15])
  tmp.lines[21] <- gsub("empty\+tag", i, c("- CIRCOS\n- MAF"))
  rmd.list[[i]] <- tmp.lines
}

for (i in names(rmd.list)){
  write.table(rmd.list[[i]],
              paste0("~/content/sample/",i,".Rmd"),
              sep = "",
              quote = FALSE,
              col.names = FALSE,
              row.names = FALSE)
}

我为同样的问题苦苦挣扎了几个小时。我想从大约 10 个模板中生成数千个单独的页面(使用 R、Blogdown 和 Hugo)并编写所有内容的脚本。

这是有效的方法:

  1. 让您的自定义 R/build.R 循环生成 markdown (.md) 页面。
  2. 让 Hugo 将 markdown (.md) 文件转换为 HTML
  3. 使用 knitr::knit,而不是 rmarkdown:render,将 Rmarkdown 模板 (.Rmd) 处理为 markdown (.md)
  4. 使用 knit() 以正确的相对 URL 将图像放到正确的位置的唯一方法是 a) 设置 output = 'dest/dir/file_name.md',b) 使用 fig.path='figure'(或任何你喜欢的)和 c ) 在每个 knit() 之后将 fig.path 目录移动到 dest/dir/figure 下。

否则 knit() 要么将图像放在 ROOT/figure 下,要么在 .md 文件中创建一个相对图像链接,例如 dest/dir/figure/plot.png 已经在 dest/dir/ 下的页面.

我的R/build.R.

updates <- c( '6.3', '6.4', '6.5');
library('knitr')

dir.create(file.path('content/', 'update'), showWarnings = FALSE);

for (update in updates) {
  dir_name <- paste0('content/update/', update);
  dir.create(file.path(dir_name), showWarnings = FALSE);
  knit(input = 'R/update.rmd', output = paste0(dir_name, '/_index.md'));
  ## THIS IS REQUIRED AFTER EVERY knit()!!! 
  ## otherwise the image links go wrong and/or the next knit() may overwrite the previous plots

  file.rename('figure', paste0(dir_name, '/figure'));    
}

R/update.rmd:

---
title: Version `r update`
date: `r Sys.Date()`
author: Jylpah
output: md_document
---
This is a test. Version is `r update`. 

```{r, plot, echo=FALSE, fig.path='figure/'}
A <- sample(10,50,replace = TRUE);
hist(A);
```

## Something else

Another plot

```{r, plot_another, echo=FALSE, fig.path='figure/'}
B <- sample(10,50,replace = TRUE);
hist(B);
```

我希望以上内容在某种程度上是可以理解的,并对您和其他人有所帮助。