R Markdown how to make parameter optional

Is there a way to make a parameter optional in R Markdown when using knitr? Below is a stand-alone example, my simple attempt to omit a parameter, and the error.

Example RMD file:

---
title: "`r params$name`"
date: \`r format(Sys.Date(), "%B %d, %Y")`\
output:
  html_document:
    toc: true
params:
    A: NA
    B: NA
    C: NA
--- 
# Header-1

`r params$A` 
`r params$B`
`r params$C`

Example run script:

render(input = "mytemplate.Rmd", 
       output_file = "fileout.html", 
       params = list(
         name = 'Bob', 
         A = 'turkey', 
         B = 'otter'
         )
       )

Resulting error when omitting "C":

Error in knitr::include_graphics(params$C) : 
  Cannot find the file(s): NA


Read more here: https://stackoverflow.com/questions/67479791/r-markdown-how-to-make-parameter-optional

Content Attribution

This content was originally published by a11 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: