Find the name of the current file being rendered in Quarto

I use Quarto to generate docx files. I like to include a section at the end of the qmd file that contains useful information (the Quarto version, the R version, the date and time of rendering, machine information). This information is useful later when I need to revisit the job. When I previously used RMarkdown, I was also able to extract the name of the Rmd file that was being processed, using a trick from someone (and I don't remember where...), like this (in the Rmd file itself):

This RMarkdown document has the filename r gsub(",.*$","",gsub("rmarkdown::render(","",commandArgs()[6],fixed=TRUE)).

For Quarto, the same trick does not work. Does anyone know any way to find out the name of the qmd file that is being rendered?

The quarto package can give some useful information, but not the rendering file.

Thanks
Stephen

I don't think we have the information stored and available from within Quarto - Maybe we could make this available in Variables system (Quarto – Variables)

The idea is tracked in

There is a Lua workaround there.

For current workaround, I am thinking of two thinks R specific

  • knitr has knitr::current_input() function. This will return the filename of the input being knitted. You won't get the original filename because it gets processed by Quarto and an intermediate file is passed to knitr but it is just a matter for extension so this would give you the filename

This is written in test2.qmd

---
format: html
---

The input `.qmd` file for this output is `` `r xfun::with_ext(knitr::current_input(), "qmd")` `` 

It returns this
image

  • Using quarto::quarto_render() and leveraging parameters

test.qmd document

---
format: html
params:
  input_file: ""
---

The input `.qmd` file for this output is `` `r params$input_file` `` 

rendered using

input <- "test2.qmd"
quarto::quarto_render(input, execute_params = list(input_file = input))

You see the idea I believe on how using params can help you pass information from R to knitr.

Hope it helps

1 Like

The knitr::current_input() solution works quite well, and seems to be the simplest approach for these purposes, assuming the input file has a qmd extension, which is nearly (mostly) always the case. The other solutions also look very useful, possibly for other purposes too.

Thanks very much for your help.

Stephen

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.