4

I would like to add the name of the currently executing Quarto file to a report without hard coding it. I am working with R inside of the RStudio IDE. Both `r scriptName::current_filename()`and `r commandArgs()` return /Applications/quarto/share/rmd/rmd.R. Is there a way to get the name of the Quarto script rather than the R file that is being processed by knitr?

itsMeInMiami
  • 2,324
  • 1
  • 13
  • 34
  • 1
    This isn't exactly a duplicate as the answer is, it's not straightforward, but have you seen [this](https://stackoverflow.com/questions/73588271/customizing-r-quarto-pdf-output-file-name)? A good suggestion of workarounds. – SamR Nov 07 '22 at 13:59
  • Thanks Sam. I hadn't seen that one. Unfortunately it looks like that version requires me to hard code the file name. I am trying to enforce a reproducible workflow with novices. So an automatic lookup is *highly* preferred over hoping the programmer didn't change the file name since they named it in code. – itsMeInMiami Nov 07 '22 at 14:28
  • 1
    Fair enough. How exactly are you compiling it (I use vscode for Quarto - does RStudio have a button or are you entering a `quarto render` type command)? Have you tried `rstudioapi::getActiveDocumentContext()` and `rstudioapi::getSourceEditorContext()`? – SamR Nov 07 '22 at 14:47
  • 1
    Great ideas but no luck. I am working in the RStudio IDE. This works if I run it from the console `rstudioapi::getSourceEditorContext()[2]` but not when I embed it in the script and try to render the the document (with the render button). I get `Error: RStudio not running`. I think it is the same core problem were Quarto is starting a new process to make the document. So the name is lost. – itsMeInMiami Nov 08 '22 at 12:37
  • This would be easier if I tried it myself but for some reason RStudio always crashes when opened on my main PC! Is the behaviour the same when you try to render with `as_job = FALSE` (or globally set `options("quarto.render_as_job" = FALSE)`? – SamR Nov 08 '22 at 13:27
  • I tried it and I get the same issue with the API saying RStudio is not running. – itsMeInMiami Nov 08 '22 at 15:33

1 Answers1

0

As a simpler workaround, you can pass the file name as a parameter to the document either using the quarto::quarto_render() or from bash terminal.

Let's suppose the filename is quarto_doc.qmd

quarto_doc.qmd

---
title: "Quarto File Name"
format: html
params:
  filename: ""
---

## Quarto

you are running the file `r params$filename`

Then from bash terminal,

fname=quarto_doc.qmd
quarto render $fname -P filename:$fname

current filename in the rendered output


Or if you prefer using R function, you can create a wrapper function around quarto::quarto_render for convenience like this,

render_qmd <- function(input, ...) {
  input_file_name <- basename(input)
  quarto::quarto_render(input, execute_params = list(filename = input_file_name), ...)
}

render_qmd("quarto_doc.qmd")

shafee
  • 15,566
  • 3
  • 19
  • 47
  • 1
    This is helpful but I am hoping that there is a way to actually lookup the file name. Given that nobody has come up with a solution I am thinking I need to put in a feature request on GitHub. – itsMeInMiami Dec 02 '22 at 12:39