Search code examples
rpdf-generationexport-to-excel

Export a list of multiple tables and plots into a pdf and an excel file



I have a dataset stored in a data frame. I generate from this dataset 2 matrices and 2 ggplots objects, which are then stored in a list. In certain cases, the output of one of those objects is null. Regardless of the output, I have trouble saving it a pdf file (All 4 objects on one page). The file ends being empty and 3kb of size. "It may be damaged or use a file format that Preview doesn’t recognize."

please find below my code:

audience_summary<-function(df){
  #cities<-unique(df$Account.name)
  cities=c('AMIENS','TOULOUSE','NANCY')
  do.call(file.remove, list(list.files("output/", full.names = TRUE)))
  list_pages<-list()
  counter=1
  for (i in cities){
    subset_df<-filter(df,Account.name==i)
    audience_list<-unique(subset_df$Ad.set.name)
    for (a in audience_list){
      subset_df1<-filter(subset_df,Ad.set.name==a)
      subset_post<-filter(subset_df1,medium=='post')
      subset_video<-filter(subset_df1,medium=='video')
      post_dataset<-NULL
      video_dataset<-NULL
      fig_post<-NULL
      fig_video<-NULL
      if(nrow(subset_post)>0){
        post_dataset<-audience_dataset(subset_post,months_list,'post')
        fig_post<-audience_plot(df,'post')
      }
      if(nrow(subset_video)>0){
        video_dataset<-audience_dataset(subset_video,months_list,'video')
        fig_video<-audience_plot(df,'video')
      }
      one_page<-list()
      one_page[[1]]<-fig_post
      one_page[[2]]<-post_dataset
      one_page[[3]]<-fig_video
      one_page[[4]]<-video_dataset
      revised_page<-list()
      page_counter<-1
      for (z in 1:length(one_page)){
        if (!is.null(one_page[[z]])){
          revised_page[[page_counter]]<-one_page[[z]]
          page_counter=page_counter+1
        } 
      }
      print(counter)
      #list_pages[[counter]]<-revised_page
      write_to_pdf(paste(a,'-',i),revised_page)
      counter=counter+1
    }
  }
  #return(list_pages)
  staplr::staple_pdf(input_directory='output',
                     output_filepath=paste0('Summary_Analysis_',Sys.Date(),'.pdf'),
                     overwrite=TRUE)
}
write_to_pdf<-function(filename,array_of_objects) {
  pdf(paste0('output/',filename,'.pdf'), width=8, height=11)
  for (o in 1:length(array_of_objects)){
    print(array_of_objects[[o]])
  dev.off()
}
audience_results<-audience_summary(revised_filtered)

I wish to output revised_page on a pdf page but as well on an excel sheet. thanks for your help.


Please find the dataset below

summary(revised_filter)
 Ad.set.name        Account.name       Post.engagement.rate reported_date Platform         
 Length:30          Length:30          Min.   :0.001642     Length:30          Length:30         
 Class :character   Class :character   1st Qu.:0.152426     Class :character   Class :character  
 Mode  :character   Mode  :character   Median :0.318894     Mode  :character   Mode  :character  
                                       Mean   :0.346229                                          
                                       3rd Qu.:0.545346                                          
                                       Max.   :0.714029      

enter image description here


UPDATE I have found a solution for pdfs but is still hoping to get a solution for the export to excel


Solution

  • I was able to resolve the issue regarding creating pdf files. I ended up using ggpubr::ggarrange(plotlist=revised_page) from the library ggpubr if(typeof(object)=='character', that is a matrix table, I used ggtexttable(object). This link was really useful. http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/

    page_counter<-1
          for (z in 1:length(one_page)){
            if (is.null(one_page[[z]])){}
            else{
              if(typeof(one_page[[z]])=='character'){
                revised_page[[page_counter]]<-ggtexttable(one_page[[z]],theme = ttheme("mOrange"))
              }
              if(typeof(one_page[[z]])[1]=='list'){
               revised_page[[page_counter]]<-one_page[[z]]
              }
              page_counter=page_counter+1
            } 
          }
          numberofiems<-length(revised_page)
          output_page<-ggarrange(plotlist=revised_page,ncol=1,nrow=numberofiems)
          print(counter)
          list_pages[[counter]]<-output_page
          write_to_pdf2(paste(a,'-',i),output_page)
    

    and I simplified the pdf function to process my code

    write_to_pdf2<-function(filename,one_page){
      pdf(paste0('output/',filename,'.pdf'), width=8, height=12)
      print(one_page)
      dev.off()
    }