Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a flextable columns after the flextable creation

Tags:

flextable

I create a flextable based on a csv file, I put some style on it, change some cells. Then I would like to remove a specific columns of this flextable before add it to a doc. Is-there a way to create a copy of a flextable and specifying col_keys?

mydf <- GetData(....)
cols <- names(mydf)
myft <- flextable(mydf, col_keys = cols)
# Adding style to ft...
# ....

# Here I want to remove one column to the ft (and only here, not when first creating the ft)
# something as:
# ft <- CreateCopyOfFlextable(ft,cols[-which(cols=='COLB')])
#
my_doc <- read_docx()
my_doc <- my_doc %>%  body_add_par("")   %>%
  body_add_flextable(value = ft) 

print(my_doc, target = 'c:/temp/doc.docx')
like image 457
Po78 Avatar asked May 15 '19 11:05

Po78


People also ask

How to use flextable layout in MS Excel?

flextable layout can be easily managed. A set of functions will let you merge cells, add title rows, add footer notes, change the widths or heights. Use set_header_labels () to replace labels of the bottom row of header. When the flextable is created, their values are the column names of the data.frame.

How to replace labels of bottom row of header in flextable?

Use set_header_labels () to replace labels of the bottom row of header. When the flextable is created, their values are the column names of the data.frame. New header rows can be added at the top or bottom of the header. Under the hood, the names are in a single row data.frame associated with the header part of the flextable.

How to get flextable dimensions of columns and rows by default?

This part applies only when layout is “fixed”. The default sizes of flextable columns and rows are set by default values. This will drive to inadequate rows heights and columns widths in some cases. You can use function dim to get flextable dimensions.

How do I add new rows to a flextable?

When the flextable is created, their values are the column names of the data.frame. New header rows can be added at the top or bottom of the header. Under the hood, the names are in a single row data.frame associated with the header part of the flextable. You can add new rows later, they will be binded to that data.frame.


1 Answers

I just had the same problem and had a devil of a time Googling for a solution. @David-Gohel truly has the answer here, but I feel the need to provide a similar solution with additional explanation.

My problem and the OPs is that we wanted to use the data from columns that wouldn't be displayed to influence the formatting of columns that will be displayed. The concept that was not initially obvious is that you can send a data frame to flextable with more columns than you intend to display (instead of displaying all and deleting them you've used them). Then, by using the col_keys argument, you can select only those columns that you want to display while keeping the remaining ones around for additional processing (e.g., for using compose(), paragraph(), or add_chunk()).

If I understand correctly, COLB is supposed to be a flag to indicate that certain rows of COLC should be modified. If so, then my solution looks like this:

library(flextable)
library(magrittr)
library(officer)

df <- data.frame(COLA=c('a', 'b', 'c'),
                 COLB=c('', 'changevalue', ''),
                 COLC=c(10, 12, 13))

ft <- flextable(df, col_keys = c("COLA", "COLC")) %>% # Retain but don't display COLB
  compose(i = ~ COLB =='changevalue', # Use COLB for conditional modifications
          j = "COLC", 
          value = as_paragraph(as_chunk('100')), 
          part = 'body')  %>% 
  style(i = ~ COLB =='changevalue', # Use COLB for conditional formatting on COLC
        j = "COLC",
        pr_t = fp_text(color = "black", 
                       font.size = 11, 
                       bold = TRUE,
                       italic = FALSE,
                       underline = FALSE,
                       font.family = "Times New Roman"),
        part = "body")

ft

And here's what the above code produces (e.g., the "changevalue" column is the trigger for conditionally inserting 100 in COLC and also for changing the formatting):

Example Flextable

like image 95
D. Woods Avatar answered Oct 01 '22 01:10

D. Woods