[英]How to pass a title from Shiny application to R Markdown document?
I have a shiny app with R Markdown report. 我有一个带有R Markdown报告的闪亮应用程序。 I'm trying to pass the title via R to YAML in the
.Rmd
file. 我正在尝试通过R将标题传递给
.Rmd
文件中的YAML。
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 : report.Rmd :
I did the following in .Rmd
file which does not work: 我在无法正常运行的
.Rmd
文件中执行了以下操作:
---
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"
. 错误是:
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. 我还尝试将
params$set_title <- tex_pn()
更改为params$set_title <- tex_pn()
params$set_title <- params$pn
,这将创建一个文件,但不显示任何标题。
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 : 您应该创建
params
在YAML frontmatter,与参数后被指定的标题,所强调的在这里 :
---
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: 然后,您可以按以下方式在
render
函数中控制文档参数:
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.
这将是值得一读的部分参数报告将R降价Defintive指南中找到更多关于R中降价参数报告。
This could work, if you add the r code in fron of the YAML header like this 如果您像这样在YAML标头的标题中添加r代码,则可能会起作用
---
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
. 例如,我只是粘贴一些带有当前日期的字符串,但是在那里您将拥有
textInput
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.