Generating markdown report in shiny?

I do have another issue - which I'm not sure can be replicated.

I have put together below a more simplified version of the initial app. For some reason, when I click on "Generate report", the file created is called "report" when I specified it to be "report.pdf". I end up having to manually change the downloaded file name to add ".pdf" in order for my computer to recognize it as a PDF document. Is anyone else experiencing the same issue? Is there a correction to be made to the below?

app.R:

library(rmarkdown)
library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    downloadButton("report", "Generate report")
  ),
  server = function(input, output) {
    output$report <- downloadHandler(
      filename = "report.pdf",
      content = function(file) {
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)

        params <- list(n = input$slider)

        render(tempReport, output_file = file,
           params = params,
           envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

report.Rmd:

---
title: "Dynamic report"
output: pdf_document
params:
  n: NA
---

```{r}
# The `params` object is available in the document.
params$n
```

A plot of `r params$n` random points.

```{r}
plot(rnorm(params$n), rnorm(params$n))
```
1 Like