使用模板.docx从Shiny App编织Word文档

问题描述 投票:0回答:1

我正在尝试使用 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)

报告。Rmd:

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

`r getwd()`

```{r}
mtcars

```

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

r shiny r-markdown
1个回答
4
投票

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)
        }
© www.soinside.com 2019 - 2024. All rights reserved.