I am trying to build a webapp with shiny in R which shows values of different indicators by means of a couple of tables, one dynamic graph and one image. The result is like a factsheet for a selected item from a list and I’d like to capture the results and provide the possibility to download what is shown in the screen (the tables, graph and image).
I saw I could do it by generating a report in Rmarkdown and export it with downloadHandler, following this example (http://dev.use-r.com/shiny-examples/016-knitr-pdf). As far as I understand, in the server the output$downloadReport creates the object composed by the filename and the content based on a template of the report previously stored in the same directory as ui.R and server.R according to this:
#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)
}
)
To check the template for the report in this example I followed this (https://github.com/rstudio/shiny-examples/blob/master/016-knitr-pdf/report.Rmd). But,when I want to create my ‘report.Rmd’ template, I see there are different ways to create a new R markdown document (document with different output formats, presentation, shiny, and template), and this selection influences the output. I have tried some of them but didn’t succeed to make it work.
For instance, I took a new R Markdown document html output format, named ‘report.Rmd’ and saved in the same folder as ui and server files. With some very simple content:
title: "Untitled"
author: "Raquel_ub"
date: "Tuesday, October 27, 2015"
output: html_document
---
# Selected city
```{r, echo=FALSE}
level(input$City_id)
```
I get this non-informative error:
Error opening file: 2
Error reading: 6
Any hint/clue on what’s going on is more than welcome!
Many thanks, Raquel
Ah, my session info:
> 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
I faced a similar situation and also tried downloadHandler
but I couldn't make it work so what I did was:
plot1<<-ggplot()
.save("df.1","plot1", file="path/to/file/MyData.RData")
.MyData.RData
file in a template.Rmd
file:```{r, echo=FALSE, message=FALSE}
load("path/to/file/myData.RData")
```
I think the rest is pretty easy to figure out.
Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With