Search code examples
rfor-loopexpss

Labels not parsed in Expss for loop


I'm new to R and trying to explore my variables by groups and i'm using a for loop to pass all suiting variable names under expss.

Here is an reproducible example :


    require(expss)
    require(dplyr)
    colnoms <- as.data.frame(HairEyeColor) %>%  names(.)
    expss_digits(2)

    for (i in colnoms){      

    as.data.frame(HairEyeColor) %>%
        tab_cells(get(i)) %>%
        tab_cols(Eye) %>% 
        tab_stat_cpct() %>%
        tab_last_sig_cpct() %>% 
        tab_pivot() %>%
        set_caption(i) %>% 
        htmlTable() %>% 
        print()  
    }

I expect the name of the variable in the output (Hair, Eye, Color) but instead i get only "get(i)". Thanks for any advice


Solution

  • After get we can not to know original variable name. The simplest way to show original name is to set variable name as label:

    require(expss)
    data(HairEyeColor)
    HairEyeColor <- as.data.frame(HairEyeColor)
    colnoms <-  names(HairEyeColor)
    
    expss_digits(2)
    
    for (i in colnoms){      
        # if we don't have label we assign name as label
        if(is.null(var_lab(HairEyeColor[[i]])))  var_lab(HairEyeColor[[i]]) = i
        HairEyeColor %>%
            tab_cells(get(i)) %>%
            tab_cols(Eye) %>% 
            tab_stat_cpct() %>%
            tab_last_sig_cpct() %>% 
            tab_pivot() %>%
            set_caption(i) %>% 
            htmlTable() %>% 
            print()  
    }