Hi all,
I currently have an Rmd that, when rendered, produces a PDF and is scheduled to run daily on POSIT CONNECT. The default behavior is that the PDF has the same name as the Rmd (e.g., report.Rmd creates a pdf named report.pdf). However, I'd like the report name to have a date stamp in the title (e.g., report.Rmd creates a pdf name report_2025_02_20.pdf. I can do this in my laptop in the RStudio IDE when knitting the Rmd manually by adding the following to the YAML:
knit: (function(inputFile, encoding) {rmarkdown::render("report.Rmd",output_file=paste("report_",format(Sys.Date(), "%Y_%m_%d"), ".pdf", sep=""))})
However, when I publish this to the POSIT CONNECT server, the e-mailed PDF is titled just report.pdf. Does anyone know either why this isn't working on the POSIT CONNECT server and/or what I can do to get a dynamic file name for the PDF that gets rendered and sent?
Thank you!
1 Like
I figured out a round-about way to do it. It takes 3 Rmds. The main RMD just has a chunk that sets the dyanmic name, renders to Attachment File, and then uses render_connect_email to send a custom e-mail with only the newly-rendered attachment file as the attachment
The Main RMD just has:
library(dplyr)
library(blastula)
MyFileName <- paste0("Report_",format(Sys.Date(), "%Y_%m_%d"),".pdf")
# Create the PDF
rmarkdown::render(
"FormatReport.Rmd",
output_format="pdf_document",
output_file=MyFileName
)
render_connect_email(input = "FormatEmail.Rmd") %>%
attach_connect_email(
subject = "Report Update",
attach_output = FALSE,
attachments = MyFileName
)
The FormatReport.Rmd is the RMD for the file you'd like to send as an attachment.
The FormatEmail.Rmd is with output: blastula::blastula_email set in the YAML of the RMD and then just has text with no code (or you could get fancy...Sending Custom Emails from Posit Connect – Solutions)
---
title: "FormatEmail"
date: "`r Sys.Date()`"
output: blastula::blastula_email
---
## Here are the latest results.
Then when you publish to POSIT CONNECT you publish the Main.RMD and include the FormatReport.Rmd and FormatEmail.Rmd, set the scheduler in POSIT CONNECT for the Main.Rmd. and you're done. Now at a specified time you get an e-mail with the FormattedReport but with the dyanmic title, and the body of the e-mail is just the text in your FormatEmail.Rmd.
That said, I'd love to hear if someone has a more straight-forward approach!
The issue you're encountering is likely due to how the POSIT CONNECT server handles the rendering process, which may not respect custom naming functions in the YAML. While the local RStudio IDE allows custom file names during rendering, POSIT CONNECT may be overriding this behavior.
To get a dynamic file name with a date stamp on POSIT CONNECT, you can try specifying the output file name within your R code (outside of YAML) using rmarkdown::render()
with the output_file
argument. Here's a potential solution:
output_file <- paste("report_", format(Sys.Date(), "%Y_%m_%d"), ".pdf", sep="")
rmarkdown::render("report.Rmd", output_file = output_file)
Make sure that this code is placed in the Rmd file or in a script that runs before rendering. This should work for the POSIT CONNECT server, ensuring the PDF gets the correct date-stamped filename when it's rendered and emailed.