Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I would like to use column names that where selected by user in flextable

I would like to have the grouped columns bold. But the function bold doesnt accept a string type of column name. How can I make input[[paste0("options_agg_", input$tabs)]] work for selecting rows in bold function?

This works:

ft <- as_grouped_data(filtered_other(), groups = input[[paste0("options_agg_", input$tabs)]], columns = input[[paste0("options_dim_", input$tabs)]]) %>%
      as_flextable() %>%
      bold(i = ~ !is.na(Product.category))
    
    htmltools_value(ft)

and this does not:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ft <- as_grouped_data(filtered_other(), groups = input[[paste0("options_agg_", input$tabs)]], columns = input[[paste0("options_dim_", input$tabs)]]) %>%
      as_flextable() %>%
      bold(i = ~ !is.na(input[[paste0("options_agg_", input$tabs)]]))
    
    htmltools_value(ft)

I dont want it to be always set to Product.category.

I have tried: as.formula. I have also considered eval & parse. But I did not make it work

>Solution :

One option would be to use reformulate to create your formula object.

Using a minimal reproducible example based on mtcars:

library(flextable)
library(magrittr)

col <- "mpg"

fml <- reformulate(paste0("!is.na(", col, ")"))

fml
#> ~!is.na(mpg)

ft <- as_grouped_data(mtcars, groups = "cyl", columns = c("cyl", "mpg")) %>%
  as_flextable() %>%
  bold(i = fml)

ft

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading