Padding unit of measurement

Hello,

I'm trying to use flextable to construct a table on a pre-formatted data frame. The data frame itself is a table that's been generated in other software and imported into R. As such, it has most of the formatting, including indentation, already embedded in the columns.

Is there a straight forward way to preserve this in flextable? I haven't found one, so my approach has been to use the padding() to add indentation to the rows that require it. What I can't seem to figure out is the unit of measurement padding() takes.

My workaround involves counting the number of blanks on a given row, and using that as the padding factor, since my data frame is pre-formatted. However, it's clear that these are two different units of measurement.

Hope the reprex below will help clarify.

suppressPackageStartupMessages(
  {
    library(dplyr)
    library(janitor)
    library(flextable)
    library(stringr)
  }
)
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'janitor' was built under R version 3.6.3
#> Warning: package 'stringr' was built under R version 3.6.3

#an example of my pre-formatted table - a single column with indentations preserved
df_demo <- data.frame(Variable = c('Fruit','   Apple', '   Orange'), stringsAsFactors = FALSE)

#no indentation preserved - this seems to be expected
df_demo %>% 
  flextable()

#my work around - count number of blank spaces
df_demo <- df_demo %>%
            mutate(n_spaces = str_count(Variable, pattern=" "))

#use this as "padding" in flextable - it's clear the padding unit is not equal to the number of characters - any idea what it is?
df_demo %>%
  flextable() %>%
  padding(j=1, padding.left = df_demo$n_spaces)

#if I add a multiplier, it looks a bit better - but i don't want to guess and make this programmatic as possible
df_demo %>%
  flextable() %>%
  padding(j=1, padding.left = 3 * df_demo$n_spaces)

Created on 2020-11-12 by the reprex package (v0.3.0)

If there are any insights or suggestions to differing approaches, please chime in! Also, this was my first reprex! Awesome package!

Cheers,
Matt

Thanks for the reprex, sorry I can't offer one in return (it chokes for some reason; the ending error is bogus). Eyeballing with various padding.left values, it appear that the unit is %age of column width

suppressPackageStartupMessages(
    {
        library(dplyr)
        library(janitor)
        library(flextable)
        library(stringr)
    }
)

df_demo <- data.frame(Variable = c('Fruit','   Apple', '   Orange'), stringsAsFactors = FALSE)

df_demo %>%
    flextable() %>%
    padding(j=1, padding.left = 10) %>%
    padding(j=1,i=1,padding.left = 0)
#> Error in knit_print.flextable(x, ...): unsupported format for flextable rendering:markdown_strict

Created on 2020-11-12 by the reprex package (v0.3.0)

1 Like

Hi technocrat,

Thank you for the quick and insightful reply! I think I see this now and will be helpful in planning for projects that involving styling + indentation.

Cheers,
Matt

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.