Is it possible to create a self-rendering Rmarkdown document?

You can treat an Rmd file as an Rscript. For instance, assume that your Rmd file looks like this

---
title: "Untitled"
author: "ekoam"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

You can then prepend the following code to that Rmd file

#!/usr/bin/env Rscript

args <- commandArgs()
fname <- normalizePath(sub("--file=", "", args[grepl("--file=", args)]))
thisfile <- readLines(fname)
newfname <- paste0(tempdir(), "/", basename(fname))
writeLines(thisfile[-1:-which(thisfile == "q(\"no\")")], newfname)
rmarkdown::render(newfname, output_dir = dirname(fname))
q("no")

The trick here is q("no"). This line terminates the R session, and, thus, whatever written after it will be ignored. Such an effect also means high flexibility for coding because you can write almost any valid R code before that q("no"). The code above simply creates another temporary Rmd file with the same content as what is after q("no"). Then, we rmarkdown::render that temporary file and dump the output to the current directory.

The complete Rmd file looks like this

#!/usr/bin/env Rscript

args <- commandArgs()
fname <- normalizePath(sub("--file=", "", args[grepl("--file=", args)]))
thisfile <- readLines(fname)
newfname <- paste0(tempdir(), "/", basename(fname))
writeLines(thisfile[-1:-which(thisfile == "q(\"no\")")], newfname)
rmarkdown::render(newfname, output_dir = dirname(fname))
q("no")

---
title: "Untitled"
author: "ekoam"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Disclaimer: I do not know much about Linux, nor do I have a Linux machine with R installed available to test my answer. Let me know if this does not work properly. If this inspires someone to write a better answer, I'll happily delete this one.


Let me approach this backwards:

  • To knit a document, we need to call rmarkdown::render().
  • To run this command from a command line, we can use RScript -e "rmarkdown::render('document.Rmd')", see e.g. here.
  • Probably you do not want to hardcode the path to Rscript but use a shebang like #!/usr/bin/env Rscript instead.
  • The problem with this approach is that it does not allow to pass arguments to Rscript.
  • I therefore propose to use a "wrapper" in the shebang that is responsible for calling Rscript.
  • With the name of the RMD file as argument, the required R code becomes rmarkdown::render(commandArgs(trailingOnly=TRUE)[1]).

Step by step:

  1. Create the wraper (in any directory that is in your $PATH) and make it executable:

     touch KnitWrapper.sh
     chmod +x KnitWrapper.sh
    
  2. Write the following to KnitWrapper.sh:

     #!/usr/bin/env bash
     Rscript -e "rmarkdown::render(commandArgs(trailingOnly=TRUE)[1])" $1
    
  3. Make sure Rscript is in your $PATH. If you update R, update your $PATH accordingly.

  4. In your RMD file, add the shebang #!/usr/bin/env KnitWrapper.sh.

  5. Make your RMD file executable and run it:

     chmod +x yourDocument.Rmd
     ./yourDocument.Rmd
    

Unfortunately, the above is largely untested. Let me know if it does not work, so I can either fix or delete this answer.