Search code examples
rrowgt

Having issues with creating a gt table in R


I get the following error, "Error: Don't know how to select rows using an object of class quosures"

I have narrowed it down to one specific section of code in the R markdown file. If I highlight from Sum_Adjusted to "seriesttls") and run selected line, no errors. It is the tab row group item that throws it off.

  Sum_Adjusted <- Adjusted %>%gt(rowname_col = "seriesttls") %>%  
  tab_row_group(group = "Super Sectors",
            rows = vars("Mining and logging","Construction","Manufacturing","Trade, 
  transportation, and utilities","Information","Financial activities","Professional and 
 business services","Education and health services","Leisure and hospitality","Other 
 services","Government"))

I am hoping that from just looking at the code that someone can explain why I am getting this error now and not the last dozen times I have ran this exact same code. I have not been able to reproduce this in a smaller example.

str(Adjusted) produces this

  tibble [12 x 7] (S3: tbl_df/tbl/data.frame)
 $ seriesttls       : chr [1:12] "Total nonfarm" "Mining and logging" 
 "Construction" "Manufacturing" ...
 $ empces           : num [1:12] 1335900 15000 91000 60200 282200 ...
 $ MonthlyDifference: num [1:12] 4800 0 -1000 0 900 600 500 1500 0 1800 ...
 $ AnnualDifference : num [1:12] 100900 200 -1900 5200 30600 ...
 $ PercentGrowthRate: num [1:12] 0.0817 0.0135 -0.0205 0.0945 0.1216 ...
 $ Max              : num [1:12] 1442800 15800 146400 60300 282200 ...
 $ oftotal          : num [1:12] 1 0.0112 0.0681 0.0451 0.2112 ...

Solution

  • The issue is that the rows should be a vector and thus instead of vars (probably deprecated), use the standard select-helpers or just concatenate with c as described in documentation

    rows - The rows to be made components of the row group. Can either be a vector of row captions provided in c(), a vector of row indices, or a helper function focused on selections. The select helper functions are: starts_with(), ends_with(), contains(), matches(), one_of(), and everything().


    We could do

    library(gt)
    library(dplyr)
    Adjusted %>%
       gt(rowname_col = "seriesttls") %>%  
      tab_row_group(label = "Super Sectors",
                rows = c("Mining and logging","Construction","Manufacturing","Trade, 
      transportation, and utilities","Information","Financial activities","Professional and 
     business services","Education and health services","Leisure and hospitality","Other 
     services","Government"))
    

    Using a reproducible example

    gtcars %>%
      dplyr::select(model, year, hp, trq) %>%
      dplyr::slice(1:8) %>% 
      gt(rowname_col = "model") %>% 
        tab_row_group(
        label = "numbered",
        rows = c("GT", "California"))
    

    -output

    enter image description here