Printing of visualizations on same file PDF new pages in R

Below snippet, is about a process of putting several R result visualizations in a pdf file. The few visualizations are pltBestSellingProducts, pltTreemap and wordCloud. Each using rendering method is renderPlot, renderPlot and renderWordcloud2 respectively. The problem is. When I download the pdf files, only vals$BestSellingProducts can be seen, while vals$wordCloud and vals$Treemap, can't be seen in the said PDF. I don't know what's the issue because those visualizations did not return NULL(that means something is stored inside but my extraction method to the pdf, might be wrong). Can someone give me a hint on it? Also I have try searching for R Markdown implementation, but I want something which can be implement easier on the go without considering about the quality.

vals <- reactiveValues({
    BestSellingProducts = NULL,
    wordCloud = NULL,
    Treemap = NULL
})


pdf_file <- tempfile(fileext = ".pdf")
pdf(pdf_file)
print(vals$BestSellingProducts)
print(vals$wordCloud)
print(vals$Treemap)
dev.off()

output$pltBestSellingProducts <- renderPlot({
    table_BestSellingProducts <- OPT_csv_data() %>%
      group_by(product_id) %>%
      summarize(count=n()) %>%
      top_n(10,wt=count) %>%
      left_join(select(products_csv_data(),product_id,product_name),by="product_id") %>%
      arrange(desc(count))
    kable(table_BestSellingProducts)
    
    BestSellingProd <- table_BestSellingProducts %>%
      ggplot(aes(x=reorder(product_name,count), y=count))+
      geom_bar(position="dodge", stat="identity",fill="#F9CF59")+
      coord_flip() +
      theme_classic() +
      labs(title ="", y="Number of units sold", x="")
    
    vals$BestSellingProducts <- BestSellingProd + scale_y_continuous(labels=human_num)
    vals$BestSellingProducts
  })


output$pltTreemap <- renderPlot({
    tmp <- products_csv_data() %>%
      group_by(department_id,aisle_id) %>%
      summarize(n=n())
    tmp <- tmp %>% left_join(departments_csv_data(),by="department_id")
    tmp <- tmp %>% left_join(aisles_csv_data(),by="aisle_id")
    
    tmp2 <- OPT_csv_data() %>%
      group_by(product_id) %>%
      summarize(count=n()) %>%
      left_join(products_csv_data(),"product_id") %>%
      ungroup() %>%
      group_by(department_id,aisle_id) %>%
      summarize(sumcount=sum(count)) %>%
      left_join(tmp,by=c("department_id","aisle_id")) %>%
      mutate(onesize=1)
    
    vals$Treemap<-treemap(tmp2,index=c("department","aisle"),vSize="sumcount",title="",palette="Set3",border.col="#FFFFFF")
    vals$Treemap
  })


output$wordCloud <- renderWordcloud2({
    vals$wordCloud<-wordcloud2(Top_text(), size = 1, shape = "hexagon")
    vals$wordCloud
  })

I am expecting that all visualizations, can be printed and showed at the pdf files. Instead of the current problem where, only vals$BestSellingProducts can be seen while the others are missing.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.