【问题标题】:I'm failing to create a pagedown::chrome_print pdf output from a shiny app which takes parameter inputs and renders the Rmarkdown我无法从一个闪亮的应用程序创建一个 pagedown::chrome_print pdf 输出,该应用程序接受参数输入并呈现 Rmarkdown
【发布时间】:2021-06-18 00:04:43
【问题描述】:

我有一个围绕 {pagedreport} 包(pagedown 的附加组件)构建的 rmarkdown 文件。这会创建一个 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),但我不知道在我的闪亮应用中将此参数放在哪里?

【问题讨论】:

标签: r shiny r-markdown pagedown


【解决方案1】:

我很确定你必须确保你的render 函数在参数file 给出的位置创建一个文件。在您当前的方法中,render 使用默认值,这是行不通的。因此,它应该像将output_file 参数添加到render 一样简单。以下代码 sn-ps 适用于我:

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)

这个 sn-p 原则上显示了它现在是如何工作的,我不知道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)
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2019-02-27
    • 1970-01-01
    • 2021-09-30
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多