问题描述
我有一个 rmarkdown 文件,它是围绕 {pagedreport} 包(pagedown 的附加组件)构建的。这将创建一个 html,他们使用 pagedown::chrome_print 将 html 转换为 pdf。
当我在 rmarkdown 文件中手动输入参数时,我拥有的 rmarkdown 文件按计划工作,但我希望能够从闪亮的应用程序中选择我的参数。
我目前已经尝试过这种方法
output$report <- downloadHandler(
# For PDF output,change this to "report.pdf"
filename = "new_report.pdf",content = function(file) {
# copy the report file to a temporary directory before processing it,in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path("../temp/","myRmarkdown.Rmd")
file.copy("myRmarkdown.Rmd",tempReport,overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(A = input$A,B = input$B)
# Knit the document,passing in the `params` list,and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
library(rmarkdown)
render(tempReport,params = params,envir = new.env(parent = globalenv()))
}
我得到了结果:
Output created: MyRmarkdown.html
但是没有创建pdf。 Pagedown 使用 chrome_print 进行转换 - 这是在 Rmarkdown 的 yaml 中。
这是我的 Rmarkdown 文件的 YAML
output:
pagedreport::paged_windmill:
img_to_dark: TRUE
logo_to_white: TRUE
knit: pagedown::chrome_print
#toc: TRUE
toc-title: "Table of Contents"
#toc_depth: 1
main-color: "#264653"
secondary-color: "#2a9d8f"
google-font: TRUE
main-font: "Raleway"
header-font: "Roboto"
params:
A: NA
B: NA
editor_options:
markdown:
wrap: 72
我知道 Shiny 和 chrome_print() 之间存在问题,您需要添加 chrome_print(...,async = TRUE),但我不知道在我的闪亮应用程序中将此参数放在哪里?
解决方法
我很确定您必须确保您的 render
函数在参数 file
给定的位置创建一个文件。在您当前的方法中,render
使用默认值,这是行不通的。因此,它应该像将 output_file
参数添加到 render
一样简单。以下代码片段对我有用:
test.Rmd
---
title: "Untitled"
output: html_document
params:
title: "Test"
heading: "Header"
---
```{r setup,include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r results="asis"}
cat("## ",params$title)
```
You passed in argument `r cat(params$heading)`
app.R
library(shiny)
library(rmarkdown)
ui <- fluidPage(textInput("heading","Heading"),textInput("title","Title"),downloadButton("download_ok","Works"),downloadButton("download_nok","Does not Work"))
server <- function(input,output) {
output$download_ok <- downloadHandler(
filename = function() {
paste("data-",Sys.Date(),".html",sep="")
},content = function(file) {
p <- list(title = input$title,heading = input$heading)
render("test.Rmd",output_file = file,params = p)
}
)
output$download_nok <- downloadHandler(
filename = function() {
paste("data-",params = p)
}
)
}
shinyApp(ui,server)
这段代码显示了现在原则上是如何工作的,我不知道 pagedreport
。如果 YAML 中有一个选项可以立即生成 pdfs
(无需手动调用任何函数),这应该以相同的方式工作。
如果您绝对需要调用该函数,则可以在 downloadHandler
之后的 render
中执行此操作,假设该函数可以处理输入文件名并在给定位置创建输出文件(再次您需要在给定位置创建文件 [根据参数 file
])
阅读 pagedown::chrome_print
的文档后,您可以按如下方式调整您的 downloadHandler
:
output$report <- downloadHandler(
filename = "new_report.pdf",content = function(file) {
tempReport <- file.path("../temp/","myRmarkdown.Rmd")
file.copy("myRmarkdown.Rmd",tempReport,overwrite = TRUE)
params <- list(A = input$A,B = input$B)
html_fn <- rmarkdown::render(tempReport,params = params,envir = new.env(parent = globalenv()))
pagedown::chrome_print(html_fn,file)
}