闪亮的交互式文档下载按钮覆盖原始 R markdown
Shiny interactive document download button overwrites original R markdown
所以我正在尝试编写一个 html R markdown 文档,其中包含交互式闪亮位,允许用户编辑图形,然后将结果下载为 pdf。但是,我尝试执行此操作的方式存在一些灾难性的错误,因为一旦 html 开始,它就会用 pdf 的内容覆盖原始降价文件 - 将其变成完全乱码编辑。
我怀疑我是否找到了一种在 R 上失败的全新方法,但我一直无法找到其他人遇到此问题的地方。此外,我查看了闪亮的参考资料 material 并且此时我只是在兜圈子,所以任何帮助将不胜感激。
我使用的是 Rstudio 1.0.44、rmarkdown 1.2 和 shiny 0.14.2。一个小的(不)工作示例:
---
title: "Minimum Failing Example"
author: "wittyalias"
date: "December 5, 2016"
output: html_document
runtime: shiny
---
```{r echo = FALSE}
library(ggplot2)
today <- Sys.Date()
inputPanel(downloadButton("dnld", label = "Download pdf"))
renderPlot({
# Example code from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
p1 <<- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
geom_line() +
ggtitle("Growth curve for individual chicks")
p1
})
reactive({
fname <- paste0("Chick Weight - ", today, ".pdf")
output$dnld <- downloadHandler(filename = fname,
content = makethepdf(file))
makethepdf <- function(fname) {
pdf(fname,
width = 14,
height = 8.5)
p1
dev.off()
}
})
```
编辑:明确一点:我希望用户能够下载多页图表,其中一些图表具有不同的格式。用户不会只下载降价文档的 pdf 版本。
发生这种情况是因为我无法识别 makethepdf 使用 file = [name of the file]
运行的原因。插个print(fname)
看看。下载处理程序不应该在观察者内部。你需要把它单独放在外面。由于某种原因,我也未能使 pdf()
dev.off()
组合工作,所以下面是一个工作版本。
output$dnld = downloadHandler(filename = paste0("Chick Weight - ", today, ".pdf"),
content = function(file){
ggsave(file, plot = p1, width = 14, height = 8.5)
})
使用tempfile()
和tempdir()
创建临时文件:
output$downloadReport = downloadHandler(
filename = function() {
normalizePath(tempfile("report_", fileext = ".docx"), winslash = "/")
},
content = function(file) {
out = rmarkdown::render("./report.Rmd",
output_file = file,
output_dir = tempdir(),
output_format = "pdf_document",
intermediates_dir = tempdir(),
envir = new.env(),
params = list( fontSize = 10)
)
})
我通常为下载的报告使用单独的 .Rmd 模板,因为布局和文本通常与应用程序中的内容相似但不完全相同。
我还发现使用参数是一种将输入设置从我的应用程序传递到我的报告的便捷方式。详情见this RStudio post
好吧,我的代码有很多问题,但是使用其他答案中的一些建议我已经能够解决它。
这个小文档的主要问题是 downloadHandler 中的 content
是一个函数 ,但在我的代码中我将 content
设置为函数调用的结果。看起来当闪亮的应用程序是第一个 运行 时它编译 content
,认为它是一个函数,但实际上最终调用了该函数。它发送 file
作为参数,它似乎不存在,除了作为基本函数。当我在控制台中使用它时,仅使用 file
调用 makethepdf
会引发错误,但无论出于何种原因,在这个应用程序中它都会随调用一起进行,显然是 file = [name of the .Rmd]
(正如 OganM 所说).
要修复,请更改:
output$dnld <- downloadHandler(filename = fname,
content = makethepdf(file))
到
output$dnld <- downloadHandler(filename = fname,
content = makethepdf)
明确一点:如果 content
使用 file
以外的任何参数调用 makethepdf
,此代码不会覆盖 .Rmd 文件。例如,content = makethepdf(fnm))
导致下载按钮显示 object not found
错误,content = makethepdf(fname))
导致下载按钮在按下时抛出 attempt to apply non-function
错误。
所以我正在尝试编写一个 html R markdown 文档,其中包含交互式闪亮位,允许用户编辑图形,然后将结果下载为 pdf。但是,我尝试执行此操作的方式存在一些灾难性的错误,因为一旦 html 开始,它就会用 pdf 的内容覆盖原始降价文件 - 将其变成完全乱码编辑。
我怀疑我是否找到了一种在 R 上失败的全新方法,但我一直无法找到其他人遇到此问题的地方。此外,我查看了闪亮的参考资料 material 并且此时我只是在兜圈子,所以任何帮助将不胜感激。
我使用的是 Rstudio 1.0.44、rmarkdown 1.2 和 shiny 0.14.2。一个小的(不)工作示例:
---
title: "Minimum Failing Example"
author: "wittyalias"
date: "December 5, 2016"
output: html_document
runtime: shiny
---
```{r echo = FALSE}
library(ggplot2)
today <- Sys.Date()
inputPanel(downloadButton("dnld", label = "Download pdf"))
renderPlot({
# Example code from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
p1 <<- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
geom_line() +
ggtitle("Growth curve for individual chicks")
p1
})
reactive({
fname <- paste0("Chick Weight - ", today, ".pdf")
output$dnld <- downloadHandler(filename = fname,
content = makethepdf(file))
makethepdf <- function(fname) {
pdf(fname,
width = 14,
height = 8.5)
p1
dev.off()
}
})
```
编辑:明确一点:我希望用户能够下载多页图表,其中一些图表具有不同的格式。用户不会只下载降价文档的 pdf 版本。
发生这种情况是因为我无法识别 makethepdf 使用 file = [name of the file]
运行的原因。插个print(fname)
看看。下载处理程序不应该在观察者内部。你需要把它单独放在外面。由于某种原因,我也未能使 pdf()
dev.off()
组合工作,所以下面是一个工作版本。
output$dnld = downloadHandler(filename = paste0("Chick Weight - ", today, ".pdf"),
content = function(file){
ggsave(file, plot = p1, width = 14, height = 8.5)
})
使用tempfile()
和tempdir()
创建临时文件:
output$downloadReport = downloadHandler(
filename = function() {
normalizePath(tempfile("report_", fileext = ".docx"), winslash = "/")
},
content = function(file) {
out = rmarkdown::render("./report.Rmd",
output_file = file,
output_dir = tempdir(),
output_format = "pdf_document",
intermediates_dir = tempdir(),
envir = new.env(),
params = list( fontSize = 10)
)
})
我通常为下载的报告使用单独的 .Rmd 模板,因为布局和文本通常与应用程序中的内容相似但不完全相同。
我还发现使用参数是一种将输入设置从我的应用程序传递到我的报告的便捷方式。详情见this RStudio post
好吧,我的代码有很多问题,但是使用其他答案中的一些建议我已经能够解决它。
这个小文档的主要问题是 downloadHandler 中的 content
是一个函数 ,但在我的代码中我将 content
设置为函数调用的结果。看起来当闪亮的应用程序是第一个 运行 时它编译 content
,认为它是一个函数,但实际上最终调用了该函数。它发送 file
作为参数,它似乎不存在,除了作为基本函数。当我在控制台中使用它时,仅使用 file
调用 makethepdf
会引发错误,但无论出于何种原因,在这个应用程序中它都会随调用一起进行,显然是 file = [name of the .Rmd]
(正如 OganM 所说).
要修复,请更改:
output$dnld <- downloadHandler(filename = fname,
content = makethepdf(file))
到
output$dnld <- downloadHandler(filename = fname,
content = makethepdf)
明确一点:如果 content
使用 file
以外的任何参数调用 makethepdf
,此代码不会覆盖 .Rmd 文件。例如,content = makethepdf(fnm))
导致下载按钮显示 object not found
错误,content = makethepdf(fname))
导致下载按钮在按下时抛出 attempt to apply non-function
错误。