使用ReporteRs包

时间:2016-03-31 13:21:23

标签: r shiny shiny-server shinydashboard reporters

我有一个R闪亮的代码,可以生成各种报告,文字云,情感分析和各种其他内容。现在我希望通过点击一个按钮,所有这些生成的报告都可以一次性下载并附加到ppt。所以,例如它应该看起来像:

幻灯片1:文字云

幻灯片2:情感分析

幻灯片3:报告1 ......等等

到目前为止,我可以单独下载所有这些报告,即我的Shiny UI中有不同的标签,每个报告都会转到它并点击"下载"它由 downloadHandler 下载。

此外,只需点击一下,我就可以用一个pdf下载所有这些报告,即在一个页面中我有报告1,依此类推。

直到现在我已经到达下面:

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

ppt。正在生成但我无法通过使用vector.graphic =FALSE作为选项在其中看到条形图。如果我删除了这个,我收到此错误

  

警告:观察者中未处理的错误:   javax.xml.bind.UnmarshalException     - 链接异常:[org.xml.sax.SAXParseException:根元素之前的文档中的标记必须格式正确。]   observeEvent(输入$ downloadReport)

有人可以指出我的错误。

1 个答案:

答案 0 :(得分:7)

让我们尝试重现=)

1)我没有您的数据,所以我使用iris并选择用于选择table

的第二个colunm的输入

UI

library(shiny)


shinyUI(

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

  )
)

服务器

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