Reputation: 2236
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.
Upvotes: 0
Views: 941
Reputation: 15419
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.
Upvotes: 1
Reputation: 3380
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
.
Upvotes: 0