Search code examples
rtransposegt

Transposing a table for use with gt::gt()


I'd like to pipe gt at the end of this command but the function creates a list, which is not allowed in gt()

Error in UseMethod("group_vars"): no applicable method for 'group_vars' applied to an object of class "c('matrix', 'array', 'list')"`

test <- datasets::attenu%>% map(function(x) (sum(is.na(x)))) 
test %<>% t(.) 

I can do it if

datasets::attenu %>% map(function(x) (sum(is.na(x)))) %>% as_tibble() %>% gt::gt()

but then I can't transpose the table

test <- t(test)

at any point. Any suggestions about how to make the table vertical? Maybe the axis-flip is a "bug" that can be added to an update.


Solution

  • You can first summarise and get data in long format :

    library(dplyr)
    library(tidyr)
    
    datasets::attenu %>%
       summarise(across(.fns = ~sum(is.na(.)))) %>%
       #Or use summarise_all in older version of dplyr
       #summarise_all(~sum(is.na(.))) %>%
       pivot_longer(cols = everything()) %>%
       gt::gt()
    

    enter image description here