Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab stops are put in column 1 ignoring the selection (e.g. j=3:8) #635

Open
adrianolszewski opened this issue May 27, 2024 · 3 comments
Open

Comments

@adrianolszewski
Copy link

adrianolszewski commented May 27, 2024

Hello @davidgohel
First of all, thank you wholeheartedly for bringing decimal (and other) tabulation marks into flextable. It is totally a game changer!
And it works perfectly.

Only I noticed one thing. I formatted a table, where the 2 first columns described the content, and the remaining columns held the numeric content.

In the 1st column I prepended the content with "\t" to mimic a nested hierarchy of items. That's very common in my field.

I didn't need any specific positions of tab stops here. Just plain, simple tabs. So I explicitly told flextable to put tab stops only in concrete cells: at the cross of i=1:nrow(data) and j=3:8.

Yet the tab stops were put in the first column too, which spoiled the hierarchy.
obraz

Luckily it was enough to select the 1 column and delete the tab stops with a mouse to fix it.

obraz

Maybe this issue was caused by the fact that I merged cells in the the 1 row to make a vertical sub-header?

@adrianolszewski
Copy link
Author

Is it possible to set the tab stops for a selection of columns?

When I tried doing separate assignments, an error was returned:

   tab_settings(part="body",
+                i=1:nrow(result),
+                j=3:8,
+                fp_tabs(
+                  fp_tab(pos = 0.65/2.54, style="decimal")
+                )) +
+   
+   tab_settings(part="body",
+                i=1:nrow(result),
+                j=14,
+                fp_tabs(
+                  fp_tab(pos = 0.7/2.54, style="decimal"),
+                  fp_tab(pos = 1.23/2.54, style="left"),
+                  fp_tab(pos = 1.97/2.54, style="decimal")
+                ))

Error in nrow_part(x, part) :
Function `nrow_part()` supports only flextable objects.

And it seems that tab stops are set indeed for the entire table, it ignores the column selection.

@davidgohel
Copy link
Owner

Hello,

sorry, we can't reproduce the error. We would need a reproductible example!

In the code you shared, you are using +, is it not |> that you wanted to use ?

@adrianolszewski
Copy link
Author

adrianolszewski commented Nov 9, 2024

Dear @davidgohel ,
I'm sorry, there was indeed a typo. But regardless of that the problem still exists.
Namely, the "j" index for columns isn't honoured + only the last tab_settings is applied...

To run it the following template will be necessary - that's because I set the overall tabulation width to 0.4.
You can do it also manually if you prefer (without the template).
The template:
output_template.docx

The data:

df <- structure(list(PT = c("Gastrointestinal disorders", "\tNausea", 
"\tTotal"), Patients_SOC = c(NA, "\t1\t(\t100.0%)", "\t1"), Events_SOC = c(NA, 
"\t1\t(\t100.0%)", "\t1"), Patients_All = c(NA, "20.0%", "20.0%"
), Events_All = c(NA, "12.5%", "12.5%")), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame"))
> df

  PT                           Patients_SOC      Events_SOC        Patients_All Events_All
  <chr>                        <chr>             <chr>             <chr>        <chr>     
1 "Gastrointestinal disorders"  NA                NA               NA           NA        
2 "\tNausea"                   "\t1\t(\t100.0%)" "\t1\t(\t100.0%)" 20.0%        12.5%     
3 "\tTotal"                    "\t1"             "\t1"             20.0%        12.5%   

The code:

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

set_flextable_defaults(
  font.family = "Arial Narrow",
  font.size = 10,
  padding = 1,
  padding.left = 1,
  padding.right = 1,
  line_spacing = 1
)

ft <- df |>
  flextable() |>
  theme_booktabs() |>
  width(width = c(5.5, rep(2.7, 4)), unit = "cm") |>
  tab_settings(
    part = "body",
    j = 2:3,
    i = 1:nrow(df),
    fp_tabs(
      fp_tab(pos = 0.50 / 2.54, style = "decimal"),
      fp_tab(pos = 0.83 / 2.54, style = "left"),
      fp_tab(pos = 1.50 / 2.54, style = "decimal")
    )
  ) |>
  tab_settings(
    part = "body",
    j = 4:5,
    i = 1:nrow(df),
    fp_tabs(
      fp_tab(pos = 0.62 / 2.54, style = "decimal")
    )
  )

read_docx("./output_template.docx") |>
  officer::body_remove() |>
  body_add_fpar(fpar(value = "Some caption"), style = "TFL_caption") |>
  body_add_flextable(value = ft) |>
  body_end_section_portrait() |>
  print(target = "tab_test.docx")

It produces the output like this:
obraz

I attach this file as well.
tab_test.docx

My intention was as below:

  1. No tab stop in the 1st column (the default one is good).
    obraz

  2. There should be 3 tab stops in columns 2 and 3 as defined in the above code
    obraz

  3. In the last 2 columns there should be just single decimal tab stop
    obraz

So, together, this is what I expect
obraz

And the file:
tab_test.docx

PS: would it be possible to allow for units in the tab stop definitions? Something like fp_tab(pos = 1.50, style="decimal", unit="cm"), similarly to already existing possibility for width(width = c(5.5, rep(2.7, 4)), unit = "cm").


> packageVersion("officer")
[1] ‘0.6.7’
> packageVersion("flextable")
[1] ‘0.9.7’

> sessionInfo()
R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=Polish_Poland.utf8  LC_CTYPE=Polish_Poland.utf8    LC_MONETARY=Polish_Poland.utf8 LC_NUMERIC=C                  
[5] LC_TIME=Polish_Poland.utf8    

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

other attached packages:
[1] flextable_0.9.7 officer_0.6.7   tidyr_1.3.1     dplyr_1.1.4    

loaded via a namespace (and not attached):
 [1] zip_2.3.0               Rcpp_1.0.11             fontBitstreamVera_0.1.1 pillar_1.9.0            compiler_4.2.0         
 [6] forcats_1.0.0           tools_4.2.0             digest_0.6.33           uuid_1.1-1              evaluate_0.23          
[11] lifecycle_1.0.3         tibble_3.2.1            pkgconfig_2.0.3         rlang_1.1.1             cli_3.6.1              
[16] rstudioapi_0.15.0       xfun_0.41               fontLiberation_0.1.0    haven_2.5.3             fastmap_1.1.1          
[21] withr_2.5.2             knitr_1.45              xml2_1.3.5              generics_0.1.3          gdtools_0.4.1          
[26] vctrs_0.6.4             systemfonts_1.1.0       askpass_1.2.0           hms_1.1.3               grid_4.2.0             
[31] tidyselect_1.2.0        glue_1.6.2              data.table_1.14.8       R6_2.5.1                textshaping_0.3.7      
[36] fansi_1.0.5             rmarkdown_2.25          tzdb_0.4.0              readr_2.1.4             purrr_1.0.2            
[41] magrittr_2.0.3          fontquiver_0.2.1        htmltools_0.5.6.1       ragg_1.2.6              utf8_1.2.4             
[46] openssl_2.1.1   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants