0

I would like to be able to create a folder to store my Markdown document in from within R Markdown.

title: thetitle
author: myinititals
date: "`r Sys.Date()`"
knit: (function(inputFile, encoding) { 
          out_dir <- 'test';
          rmarkdown::render(inputFile,
                            encoding=encoding, 
                            output_file=file.path(dirname(inputFile), out_dir, 
'analysis.docx')) 
})

The problem is that it only works if the folder already exists. Is there a way to initialise a folder from within Rmarkdown.

Tom
  • 2,173
  • 1
  • 17
  • 44

1 Answers1

1

As far as I get it you could achieve your desired result by passing the path for the output file via output_dir argument instead of wrangling it inside output_file. Doing so will automatically create the output directory if it doesn't exist:

---
title: thetitle
author: myinititals
date: "`r Sys.Date()`"
knit: (function(inputFile, encoding) { 
          out_dir <- 'test';
          rmarkdown::render(inputFile,
                            encoding=encoding, 
                            output_dir = file.path(dirname(inputFile), out_dir), 
                            output_file='analysis.docx') 
  })
---
stefan
  • 90,330
  • 6
  • 25
  • 51
  • I have been trying to combine this answer with automatically naming files, but I have not been successful (so I put a bounty on it). In case you are interested: https://stackoverflow.com/questions/71751149/initialise-output-location-outside-of-rmarkdownrender – Tom May 03 '22 at 10:03