0

I have an R markdown file that automatically generates a report, the problem is that the output file is the R file name so every time that i change the client and generate a new report it eliminates the report generated before, i need to save the output file as Name_LastName_Month_Year.

The Name and the LastName variables are declared in a code chunk bellow the YAML header, the Month and the Year are params declared in the header

The output file is a pdf.


output:
  pdf_document: default


```{r}
# Load necessary packages
library(ggplot2)
library(rmarkdown)

# Get the variable names from mtcars
params <- names(mtcars)

# Generate reports for each variable
for (i in seq_along(params)) {
  param <- params[i]
  
  # Generate the plot
  plot <- ggplot(mtcars, aes_string(x = "mpg", y = param)) +
    geom_point() +
    labs(title = paste("Scatter Plot of mpg vs.", param),
         x = "mpg",
         y = param)
plot

# Generate the report
report_file <- paste0("report_", i, ".pdf")
rmarkdown::render(input = "example.Rmd",output_file = report_file)
}

```

The optimal result would be to generate the report and to have the output file with the name: investorname_investorlastname_month_year.pdf

  • 3
    `rmarkdown::render` has an optional argument `output_file` so you can use that to name the file however you'd like. But you don't show your code to render/compile the reports... how are you doing that? – Gregor Thomas Jun 11 '23 at 20:57
  • What do you mean? to render the report I just click on knit. I tried to use the rmarkdown::render function; Like this: # Set the output file name output_filename <- paste0(params$investorname, "_", params$investorlastname, "_", params$month, "_", params$year, ".pdf") # Knit the document and save as PDF with the custom name rmarkdown::render(input = "Monthly_Report_Generator.Rmd", output_format = "pdf_document", output_file = output_filename) But It gives me an error. params already exists in the knit environment so cant be overwritten by render params – Pepe Cobos Jun 13 '23 at 11:45
  • You should take the params out of your YAML header and write a separate R script that calls your `rmarkdown::render` on your report in a `for` loop, passing the correct params and saving the file to the appropriate output file at each iteration. – Gregor Thomas Jun 13 '23 at 13:36
  • Clicking the knit button is great for a single report, but when you're generating multiple reports from the same file you need to use code, not a button, to do it well. – Gregor Thomas Jun 13 '23 at 13:36
  • Yes, I know, that would be the final result, but first I'm trying to get the output file to be saved as the name I want (with variables declared in the code afterward and with parameters), then I'll do the for loop. Because so far I cant even get the name of the file, i don't want to start with the loop. – Pepe Cobos Jun 13 '23 at 14:25
  • Well, if you use the `knit` button you don't control the file name. You need to use a function like `render()` or `knit2pdf()` for that. And as you discovered, to pass parameters via `rmarkdown::render`, you can't have parameters in the YAML header. So even if you don't want to start with the loop, you may have reached the point of moving toward the loop. (You don't have to start with a loop, you can start with a single instance. But it needs to be a separate script from your Rmd template.) – Gregor Thomas Jun 13 '23 at 14:27
  • Ok ill try, but even if I start doing the loop, the functions to change the name of the output file out of the YAML are not working. Not the rmarkdown::render not the knitr::opts_chunk$set not the knitr::opts_knit$set I even try to create a new R Markdown file and try those functions without any other code and I'm getting the same errors all the time. – Pepe Cobos Jun 13 '23 at 14:35
  • Can you make an example that is minimal and reproducible and edit your question? You haven't shared your code attempts with removing the parameters and calling `render`, and no one else can run the Rmd code you've shared because we don't have your files. Try a very simple file first, like make a single plot using `mtcars` and have a parameter determine which column goes on the y-axis, and name the output file with that name. [This is a pretty good example to follow](https://stackoverflow.com/a/15397832/903061), though it's using a LaTeX source not a markdown source. – Gregor Thomas Jun 13 '23 at 14:39
  • Yeah, i have done it and I have the same error: Error in sink(con, split Calls: Execution halted = debug) : sink stack is full evaluate -> -> evaluate_call -> watchout -> sink – Pepe Cobos Jun 13 '23 at 16:11

0 Answers0