5

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.

Batanichek
  • 7,761
  • 31
  • 49
Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51
  • @Batanichek: I realzied my mistake and now I am using addPlot() only since my worddownload() function is rendering plot. After making these changes I am getting new error. Please see the edited question – Rahul Agarwal Apr 04 '16 at 14:26
  • So let see what return your function ( not in shiny in simple r session)-- in my its null ( `data(crude) tdm <- TermDocumentMatrix(crude) m<-as.matrix(tdm); word.freq<-sort(rowSums(m),decreasing=TRUE); minimum=4 p<-wordcloud(words=names(word.freq),freq=word.freq,min.freq=minimum,random.order=FALSE,colors=brewer.pal(8, "Dark2"),main="Title") p `) . so may be better really in fucntion save plot as png and than addImage? – Batanichek Apr 04 '16 at 14:37
  • In addPlot.pptx you can see "fun -- plot function. The function will be executed to produce graphics. For grid or lattice or ggplot object, the function should just be print and an extra argument x should specify the object to plot. For traditionnal plots, the function should contain plot instructions. See examples." – Batanichek Apr 04 '16 at 14:40
  • @Batanichek: Thanks!! problem got solved. It worked. – Rahul Agarwal Apr 05 '16 at 14:15

1 Answers1

8

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)
       }
     )
  })
Batanichek
  • 7,761
  • 31
  • 49
  • Actually the last part is wrong, see below the correct code: doc <- addPlot(doc, fun= function() barplot(table(words_per_tweet[,c("Sepal.Length",input$sel)]), border=NA,main="Distribution of words per tweet", cex.main=1,col="darkcyan"),vector.graphic =FALSE ) @batanichek: As you said in your comments,"For grid or lattice or ggplot object, the function should just be print and an extra argument x should specify the object to plot.For traditionnal plots, the function should contain plot instructions". This part helped with me plus the documentation of ReporteRs package. – Rahul Agarwal Apr 06 '16 at 07:04
  • You can define `barplot(table(words_per_tweet[,c("Sepal.Length",input$sel)]), border=NA,main="Distribution of words per tweet", cex.main=1,col="darkcyan")` as new plot fucntion and use it. Are you test my code? It isnt work? – Batanichek Apr 06 '16 at 07:20