manu
manu

Reputation: 11

Shiny - not able to download a markdown output

I am really struggling to get this one down. I have searched here and all over but I am not sure what I am doing wrong. I apologize as this maybe a really silly question.But I am a novice. I have put in place a shiny application, where it renders an existing R markdown file which is compiled based on the Shiny inputs. In the end I get what is like a PDF file. Everything works fine except for the option to download. When I push the download, it just opens another webpage session from the beginning. How can get the final document displayed as an PDF file downloaded. Really appreciate any help.

I edited this code based on a shiny example, but still cannot get to the bottom of this. When I click on the download button it opens another session. Edited code

library(shiny)
library(knitr)
shinyServer(function(input, output,session) {
  library(knitr)
  output$markdown <- renderUI({
  HTML(markdown::markdownToHTML(knit('RMarkdown_pdf1.Rmd', quiet = TRUE)))
  })
  output$downloadData <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
      ))
    },

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

      # 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)
    }
  )
})

library(shiny)
library(knitr)

shinyUI(
  fluidPage(
    titlePanel("Drift Report - Beta Version 1.0"),
    selectInput("n",
                "Number of files:",
                choices = c(1,2,3,4)),
    checkboxInput("d", label = "Data Summary", value = FALSE),
    checkboxInput("k", label = "Drift Plots", value = FALSE),    
    radioButtons("p", label = "Plot Type", 
                       choices = list("Point Plot" = 1, "Cumm Plot"=2, "Both     - Side by Side"=3, "Both - One underneath the Other"=4),selected = NULL,inline=TRUE),
    sliderInput("s","No of Plots", min = 1, max = 50, value = 10, width =   "40%"),
    submitButton("Apply Changes"),
    conditionalPanel(
      condition = "input.n == 1",
      fileInput("dat","File Upload for Analysis", accept = ".eff")
    ),
    radioButtons('format', 'Document format for Download', c('PDF', 'HTML',   'Word'),
                 inline = TRUE),
    conditionalPanel(
      condition = "input.n == 2",
      fileInput("dat","1st File Upload for Analysis"),
      fileInput("dat3","2nd File Upload for Analysis")
    ),
    downloadButton('downloadData', 'Download'),
    uiOutput('markdown')
  )
)

log

2016-04-26T18:50:56.177858+00:00 shinyapps[98254]: Warning in file(filename, "r", encoding = encoding) :

2016-04-26T18:50:56.177862+00:00 shinyapps[98254]: cannot open file 'datEff.R': No such file or directory

2016-04-26T18:50:56.178919+00:00 shinyapps[98254]: Quitting from lines 10-45 (RMarkdown_pdf1.Rmd)

2016-04-26T18:50:56.180088+00:00 shinyapps[98254]:

2016-04-26T18:50:56.182026+00:00 shinyapps[98254]: cannot open the connection

2016-04-26T18:50:56.182763+00:00 shinyapps[98254]: Warning: Error in file: cannot open the connection

2

Upvotes: 1

Views: 2742

Answers (1)

Xiongbing Jin
Xiongbing Jin

Reputation: 12087

The id of your download button is downloadData but in server code you used downloadReport. They don't match.

Upvotes: 2

Related Questions