我正在尝试构建一个R中有光泽的Web应用程序,它通过几个表、一个动态图和一个图像来显示不同指标的值。结果就像是列表中所选项目的情况说明书,我想捕捉结果,并提供下载屏幕上显示内容的可能性(表格、图表和图像)。
我看到我可以通过在Rmarkdown中生成一个报告并使用downloadHandler导出它来实现这一点,如下例所示(http://dev.use-r.com/shiny-examples/016-knitr-pdf)。据我所知,在服务器中,输出$downloadReport创建了一个由文件名和内容组成的对象,该对象基于之前存储在与ui相同目录中的报告模板。R和服务器。R据此:
#Output report
output$downloadReport <- downloadHandler(
filename = function() {
paste(input$City_id,'factsheet', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- normalizePath('report.Rmd')
return(src)
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd')
library(rmarkdown)
out <- render('report.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
}
)
为了检查本例中报告的模板,我遵循了以下步骤(https://github.com/rstudio/shiny-examples/blob/master/016-knitr-pdf/report.Rmd)。但是,当我想创建我的"report.Rmd"模板时,我发现有不同的方法可以创建新的R标记文档(具有不同输出格式、演示文稿、光泽和模板的文档),并且这种选择会影响输出。我试过其中一些,但没有成功。
例如,我采用了一种新的R Markdown文档html输出格式,名为"report.Rmd",并将其保存在与ui和服务器文件相同的文件夹中。有一些非常简单的内容:
title: "Untitled"
author: "Raquel_ub"
date: "Tuesday, October 27, 2015"
output: html_document
---
# Selected city
```{r, echo=FALSE}
level(input$City_id)
```
我得到了一个非信息性错误:
Error opening file: 2
Error reading: 6
任何关于正在发生的事情的暗示/线索都是非常受欢迎的!
非常感谢,Raquel
啊,我的会话信息:
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252
[3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Spain.1252
attached base packages:
[1] splines stats graphics grDevices utils datasets methods base
other attached packages:
[1] rmarkdown_0.8.1 shiny_0.12.2 rsconnect_0.4.1.7 rCharts_0.4.5
[5] doBy_4.5-13 survival_2.37-7 foreign_0.8-66
loaded via a namespace (and not attached):
[1] digest_0.6.8 evaluate_0.8 formatR_1.2.1 grid_3.1.1
[5] htmltools_0.2.6 httpuv_1.3.3 jsonlite_0.9.17 knitr_1.11
[9] lattice_0.20-29 magrittr_1.5 MASS_7.3-33 Matrix_1.1-4
[13] mime_0.4 packrat_0.4.5 plyr_1.8.3 R6_2.1.1
[17] Rcpp_0.12.1 RJSONIO_1.3-0 rstudio_0.98.977 rstudioapi_0.3.1
[21] stringi_1.0-1 stringr_1.0.0 tools_3.1.1 whisker_0.3-2
[25] xtable_1.7-4 yaml_2.1.13
我遇到了类似的情况,也尝试了downloadHandler
,但我无法使其工作,所以我所做的是:
- 在服务器代码块中,将生成的绘图、data.frame等放入全局环境中,如下所示:
plot1<<-ggplot()
- 用
save("df.1","plot1", file="path/to/file/MyData.RData")
保存这些对象 - 在
template.Rmd
文件中加载MyData.RData
文件:
```{r, echo=FALSE, message=FALSE}
load("path/to/file/myData.RData")
```
我认为剩下的很容易弄清楚。
干杯!