【问题标题】:Knit word document from Shiny App with template.docx使用模板.docx 从 Shiny App 编织 word 文档
【发布时间】:2020-10-14 07:49:23
【问题描述】:

我正在尝试使用 template.docx 文件从闪亮的应用程序中编织一个 word 文档。我收到以下错误消息:

pandoc.exe: template.docx: openBinaryFile: 不存在(没有这样的文件或目录)

以下 3 个文件当前都位于同一目录中。

app.R:

library(shiny)
library(rmarkdown)

ui <- fluidPage(

    titlePanel("Word template"),

    sidebarLayout(
        sidebarPanel(),

        mainPanel(
            downloadButton("download", "Download Report")
        )
    )
)

server <- function(input, output) {

    output$download <- downloadHandler(
        
        filename = function() {
            "report.docx"
        },
        content = function(file) {
            
            src <- normalizePath('report.Rmd')
            
            owd <- setwd(tempdir())
            on.exit(setwd(owd))
            file.copy(src, 'report.Rmd', overwrite = TRUE)
            
            out <- render('report.Rmd',
                          envir = new.env(parent = globalenv()))
            
            file.rename(out, file)
        }
        
    )
    
}

shinyApp(ui = ui, server = server)

report.Rmd:

---
title: "Test"
output:
  word_document:
    reference_docx: template.docx
    
---

`r getwd()`

```{r}
mtcars

```

template.docx 是一个空的“新”Word 2016 文档

【问题讨论】:

    标签: r shiny r-markdown


    【解决方案1】:

    downloadHandler() 函数中,您需要将.Rmd 和.docx 文件复制到临时目录

    content = function(file) {
                
                src <- normalizePath(c('report.Rmd', 'template.docx')) # SEE HERE
                
                owd <- setwd(tempdir())
                on.exit(setwd(owd))
                file.copy(src, c('report.Rmd', 'template.docx'), overwrite = TRUE) # SEE HERE
                
                out <- render('report.Rmd',
                              envir = new.env(parent = globalenv()))
                
                file.rename(out, file)
            }
    

    想法

    如果不准备一个可重现的示例并考虑如何描述问题,我就不会解决这个问题。如何提出问题值得思考!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      相关资源
      最近更新 更多