Prevent Rmarkdown document from creating any output file

Is there a way to prevent Rmarkdown from creating any output file when knitted? For example,


produces when I omit the output: argument from the YAML header (or when I set it to NULL).

I want it to run all the code chunks in my_file.Rmd without generating an output file. Is that possible?

If you don't want output file, I suppose you want to exectute the Rmd file as an R script and have all the object created in your rsession ? Where do you want the output of the chunk to be printed / stored ?

One way that could be of interest to you would be to convert your Rmd file to an R script that you would source as any other R script

1 Like

Hi @cderv,

Thanks so much for the response. Yes, I suppose I want to have my cake and eat it too. I prefer working with/developing my code in Rmd files over R scripts (not talking about writing packages or functions here. Talking about analysis projects, e.g., data_01_import.Rmd, data_02_merge.Rmd, data_03_clean.Rmd, analysis_01_descriptive.Rmd, etc.). Once they are developed, however, it would sometimes useful to be able to just execute them as an R script (i.e., source(my_file.Rmd) -- which, of course, doesn't work).

In the past, I've tried maintaining an Rmd file for interactive experimentation/data checking and manually tried to mirror key elements of the Rmd file in a separate R script. But, maintaining two files is obviously not ideal. I was totally unaware of knitr::purl(), so thank you very much for pointing me to that!!

I suppose that in a perfect world I would still prefer something equivalent to souce("my_file.rmd"), but knitr::purl() seems like the next best thing.

Thanks again!

Sourcing a Rmd file is just a wrapper away from you. This should work :

source_rmd = function(file, ...) {
  tmp_file = tempfile(fileext=".R")
  on.exit(unlink(tmp_file), add = TRUE)
  knitr::purl(file, output=tmp_file)
  source(file = tmp_file, ...)

That is why I advice knitr::purl, not for you to maintain two files.

Would that work for you ?


I haven't tried it yet, @cderv, but it looks like it should be a really helpful solution. Thanks again!

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.