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

Controlling width of column from gt package with a function

I am having trouble to control the width of my gt tables with this function I created.

Any help?

cols_fn <- function(data, y){
data %>%
    select(1:4) %>%
    gt() %>%
    gt_theme_espn() %>% cols_width(4 ~ paste0(glue::glue({y}),"px"))

}

cols_fn(mtcars,y = 900)

I have this error message: Error in force(..1) : object 'y' not found

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

>Solution :

One approach to make your function work would be to use as.formula like so:

library(gt)
library(gtExtras)
library(dplyr)

cols_fn <- function(data, y) {
  data %>%
    select(1:4) %>%
    gt() %>%
    gt_theme_espn() %>%
    cols_width(as.formula(glue::glue("4~px({y})")))
}

cols_fn(head(mtcars), y = 900)

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