Home > Back-end >  flextable: bold the maximum value in each row
flextable: bold the maximum value in each row

Time:05-30

I'm trying to bold the maximum value in every row with this code

mtcars %>% 
  flextable() %>%
  bold(max(.[1:32,]))

and got this error: Error in .[1:32, ] : incorrect number of dimensions

I also tried, with inspiration from enter image description here

CodePudding user response:

To get the max from every row you can try to overwrite the attributes of your flextable object:

Edit:

As mentioned in the comments it is not recommended to change the object strucutre by hand:

library(magrittr)
mtcars %>% 
  flextable::flextable()  -> bold_flex2

for(i in seq_len(nrow(mtcars)))
{
  bold_flex2 %<>% flextable::bold(i, which.max(mtcars[i,]))
}
bold_flex2

old answer:

library(magrittr)

mtcars %>% 
  flextable::flextable() -> bold_flex

for(i in seq_len(nrow(mtcars)))
{
  bold_flex$body$styles$text$bold$data[i, which.max(mtcars[i,])] <- TRUE
}

enter image description here

  • Related