Search code examples
r-markdownhugokableblogdown

Correct way to batch render Rmarkdown files for use with Hugo


I am trying to render a bunch of .html pages from .Rmd by using rmarkdown::render() on a template .Rmd and pass it some variable through a loop.

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

The template is set up to expect the variable and renders fine. However, other .Rmd pages in other parts of the site no longer render properly if I generate my .html files then run blogdown::serve_site()

The .html files rendered by blogdown::serve_site() seem to have incorrect headers and missing html parts when rendered automatically by blogdown. The generated html page still retains the yaml header components at the top of the file.

Eg:

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

If I don't batch generate my files, and simply run serve_site() on the existing site files then I get no issue. My suspicion is that running rmarkdown::render() changes some session parameter, but I have no idea. My aim is to create the necessary pages myself and then have blogdown render the rest upon serve_site().

Does anyone have any idea what the issue is?


Solution

  • So, I worked out how to generate the correct file outputs. My issue was borne of an ignorance of how blogdown works.

    Essentially, when serving site, blogdown does the following:

    1. Locate .Rmd files
    2. Covert to .html files formatted for HUGO
    3. HUGO serves the final .html file to its location in /public

    What I was doing was skipping these steps and creating a final rendered .html file through rmarkdown::render() thus creating files that were incompatible with the theming actions carried out by HUGO before serving the final .html.

    Instead of manually rendering .Rmd files through a loop and passing it variables, I edit lines of the template and write the output as individual .Rmd files:

    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)
    }