Flextable 忽略 R 中的单元格宽度参数

发布于 2025-01-16 21:24:36 字数 3767 浏览 2 评论 0原文

我在 R 中有一个数据框,我已将其转换为 Flextable。看起来不错,但我需要手动设置列宽。我尝试了多种方法来实现这一目标,但到目前为止还没有成功。我发现特别奇怪的是,当我运行 dim(myflextable) 来查看尺寸时,返回的值是我设置的值,但 Flextable 本身没有这些尺寸。任何见解将不胜感激。

数据框+库

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"))

弹性格式尝试

 #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)

暗淡(工作表)输出 - 指示我成功更改了列宽:

$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 

但这没有反映在表中:

在此处输入图像描述

抱歉,如果我遗漏了一些明显的内容,我已经花了很多时间在这个简单的事情上并浏览了文档,但一直无法找到解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文