Rmarkdown Directing Output File into a Directory

Rmarkdown directing output file into a directory

You could try setting the out_dir variable in the function you are giving knit to render:

knit: (function(inputFile, encoding) { 
out_dir <- 'test';
rmarkdown::render(inputFile,
encoding=encoding,
output_file=file.path(dirname(inputFile), out_dir, 'analysis.html')) })

R: In RStudio how do I make knitr output to a different folder to avoid cluttering up my drive?

As Eric pointed out in the comments, if you're willing to forego the convenience of the Knit HTML button (which produces HTML files that live alongside your .Rmd), you can just call rmarkdown::render directly.

However, if you really need to customize your workflow, you can override the Knit HTML button to run whatever command you via the rstudio.markdownToHTML option. This command could invoke rmarkdown with specific options (such as output directory) and perform other pre- or post-processing tasks. Documentation here:

https://support.rstudio.com/hc/en-us/articles/200552186-Customizing-Markdown-Rendering

Note that setting the rstudio.markdownToHTML option will turn off some of the newer RMarkdown V2 integration features baked into RStudio, since RStudio will no longer be able to infer what engine is being used to render the document.

specifying output path for knit2html

First, thanks for the very clear and reproducible question. If you take a look at the knit2html function source code, you can understand what the problem is :

R> knit2html
function (input, ..., envir = parent.frame(), text = NULL, quiet = FALSE,
encoding = getOption("encoding"))
{
if (is.null(text)) {
out = knit(input, envir = envir, encoding = encoding,
quiet = quiet)
markdown::markdownToHTML(out, outfile <- sub_ext(out,
"html"), ...)
invisible(outfile)
}
else {
out = knit(text = text, envir = envir, encoding = encoding,
quiet = quiet)
markdown::markdownToHTML(text = out, ...)
}
}
<environment: namespace:knitr>

If the text argument is NULL (ie, if you provide a file as input instead of a character vector), then the given file is passed to the knit function, and the markdownToHTML function is called the following way :

markdown::markdownToHTML(out, outfile <- sub_ext(out, "html"), ...)

So in this case the output file name is generated by substituting the existing file name extension with html, and you can't provide your own output filename as an argument.

Rmarkdown does not follow same paths as R script and console commands

Try something that looks like this as I am not sure of the nature of your `R Markdown.

test <- readRDS(here::here("data/test_data.rds"))

The bottom line is to use the here function from the here package.



Related Topics



Leave a reply



Submit