Parameterized and Scheduled Reports Filenames

Hello, I have a parameterized and scheduled rmarkdown document. I would like to customize the knitr::render() function as called by rsconnect so that I can specify the output filename that is automatically distributed to collaborators (by appending Sys.Date() or other).

Is this possible?

Thanks!

edit: I see there is a related topic at Use single Rmd to generate >1 output files that don't get overwritten with new runs?. adding knit with a render call to the YAML header works when knitting from the IDE but not in deployment.

What's the problem? Here's an example I used one time. Is this what you mean? So you create the rmd file with the document structure and an R script that has a line like this inside a loop.

  rmarkdown::render(input = "raw_data/report_any.rmd",
                    # output_format = "word_document",
                    output_file = paste0("report_", stringr::str_to_lower(site), ".docx"),
                    output_dir = "raw_data")

Thanks for the reply!

The key here is that the document is being deployed to the RStudio Connect platform. We are using the Connect Scheduled Output feature to schedule the redeployment and distribution of the document. We are trying to instruct Connect at redeployment to rename the output file from the standard my-report.Rmd -> my-report.html to my-report_foo.html, where foo could be Sys.Date() or ideally a variable from the document itself. This occurs on the Connect server so we can't access the rmarkdown::render() function directly.

I suspect a native solution might use a YAML hook as described in https://docs.rstudio.com/connect/1.7.4/user/r-markdown.html to set the output filename.

Hope that helps!

Ok it's out of my knowledge. Can you just rename the file after it's created?

That's right. I think the preferred way to do this (that I am aware of) would be to have a "parent" R Markdown document that "renders" a child and then attaches the result to an email. You can then use the pattern referenced there in your link (updated reference here: Posit Connect Documentation Version 2024.02.0 - R Markdown).

There is a way to change the output file of an Rmd from within the Rmd itself, but it seems that this approach does not work on RStudio Connect. However, for your edification it is discussed here:

I'm definitely curious to see if anyone has any other thoughts on how this approach might work! One reason that I suspect Connect enforces the file name convention is that Connect does some network routing to be sure you "see" the right thing in the browser when you visit the app directly. (i.e. it redirects you from /my/app/path to /my/app/path/myfile.html).

I recognize that this is not an ideal workflow, but setting up the parent document and running render() from there is not a whole lot more work and should give you the flexibility that you are looking for! Please report back how it goes, or if you run into any trouble implementing!!

1 Like

Thanks @cole! I ended up switching to an interactive rmarkdown document so more to follow in a separate thread.

edit: related post here External Resource Files - Interactive R Markdown docs

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.