从 Shiny App 使用 template.docx 编织 word 文档 [英] Knit word document from Shiny App with template.docx

查看:50
本文介绍了从 Shiny App 使用 template.docx 编织 word 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to knit a word document from a shiny app using a template.docx file. I get the following error message:

pandoc.exe: template.docx: openBinaryFile: 不存在(没有那个文件或目录)

pandoc.exe: template.docx: openBinaryFile: does not exist (No such file or directory)

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

The following 3 files were all currently located in the same directory.

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:

report.Rmd:

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

`r getwd()`

```{r}
mtcars

```

template.docx 是一个空的新"文件Word 2016 文档

template.docx is an empty "new" Word 2016 document

推荐答案

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

In the downloadHandler() function you need to copy both .Rmd and .docx files to the temporary directory

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)
        }

想法

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

Thoughts

I wouldn't have worked this out without preparing a reproducible example and thinking about how to describe the problem. It's worth thinking about how to ask your question!

这篇关于从 Shiny App 使用 template.docx 编织 word 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆