How to include both column names and column labels for sas datasets at the top of table in gt tables in r?

32 Views Asked by At

1. Query I am using sas datasets (sas datasets contain labels that can be read labelled::get_variable_labels(df)) and trying to create gt table that can have both column name and labels at the top of data. But I am not sure how to do that and nor able to find anything relevant on net.

By default gt is using lables as the header of the data.

enter image description here

data

df %>% head()
# A tibble: 6 × 357
  SPOUSARR SPOUSE NRESPOUS HHSPOUSE SPOUSEFW SPOUSENF HHKID
     <dbl>  <dbl>    <dbl>    <dbl>    <dbl>    <dbl> <dbl>
1       NA      0       NA        0       NA       NA     0
2       NA      0       NA        0       NA       NA     0
3       NA      1        3        0       NA       NA     0
4       NA      0       NA        0       NA       NA     6
5       NA      1        0        1       NA       NA     2
6       NA      1        3        0       NA       NA     4

below code gives me the label names in the dataset:

labelled::get_variable_labels(df)

$SPOUSARR
[1] "YEAR SPOUSE ARRIVAL IN US"

$SPOUSE
[1] "SPOUSE ON FAMILY GRID"

$NRESPOUS
[1] "NON HH RESIDENT SPOUSE"

$HHSPOUSE
[1] "SPOUSE IN HH"

2. Desired output: Structure of table that I am trying to achieve is like below: enter image description here

3. Sample data Source used in this post is downloaded from this data link government source https://www.dol.gov/agencies/eta/national-agricultural-workers-survey/data/files-sas.

4. packages & code used:

library(tidyverse)
library(haven)
library(gt)
library(labelled)

# after download & unzipping 15mb file
df <- haven::read_sas("naws_all.sas7bdat")

# to view table
df %>% head() %>% gt()

# to view lables
labelled::get_variable_labels(df)

Would really Appreciate any help.

1

There are 1 best solutions below

3
Friede On BEST ANSWER

Tweaking fun() from this answer, we can try

# downloading and preparing data
if(exists("hasRun") == FALSE) {
  temp = tempfile()
  temp2 = tempfile()
  download.file("https://www.dol.gov/sites/dolgov/files/ETA/naws/pdfs/naws_all.zip", temp) 
  unzip(zipfile = temp, exdir = temp2)
  df = haven::read_sas(file.path(temp2, list.files(temp2)))
  unlink(c(temp, temp2))
  hasRun = TRUE 
}
X = df[1:3, 1:3]

fun = \(df, column, label) {
  cols_list = as.list(label) |> setNames(column)
  df |> 
    gt::gt() |>
    gt::cols_label(.list = cols_list, .fn = gt::md) |>
    gt::cols_align(align = "left")
}
fun(X, colnames(X), 
    paste0(colnames(X), "<br>", labelled::get_variable_labels(X)))

gives

enter image description here