最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I create a table using flextable in R with a horizontal scroll with customised width settings - Stack Overflow

programmeradmin0浏览0评论

I using flextable to produce tables in html format. I have a table with long text in some cells and so would like to increase the column width, but width() and autofit() only increase column widths up to a certain width. Here is a reproducible example:

pacman::p_load(flextable)

# Create a sample data frame
flowers <- data.frame(
  Sepal.Length = rnorm(10),
  Sepal.Width = rnorm(10),
  Petal.Length = rnorm(10),
  Petal.Width = rnorm(10),
  longtext = sample(c("really long text................................................................really long text................................................................really long text................................................................really long text................................................................really long text................................................................really long text................................................................really long text................................................................really long text................................................................really long text................................................................"), 10, replace = TRUE))

# Create the flextable and specify width
flextable(flowers) |> 
  width(j = "longtext", width = 500,unit="cm") |>
  set_table_properties(opts_html = list(scroll = list(height = "1000px")))

#This also has no effect on column widths
flextable(flowers) |> 
  set_table_properties(layout = "autofit") |>
  set_table_properties(opts_html = list(scroll = list(height = "1000px")))

#The same thing happens with the gt package - increases column widths up to a point, but not enough to get rows 1 line high
gt(flowers) %>%
   cols_width(vars(longtext) ~ px(1000))

My desired outcome is to have a wide column for "longtext" which can be viewed by scrolling so that row heights remain 1 line high. I also tried specifying row heights, but this didn't work. For example, this works using kable and kable Extra:

kbl(flowers)  |> 
  column_spec(which(colnames(flowers) == "longtext"), width_min = "40in") |> 
  scroll_box(width = "1000px", height = "1000px")

Very grateful for any suggestions!

Session info:

R version 4.4.2 (2024-10-31)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.6.1
flextable_0.9.6
gt_0.11.1
kableExtra_1.4.0

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论