Search code examples
rshinyshiny-servershinydashboardreporters

Generate powerpoint slides with R Shiny using ReporteRs package


I have a R shiny code which makes various reports, word cloud, sentiment analysis and various other things. Now I want that by click of a button all these reports which are generated can be downloaded in one single shot and attached to ppt. So, for instance it should look like:

Slide 1: Word cloud

Slide 2: Sentiment Analysis

Slide 3: Report 1 ...and so on

Till now, I can download all these reports separately i.e. I have different tabs in my Shiny UI, for every report and I go to it and click "Download" and it get downloaded by downloadHandler.

Also, In one click I can download all these reports in one pdf i.e. in one page I have report 1 and so and so forth.

Till now I have reached till below:

    #downloadReport is my action button
    #on click of this button I am expecting the ppt. to be downloaded
    observeEvent(input$downloadReport, {
    # Create a PowerPoint document
    doc = pptx( )

    # Slide 1 : Title slide
    #+++++++++++++++++++++++
    doc <- addSlide(doc, "Title Slide")
    doc <- addTitle(doc,"Create a PowerPoint document from R software")
    doc <- addSubtitle(doc, "R and ReporteRs package")


   # Slide 2 : Add Word Cloud
    #+++++++++++++++++++++++
    doc <- addSlide(doc, "Title and Content")
    doc <- addTitle(doc, "Bar Plot")
    newData=rawInputData(); # Function which captures data (.csv file) when I have input it through R shiny
    words_list = strsplit(as.character(newData$CONTENT), " ") #CONTENT is the column which contains the test data
    words_per_tweet = sapply(words_list, length)
    pptwordcloud<-barplot(table(words_per_tweet), border=NA,main="Distribution of words per tweet", cex.main=1,col="darkcyan")
   #pptwordcloud<-barplot(table(words_per_tweet), col="darkcyan")
    doc <- addPlot(doc, fun= print, x = pptwordcloud,vector.graphic =FALSE ) 
    writeDoc(doc,'file1.pptx')
  })

The ppt. is getting generated but I can't see barplot in it by using vector.graphic =FALSE as a option. If I remove this,I am getting this error

Warning: Unhandled error in observer: javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.] observeEvent(input$downloadReport)

Can somebody point out my error.


Solution

  • Let try to reproduce =)

    1) I havent your data so i use iris and select input used for choise second colunm for table

    UI

    library(shiny)
    
    
    shinyUI(
    
      # Use a fluid Bootstrap layout
      fluidPage(    
        selectInput("sel",label = "col",choices = colnames(iris)[2:ncol(iris)]),
        downloadButton('downloadData', 'Download')
    
      )
    )
    

    Server

    library(shiny)
    library(DT)
    library(ReporteRs)
    
    shinyServer(function(input, output,session) {
    
      output$downloadData <- downloadHandler(
          filename = "file.pptx",
          content = function(file) {
            doc = pptx( )
    
            # Slide 1 : Title slide
            #+++++++++++++++++++++++
            doc <- addSlide(doc, "Title Slide")
            doc <- addTitle(doc,"Create a PowerPoint document from R software")
            doc <- addSubtitle(doc, "R and ReporteRs package")
    
    
            # Slide 2 : Add Word Cloud
            #+++++++++++++++++++++++
            doc <- addSlide(doc, "Title and Content")
            doc <- addTitle(doc, "Bar Plot")
            #newData=rawInputData(); # Function which captures data (.csv file) when I have input it through R shiny
            #words_list = strsplit(as.character(newData$CONTENT), " ") #CONTENT is the column which contains the test data
            #words_per_tweet = sapply(words_list, length)
            words_per_tweet=iris
            pptwordcloud<-function(){
              barplot(table(words_per_tweet[,c("Sepal.Length",input$sel)]), border=NA,main="Distribution of words per tweet", cex.main=1,col="darkcyan")
            }#pptwordcloud<-barplot(table(words_per_tweet), col="darkcyan")
            doc <- addPlot(doc, fun= pptwordcloud,vector.graphic =FALSE ) 
            writeDoc(doc,file)
           }
         )
      })