davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
554 stars 79 forks source link

save_as_image alignment issues #603

Closed rosiebae closed 7 months ago

rosiebae commented 7 months ago

Hi, this is my first time posting so please let me know if I'm doing anything incorrectly.

I'm using the save_as_image function to save a table as a PNG file. I've been noticing that in some instances, an alignment issue occurs. Generally, it happens when I merge cells. There's no issue when I view the table in R Studio, but when I save it as an image, the issue pops up. Please see example screenshots below:

Screenshot of table from R Studio: Capture

Saved PNG using save_as_image: temp_tbl

Please find a minimal reproducible example below:

library(flextable)
library(tidyverse)

# Create mock data

text <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
source1 <- "DATA_SOURCE_A.COURSE_TITLE\nDATA_SOURCE_A.SUBJECT_DESCR\nDATA_SOURCE_A.CATALOG_NUMBER"
source2 <- "DATA_SOURCE_A.GRADING_BASIS\nDATA_SOURCE_A.OFFICIAL_GRADE\nDATA_SOURCE_B.STUDENT_GROUP"

temp_dat <- data.frame(
  label = c("Sources", "", "Notes"),
  col1 = c(source1, "", text),
  col2 = c(source2, "", text)
)

# Create table
temp_tbl <- flextable(temp_dat) %>%
  bg(bg = "#FFFFFF", part = "all") %>%
  merge_h(part = "body") %>%
  font(fontname = "Calibri", part = "all") %>%
  align(align = "left", part = "all")

save_as_image(temp_tbl, path = "temp_tbl.png", res = 300, expand = 0)
> sessionInfo()
R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

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

time zone: America/Chicago
tzcode source: internal

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

other attached packages:
 [1] lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2     readr_2.1.5     tidyr_1.3.1    
 [8] tibble_3.2.1    ggplot2_3.4.4   tidyverse_2.0.0 flextable_0.9.4
davidgohel commented 7 months ago

thanks for opening the issue (it's perfectly reproducible, don't worry)

We improved the rendering of text wrapping (note support for \t is also improved) but there are still misalignment, especially when the text is right aligned. You can test with the dev version

github-actions[bot] commented 4 weeks ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.