Can RMarkdown output file name be made to depend on a parameter in the YAML header?

I've created a document with a parameter called solutions that can be either true or false. I can use this to control whether solutions are displayed to the exercises in the document. This is all working just fine, but I'd also like the HTML file that is rendered to have a different name depending on the value of this parameter. If my file is named foo.Rmd, I'd like the output to be either foo.html or foo-sols.html depending on the value of the parameter. But I can't seem to get that to work.

Is there an easy way to do this that I'm missing?

When you render the document, you could use something like the code below to set the output document name based on whether the output includes the solutions or not. (The code is from my answer to a Stack Overflow question.) The parameter soln can be either TRUE or FALSE. This determines whether the rmarkdown document is rendered, respectively, with or without solutions, and also sets the output file name accordingly.

for (i in c(TRUE, FALSE)) {
  rmarkdown::render("hw.Rmd", 
                    params = list(soln = i),
                    output_file=ifelse(i, "Solutions.doc", "Homework.doc"))
}
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.