我正在开发一个R闪亮的应用程序,用户提供的信息可以修改现有的Word文档供用户下载。我在下载生成的新Word文档时遇到了困难。我尝试过常规的超链接,但这似乎行不通。
(编辑:在输入这篇文章之后,我偶然发现了如何下载超链接文件。我忘记了文件需要放在www文件夹中,如下所示:Shiny hyperlink relative path to a file。因此,虽然我可以让我的闪亮的应用程序使用这种方法,我仍然想知道为什么我下面的例子是无效的)。
我遇到了#145 (https://github.com/davidgohel/officer/issues/145)的Github问题,它几乎有了解决方案。但是下载的pptx是从零开始创建的,而我想从一个现有的单词docx开始。
在我的代码示例中,有3个downloadHandler按钮:
。
第三个按钮不像我所希望的那样工作。如果我不得不猜测,我认为这与我来自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
# -------- 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)发布于 2022-08-31 15:15:03
不要使用打印生成助手函数gen_docx2中的docx文件。相反,您应该使用print作为内容函数的最后一步,以便将文件返回到downloadHandler。
简单示例:
gen_docx2 <- function(file, doc) {
file %>%
body_add_par("Hello World") %>%
body_add_docx(src = doc)
}请注意上面的函数没有打印任何内容。
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。
https://stackoverflow.com/questions/59023362
复制相似问题