Skip to content

[FR] Expose the output file path during the knit, as opts_knit rmarkdown.output_file #2648

Open

Description

@vorpalvorpal

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

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 in opts_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

  1. vorpalvorpal commented on Nov 13, 2026

    @vorpalvorpal
    Author

    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.Rmd is knitted as a-b.Rmd), so inside the knit knitr::current_input() names the copy. The file the author actually named is kept only in render()'s local variable original_input. Code that records which source a document was rendered from (my case again) has to find render()'s frame to read it.

    The same pattern would cover it: rmarkdown.input_file in opts_knit, set to original_input, beside rmarkdown.output_dir and the proposed rmarkdown.output_file. It is one more entry in the same opts_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions