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