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.