创建一个flextble对象的

时间:2018-02-28 22:26:59

标签: r flextable

我需要创建一个flextable对象的PNG文件,以便我可以将其添加到PDF报告中。 flextable的当前版本不直接支持pdf文件。我试图使用png()/ dev.off()方法,但生成的文件没有图像,它保存,但不显示表。

这是一个简单的工作示例

library(officer)
library(flextable)
library(magrittr)

ID <- c("C01"," "," ","C02"," "," ","C03"," "," ")
Data <- c("Problem 1","Answer 1","Note 1","Problem 2","Answer 2","Note 2","Problem 3","Answer 3","Note 3")
Rank <- c("L"," "," ","L"," "," ","L"," "," ")
finalResults <- data.frame(ID,Data,Rank)

  plotIn <- function(x){

  results_table1 <- flextable(x) %>%
    theme_box() %>%

    ### Column Headers

    #  format column headers for table

    bold(part = "header") %>%
    fontsize(part = "header",size = 18) %>%
    align(part="header",align="center") %>%

    ### Body  

    # set column widths

    width(j =  ~ID, width = 1) %>%
    width(j =  ~Data, width = 8) %>%
    width(j =  ~Rank, width = 1) %>%

    # format general body of table

    fontsize(part = "body",size = 18) %>%
    align(j = ~ID, align = "center", part = "body") %>%
    align(j = ~Data, align = "left", part = "body") %>%
    align(j = ~Rank, align = "center", part = "body") %>%
    padding( padding = 5, part = "all" ) %>%
    style(pr_c = fp_cell(border = fp_border(color="black", width = 2)),part = "all") #%>%

  # format cell merging

   for(mm in seq(from=1,to=(nrow(x)-2),by=3)) {
     results_table1 <- merge_at(results_table1,i=mm:(mm+2), j = ~ID, part = "body")
     results_table1 <- merge_at(results_table1,i=mm:(mm+2), j = ~Rank, part = "body")
   }

  results_table1

  }  

  png("summary.png")
  plotIn(finalResults)
  dev.off()

我的会话信息如下:

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] magrittr_1.5    flextable_0.4.2 officer_0.2.1   shinyjs_0.8     shiny_1.0.5    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15      knitr_1.15.1      xml2_1.1.1        uuid_0.1-2       
 [5] xtable_1.8-2      R6_2.2.0          stringr_1.1.0     tools_3.3.2      
 [9] packrat_0.4.8-1   R.oo_1.21.0       miniUI_0.1.1      htmltools_0.3.5  
[13] yaml_2.1.14       digest_0.6.10     rprojroot_1.1     zip_1.0.0        
[17] base64enc_0.1-3   R.utils_2.5.0     mime_0.5          evaluate_0.10    
[21] rmarkdown_1.8     stringi_1.1.2     gdtools_0.1.6     backports_1.0.4  
[25] R.methodsS3_1.7.1 jsonlite_1.1      httpuv_1.3.5 

由于StackOverflow要求更多文本,我只是想在这里填补空间,以便我可以提交问题。

1 个答案:

答案 0 :(得分:0)

以下代码应该有所帮助:

insert_screenshot = function(x, png_file ) {
  htmltools::save_html(htmltools_value(x), 'temp.html')
  res = webshot::webshot('temp.html', png_file)
  unlink('temp.html')
  res
}
insert_screenshot(plotIn(finalResults), 'my-screenshot.png')