5

我有一个 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(input$downloadReport)

有人可以指出我的错误。

4

1 回答 1

8

让我们尝试重现 =)

1)我没有你的数据,所以我使用iris并选择用于选择第二列的输入table

用户界面

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)
       }
     )
  })
于 2016-04-05T13:13:31.717 回答