How to format last two rows in flextable R
19:52 12 Mar 2026

how do I bold the last two rows in a flextable? I have a large dataset that I run through a loop that does two functions: 1. subsets the dataset and 2. makes it into a flextable. Because of the subsetting, flextables may have a different number of rows. I found that I can bold the last row of a flextable with the code

library(magritr)
library(flextable)

df<- iris

flextable(df) %>%
bold(i = nrow(df))

However, once I put it into a loop,

df<- iris

for (i in unique(df$Species)) {
  sp<- i
  
  sub_df<- df[df$Species == sp,]
  
  ft<- sub_df %>%
    as_flextable() %>%
    bold(i = nrow(sub_df))
}

I receive an error

Error in get_rows_id(x[[part]], i) : 
  invalid row selection: out of range selection

Ultimately, I would like to bold the last two rows in each flextable. Any help is much appreciated.

r r-flextable