Tom
Tom

Reputation: 339

Can't generate R-Markdown report within ShinyApp

I've created ShinyApp where everything works fine. I'd like to add downloadHandler to generate Markdown report that contains the chosen plot. Firstly, I upload a file into ShinyApp. Nextly, I select variables to be plotted using checkBoxInput. Next step is using dropdown list to select between Lattice/ggplot2 plot and finally I'd like to click download it and get it. Unfortunately, every time I do try to download it I receive a blank Markdown page. It doesn't really matter what format of report will be generated. I'd like to get an appropiate logic for this task. I tried both solutions I found in a network:

output$downloadReport <- downloadHandler(
filename = function() {
  paste('my-report', sep = '.', switch(
    input$format, PDF = 'pdf', HTML = 'html', Word = 'docx')
  )
},

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

  owd <- setwd(getwd())
  on.exit(setwd(owd))
  file.copy(src, 'report.Rmd', overwrite = TRUE)

  out <- render('report.Rmd', switch(
    input$format,
    PDF = pdf_document(), HTML = html_document(), Word = word_document()
  ))
  file.rename(out, file)
})

and

output$report <- downloadHandler(
 filename = "report.html",

  content = function(file) {
   tempReport <- file.path(getwd(), "report.Rmd")
    file.copy("report.Rmd", tempReport, overwrite = TRUE)

     params <- list(graph = input$graph, colsel = input$colsel)
  rmarkdown::render(tempReport, output_file = file,
                    params = params,
                    envir = new.env(parent = globalenv())

So respectively I created report.rmd templates for my app to fullfill it. I tried to put a lot of things inside but none of these works. Do I miss the logic for the template?

---
title: "Untitled"
author: "user"
date: "date"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r plot, echo=TRUE}
plotdata <- reactive({
  d <- dataIn()[, c(req(input$colsel))]
  d <- melt(d, id.vars="Numer")
})

plot1 <- reactive({
  dotplot(value~Numer, data=plotdata(), auto.key = list(space="right", title="Types"), groups=variable)
})  

plot2 <- reactive({
  ggplot(plotdata(), aes(x=Numer, y=value, color=variable)) + 
    geom_point()
})  

graphInput <- reactive({
  switch(input$graph,
         "Lattice" = plot1(),
         "ggplot2" = plot2())
})
renderPlot({ 
    graphInput()
  })
})
```

Upvotes: 1

Views: 319

Answers (1)

Tom
Tom

Reputation: 339

Alright, I got it finally! Firstly, we need to run shinyApp using function "Run External". Secondly we don't need that mess I made in the template. Simple:

---
title: "Untitled"
author: "user"
date: "date"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
library(ggplot2)
library(lattice)
library(markdown)

```
```{r plot}

plot1()

plot2()

graphInput()
```

Where plot1(), plot2() and graphinput() represent my:

plot1 <- reactive({
dotplot(value~Numer,data=plotdata(), auto.key = list(space="right", title="WWW"), groups=variable)
})  

plot2 <- reactive({
ggplot(plotdata(), aes(x=Numer, y=value, color=variable)) + 
  geom_point()
})  

graphInput <- reactive({
  switch(input$graph,
         "Lattice" = plot1(),
         "ggplot2" = plot2()
)
})

Upvotes: 0

Related Questions