Vik
Vik

Reputation: 549

Shiny R, Using the downloadButton function

I'm trying to figure out how to use a downloadButton to download multiple content to one pdf file. I have in the ui the download button created:

downloadButton('downloadData', 'Download Data'))

and I am trying to get multiple plots and tables into one pdf file. I have in the server file the following

 output$downloadData <- downloadHandler(
    filename = function() {
      paste('data-', Sys.Date(), '.pdf', sep=&rdquo;)
    }

but I think this will save multiple csv files. How do i download multiple plots and tables from renders such as

output$table<-renderTable({
        P.Value<- c(lev.p,bart.p,turn.p,shap.p,jar.p,linm.p) 
        Test.Statistic<-c(lev.s,bart.s,turn.s,shap.s,jar.s,linm.s) 
        df<-data.frame(P.Value,Test.Statistic)
        rownames(df, do.NULL = TRUE, prefix = "row")
        rownames(df) <- c("Levene Test","Bartlett Test","Turning Point Test","Shapiro-Wilk Test","Jarque Bera Test","Linear Model Constant Drift Test")

        df
  })

,

  output$qq.line<-renderPlot({

    index<-1:length(lg.ret.vec())
    model<-lm((lg.ret.vec())~ index) 
    plot(model,which=2,main="QQ plot of the residuals",xlab="Theoretical Quantiles")             
    qqline(rstandard(model),col="black")
  })

,

  output$histogram<-renderPlot({

      hist(lg.ret(),main="Histogram of log returns",xlab="Log returns")
  })

to name just a few.

Upvotes: 2

Views: 3227

Answers (2)

NicE
NicE

Reputation: 21443

You could take advantage of rmarkdown to make a report with all your plots, here is one example.

In your case you could use the following downloadHandler (code adapted from the link):

output$downloadData <- downloadHandler(
    filename = function() {
      paste('report', sep = '.','html')
    },

    content = function(file) {
      src <- normalizePath('report.Rmd')

      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd')

      library(rmarkdown)
      out <- render('report.Rmd',html_document())
      file.rename(out, file)
    }
  )

And create a file called report.Rmd in the same folder as your server.R and ui.R:

report.Rmd

---
title: "Your report"
output: html_document
---

This is your plot

```{r echo=FALSE}
    index<-1:length(lg.ret.vec())
    model<-lm((lg.ret.vec())~ index) 
    plot(model,which=2,main="QQ plot of the residuals",xlab="Theoretical Quantiles")             
    qqline(rstandard(model),col="black")

```

This is your histogram

```{r echo=FALSE}
hist(lg.ret(),main="Histogram of log returns",xlab="Log returns")
```

Upvotes: 2

Liz Young
Liz Young

Reputation: 408

you need to define content to download for downloadHandler. see http://www.inside-r.org/packages/cran/shiny/docs/downloadHandler

How do you mean you are trying to download plots and tables to a single csv file? Do you mean to merge the data? You may be confused about what a csv file is.

Upvotes: 0

Related Questions