首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从R闪亮应用程序下载现有docx对象

从R闪亮应用程序下载现有docx对象
EN

Stack Overflow用户
提问于 2019-11-24 23:13:55
回答 1查看 1.1K关注 0票数 2

我正在开发一个R闪亮的应用程序,用户提供的信息可以修改现有的Word文档供用户下载。我在下载生成的新Word文档时遇到了困难。我尝试过常规的超链接,但这似乎行不通。

(编辑:在输入这篇文章之后,我偶然发现了如何下载超链接文件。我忘记了文件需要放在www文件夹中,如下所示:Shiny hyperlink relative path to a file。因此,虽然我可以让我的闪亮的应用程序使用这种方法,我仍然想知道为什么我下面的例子是无效的)。

我遇到了#145 (https://github.com/davidgohel/officer/issues/145)的Github问题,它几乎有了解决方案。但是下载的pptx是从零开始创建的,而我想从一个现有的单词docx开始。

在我的代码示例中,有3个downloadHandler按钮:

  1. 使用了原始pptx下载代码示例,这个示例来自Github问题#145
  2. ,第二个修改了上面的命令以下载docx
  3. ,第三个按钮是我尝试下载一个现有的和修改过的docx文件

第三个按钮不像我所希望的那样工作。如果我不得不猜测,我认为这与我来自read_docx的模板有关。它看起来像是在幕后创建了一些临时文件。但我不知道从这里往哪里走。

为了完整起见,以下是一些相关的链接:

Reporters package to download docx report from shiny (使用比军官R包更早的记者)

Writing word documents with the officer package: How to combine several rdocx objects? (如果将现有docx合并到我的示例中的tempfile中会很有帮助)

downloadHandler参考:https://shiny.rstudio.com/reference/shiny/latest/downloadHandler.html

代码语言:javascript
复制
# -------- Example code ------------

library(shiny)
library(officer)
library(mschart)
library(dplyr)

# Create template folder and file. (Irrelevant if already exists.)
dir.create("www")
read_docx() %>% 
    body_add_par("My template file") %>%
    print(., target = "www/template.docx")

# Existing file as Template
mytemplate <- read_docx(path = "www/template.docx") 


# For Button 1
gen_pptx <- function(chart, file) {
    read_pptx() %>% 
        add_slide(layout = "Title and Content", master = "Office Theme") %>% 
        ph_with_chart(chart = chart) %>% 
        print(target = file)
}

chart <- data.frame(x = letters[1:3], y = 1:3) %>% 
    ms_barchart(x = "x", y = "y")


# For button 2
gen_docx <- function(file) {
    read_docx() %>% 
        body_add_par("Hello World") %>%
        print(target = file)
}


# For button 3
gen_docx2 <- function(file, doc) {
    file %>% 
        body_add_par("Hello World") %>%
        body_add_docx(src = doc) %>%
        print(target = file)
}




ui <- fluidPage(
    titlePanel("Example"),

    downloadButton("chart", "Get Chart"),
    downloadButton("document", "Get New Doc"),
    downloadButton("document2", "Get Doc from Template"), 
    tags$hr(),
    tags$p("Example hyperlink works"),
    tags$a(href='template.docx', target='_blank', 'Can only download from www folder', download = 'template.docx')

)

server <- function(input, output) {

    output$chart <- downloadHandler(
        filename = function() paste0("chart_", Sys.Date(), ".pptx"),

        content = function(file) {
            file_pptx <- tempfile(fileext = ".pptx")
            gen_pptx(chart, file_pptx)
            file.rename( from = file_pptx, to = file )
        }
    )

    output$document <- downloadHandler(
        filename = function() paste0("doc_", Sys.Date(), ".docx"),

        content = function(file) {
            file_docx <- tempfile(fileext = ".docx")
            gen_docx(file_docx)
            file.rename( from = file_docx, to = file )
        }
    )

    output$document2 <- downloadHandler(
        filename = function() paste0("doc_", Sys.Date(), ".docx"),

        content = function(file) {
            file_docx <- tempfile(fileext = ".docx")
            gen_docx2(file_docx, mytemplate)
            file.rename( from = file_docx, to = file )

        }
    )


}


shinyApp(ui = ui, server = server)
EN

回答 1

Stack Overflow用户

发布于 2022-08-31 15:15:03

不要使用打印生成助手函数gen_docx2中的docx文件。相反,您应该使用print作为内容函数的最后一步,以便将文件返回到downloadHandler。

简单示例:

代码语言:javascript
复制
gen_docx2 <- function(file, doc) {
    file %>% 
        body_add_par("Hello World") %>%
        body_add_docx(src = doc)
}

请注意上面的函数没有打印任何内容。

代码语言:javascript
复制
outputdocument2 <- downloadHandler(
  filename = function() {
    paste0("doc_", Sys.Date(), ".docx")
  },
  content = function(file) {
    doc <- read_docx() %>% gen_docx2(file_docx, mytemplate)
    print(doc, target = file)
  }
)

使用read_docx()生成临时word文件,最后使用print(doc, target = file)将其传递给downloadHandler。不需要函数file.rename

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59023362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档