Repository navigation
Description
The need
A package I maintain writes a small record beside each rendered document while it is being knitted. It needs to know which output file the render will produce, so that renders of one source to different outputs keep separate records. Nothing public tells code inside the knit what that file is. The only way I have found is to search the call stack for render()'s frame and read its local variable output_file, which relies on a private detail of render().
Example
report.Rmd:
---
title: "Where am I going?"
output: html_document
---
```{r}
# today: find render()'s frame and read its local variable
from_frame <- NULL
for (i in rev(seq_len(sys.nframe()))) {
if (identical(sys.function(i), rmarkdown::render)) {
from_frame <- get("output_file", envir = sys.frame(i))
break
}
}
from_frame
# proposed
knitr::opts_knit$get("rmarkdown.output_file")
```rmarkdown::render("report.Rmd", output_file = "custom-name", output_dir = tempdir())knitr::current_input() gives the input and rmarkdown.output_dir gives the folder, but nothing gives the file.
What I looked for
rmarkdown::render(output_dir=)should be taken into account ofknitr::include_graphics()#2473 and default relative path processing ininclude_graphics()can create broken path yihui/knitr#2171 asked for knitr to know the output folder. Commit 43697f0 addedrmarkdown.output_dir, which knitr now reads. This proposal follows that pattern.- set rmarkdown context into knit_opts during render #32 first put
rmarkdown.pandoc.toand others intoopts_knit. - Allow
output_fileto be a function inrender()[FR] #2314 shows the name must be settled before the knit, becausefig.pathand the_filesfolder depend on it. - rmarkdown::render with output_format=html_notebook mangles output file name #1180:
html_notebook()renames its output to.nb.htmlafter Pandoc. - knitr has no such option. Quarto gives its engines
output-fileinQUARTO_EXECUTE_INFO.
I found no issue asking for the output file itself.
Proposal
render() sets rmarkdown.output_file beside rmarkdown.output_dir, before the knit. Its value is the absolute path of the file Pandoc is asked to write, after output_file, output_dir and the extension are applied: the path render() returns, unless a post-processor renames it (html_notebook()). It is restored with the other opts_knit values when render() exits. With several formats, each knit sees its own file. With run_pandoc = FALSE it holds the path render() would have written.
The patch is one line in render(), a sentence in the output_file documentation, a NEWS item and a test. A PR is ready if you agree with the shape.
Alternatives considered
- An exported function such as
rmarkdown::output_file(): more API, and the other render facts already live inopts_knit. - An environment variable, as Quarto uses: it leaks into child processes and is harder to restore after nested renders.
- The base name only: the absolute path matches what
render()returns.
Activity
vorpalvorpal commentedon Nov 13, 2026
A related gap, in case it is easier to consider the two together: the input file.
When the input's name holds a shell character,
render()knits a copy with those characters replaced (a b.Rmdis knitted asa-b.Rmd), so inside the knitknitr::current_input()names the copy. The file the author actually named is kept only inrender()'s local variableoriginal_input. Code that records which source a document was rendered from (my case again) has to findrender()'s frame to read it.The same pattern would cover it:
rmarkdown.input_fileinopts_knit, set tooriginal_input, besidermarkdown.output_dirand the proposedrmarkdown.output_file. It is one more entry in the sameopts_knit$set()call. This one matters less often than the output file, so I am happy for it to be dropped if you would rather keep the change to one option.