1 year ago

#336357

test-img

reidj

Flextable ignores cell width arguments in R

I have a dataframe in R that I have converted to a Flextable. It looks okay, but I need to manually set the column widths. I've tried to accomplish this several ways and so far have not been successful. What I find especially odd is that when I run dim(myflextable) to look at the dimensions, the returned values are what I've set them as but the Flextable itself does not have those dimensions. Any insight would be appreciated.

dataframe + libraries

library(dplyr)
library(flextable)
library(officer)

extractionWS <- structure(list(Sample = c(12740L, 13231L, 13232L, 13233L, 13234L), 
                             Full.name = c("Method Negative Control In Vitro Extraction 600 uL RLT-TCEP 23", 
                                           "G-A12-T5_7H9tw_co2_0-05_p0-8_b0-8_48h_tube49", 
                                           "G-A12-T4_7H9tw_co2_0-05_p0-8_b0-8_h0-1_96h_tube90",
                                           "G-A13-T3_7H9tw_co2_0-05_untx_192h_tube71", 
                                           "G-A12-T5_7H9tw_co2_0-05_p0-8_b0-8_opc0-00195_48h_tube77"),
                             Providing.lab.key = c(NA, "VOS22_00349", 
                                                   "VOS22_00290", 
                                                   "VOS22_00675", 
                                                   "VOS22_00377"), 
                             kitshelf = c("", "", "", "", ""), 
                             kitbox = c(8, 8, 8, 8, 8), 
                             kitpos = c("A1", "A2", "A3", "A4", "A5"), 
                             extractdate = c("", "", "", "", ""), 
                             concentration = c("", "", "", "", ""), 
                             rnashelf = c("", "", "", "", ""), 
                             rnabox = c("", "", "", "", ""), 
                             rnapos = c("", "", "", "", ""), 
                             comment = c("", "", "", "", "")), 
                        class = "data.frame", row.names = c(NA, -5L))

extractionWS <- extractionWS   %>%
    setNames(c("WL Number", 'Full Name', "Providing Lab Key",
               "Kit Shelf", "Kit Box", "Kit Position",
               "Extraction Date", "RNA Conc. (ng/ul)",
               "Extracted RNA Shelf", "Extracted RNA Box","Extracted RNA Position", "Comment"))

flextable format attempt

 #Create flextable
  worksheet <- flextable(extractionWS)
  
  #font style
  worksheet <- bold(worksheet, bold = TRUE, part="header")
  worksheet <- align(worksheet, align = "center", part = "all") 
  worksheet <- colformat_num(worksheet, big.mark="")
  
  #border
  border.outer = fp_border(color="black", width=2.5)
  border.horizontal = fp_border(color="black", width=1.5)
  border.vertical = fp_border(color="black", width=1.5)
  worksheet <- border_outer(worksheet, border=border.outer, part="all")
  worksheet <- border_inner_h(worksheet, part="all", border = border.horizontal)
  worksheet <- border_inner_v(worksheet, part="all", border = border.vertical)
  
  #Set fontsize
  worksheet <- fontsize(worksheet, size = 12, part = "body")
  worksheet <- fontsize(worksheet, j = 2, size = 8, part = "body")
  
  #dimensions
  worksheet <- width(worksheet, 12, width = 6)

dim(worksheet) output - indicates I successfully changed the column width:

$widths
           WL.num         Full.name Providing.lab.key          kitshelf            kitbox 
             0.75              0.75              0.75              0.75              0.75 
           kitpos       extractdate     concentration          rnashelf            rnabox 
             0.75              0.75              0.75              0.75              0.75 
           rnapos           comment      Kit Position 
             0.75              6.00              0.75 

But this is not reflected in the table:

enter image description here

Sorry if I'm missing something obvious, I've spent a lot of time on this simple thing and gone over the documentation but haven't been able to find a solution.

r

flextable

0 Answers

Your Answer

Accepted video resources