0

I have a shiny app with R Markdown report. I'm trying to pass the title via R to YAML in the .Rmd file.

Simplified Shiny App:

library(shiny)
library(rmarkdown)
ui <- fluidPage(
  titlePanel("test"),
  sidebarLayout(
    sidebarPanel(
      textInput("p", "Project Name", "Project Name"),
      downloadButton("report")
    ),

    mainPanel(
    )
  )
)

server <- function(input, output, session) {
  tex_pn <- reactive({paste("Project Name:",input$p)})

  output$report <- downloadHandler(
    filename = "report.pdf",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      params <- list(pn=input$p)
      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )

}
shinyApp(ui = ui, server = server)

report.Rmd:

I did the following in .Rmd file which does not work:

---
output:
  pdf_document :
    keep_tex: true
    number_sections: true
---

```{r echo=FALSE}
params <- list()
params$set_title <- tex_pn()
```
---
title: `r params$set_title`
author: "myname"
date: "`r Sys.Date()`"
---

some text `r params$pn`

the error is : Warning: Error in tex_pn: could not find function "tex_pn".

I have also tried changing params$set_title <- tex_pn() to params$set_title <- params$pn, which creates a file but does not show any title.

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Haribo
  • 2,071
  • 17
  • 37

2 Answers2

1

The problem here is not necessarily how your shiny app is configured, but more how the parameters are being specified in the report. You should create the params in the YAML frontmatter, with the title being specified after the parameters, as highlighted here:

---
author: "myname"
date: "`r Sys.Date()`"
params:
  set_title: test
title: "`r params$set_title`"
output: pdf_document
---

# Content
And then something here...

You can then control the document parameters within the render function as follows:

rmarkdown::render("report.Rmd",
                  params = list(set_title = "Some Text"),
                  envir = new.env(parent = globalenv()))

enter image description here

It would be worth reading the section on parameterized report in the R Markdown Defintive Guide to find more about parameterized reports in R Markdown.

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • Would be great if you could help me with another question : How could I specify the title position in Rmarkdown? the following answer did not help since it changes all the other page as well : https://stackoverflow.com/questions/34950851/rmarkdown-title-position – Haribo Jul 31 '18 at 15:21
0

This could work, if you add the r code in fron of the YAML header like this

---
output:
  pdf_document :
    keep_tex: true
    number_sections: true
---

```{r, echo=FALSE}
params <- list()
params$set_title <- paste("Report from", date())
```

---
title: `r params$set_title`
author: "myname"
date: "`r Sys.Date()`"
---

# Content
And then something here...

I am not sure, though, if this is legit, but at least for me it works. As example I just paste some string with the current date, but there you would have then your textInput.

Daniel Fischer
  • 3,280
  • 1
  • 18
  • 29