Search code examples
rdataframegt

Middle align and bolding the first row with column names big text in R using gt library


I have a data frame in R called df that I have used previously in another question regarding the gt() and the gt library in R found here .

df = structure(list(projects = c("Restaurant, Pizzeria and Pasta House Works for  New York",
                                 "London Project - New Restaurant  Project", 
                                 "Athens - Beautiful Lands for Aigaleo City Project",
                                 "Berlin City - Exhibition  near the airport with restaurants",
                                 "Pizzeria Buenos Aires New italian restaurant - pasta, tartufo and vino",
                                 "area mean value", "total mean value"), 
                    happiness.and.joyfullness = c(3.5,4, 3, 3.2, 4, 5, 3), 
                    joy.for.children = c(3, 5, 3, 4, 5,4, 4), 
                    area = c(3, 5, 3, 3, 3, 4, 4), 
                    damages.from.environment = c(2,4, 2, 3, 3, 5, 5), 
                    approach  = c(3, 1, 5,3, 5, 5, 4), 
                    expensive = c(3, 5, 3, 4, 5, 5, 5), 
                    safety.comes.first = c(5,5, 5, 5, 5, 5, 4),
                    hungry = c(3, 5, 2, 4, 5,5, 5)), 
               row.names = c(NA, -7L), 
               class = c("tbl_df", "tbl","data.frame"))

The column names are very big text and and I want to be wrapped. So I found a way:

df%>%
  gt()%>%
  cols_label("happiness.and.joyfullness" = md("happiness<br />and<br />joyfullness"))%>%
  cols_label("joy.for.children" = md("joy<br />for<br />children"))%>%
  cols_label("damages.from.environment" = md("damages<br />from<br />environment"))%>%
  cols_label("safety.comes.first" = md("safety<br />comes<br />first"))

the problem is that I want to middle align them first row and bold it.

How can I do it in R using the gt library ?


Solution

  • Like this?

    library(gt)
    library(dplyr)
    
    df <- structure(
      list(
        projects = c(
          "Restaurant, Pizzeria and Pasta House Works for  New York",
          "London Project - New Restaurant  Project",
          "Athens - Beautiful Lands for Aigaleo City Project",
          "Berlin City - Exhibition  near the airport with restaurants",
          "Pizzeria Buenos Aires New italian restaurant - pasta, tartufo and vino",
          "area mean value", "total mean value"
        ),
        happiness.and.joyfullness = c(3.5, 4, 3, 3.2, 4, 5, 3),
        joy.for.children = c(3, 5, 3, 4, 5, 4, 4),
        area = c(3, 5, 3, 3, 3, 4, 4),
        damages.from.environment = c(2, 4, 2, 3, 3, 5, 5),
        approach = c(3, 1, 5, 3, 5, 5, 4),
        expensive = c(3, 5, 3, 4, 5, 5, 5),
        safety.comes.first = c(5, 5, 5, 5, 5, 5, 4),
        hungry = c(3, 5, 2, 4, 5, 5, 5)
      ),
      row.names = c(NA, -7L),
      class = c("tbl_df", "tbl", "data.frame")
    )
    
    df %>%
      gt() %>%
      cols_label("happiness.and.joyfullness" = md("happiness<br>and<br>joyfullness")) %>%
      cols_label("joy.for.children" = md("joy<br>for<br>children")) %>%
      cols_label("damages.from.environment" = md("damages<br>from<br>environment")) %>%
      cols_label("safety.comes.first" = md("safety<br>comes<br>first")) %>% 
      tab_style(
        style = "vertical-align:middle; font-weight: bold",
        locations = cells_column_labels()
      )
    

    enter image description here

    Created on 2024-04-26 with reprex v2.1.0