简体   繁体   中英

How to pass a title from Shiny application to R Markdown document?

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.

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()))

在此处输入图片说明

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM