r语言 - 我无法从一个闪亮的应用程序创建一个 pagedown::chrome_print pdf 输出,该应用程序接受参数输入并呈现 Rmarkdown



我有一个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

我知道shine和chrome_print((之间有一个问题,需要添加chrome_print(…,async=TRUE(,但我不知道在我的shine应用程序中把这个参数放在哪里?

我确信您必须确保render函数在参数file给定的位置创建一个文件。在您当前的应用程序中,render使用默认值,但这不起作用。因此,它应该像向render添加output_file参数一样简单。以下代码片段适用于我:

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-", Sys.Date(), ".html", sep="")
},
content = function(file) {
p <- list(title = input$title,
heading = input$heading)
render("test.Rmd", params = p)
}
)
}
shinyApp(ui, server)

这个片段显示了现在原理上是如何工作的,我不知道pagedreport。如果YAML中有一个选项可以立即生成pdfs(不需要手动调用任何函数(,这应该以相同的方式工作。

如果你绝对需要调用函数,你可以在render之后的downloadHandler中这样做,假设函数可以处理输入文件名并在给定位置创建输出文件(同样,你需要在给定位置[根据参数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)
}

相关内容

最新更新