2
votes

I am trying to pipe into expss::uselabels().

A simple replicable example of what I'm trying to do (without the pipe), would be a labelled lm() model:

library(tidyverse)
library(expss)

df <- mtcars

df <-  apply_labels(df,
                  cyl =  "Number of Cylinders",
                  disp = "Displacement")

fit_1 <- df %>% 
  use_labels(lm(formula = mpg ~ disp + cyl))
summary(fit_1)

which gives labelled coefficients in the lm output:

# > Coefficients:
#>                       Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)           34.66099    2.54700  13.609 4.02e-14 ***
#> Displacement          -0.02058    0.01026  -2.007   0.0542 .  
#> `Number of Cylinders` -1.58728    0.71184  -2.230   0.0337 *  

My questions: can I first take an lm() model and then pipe into use_labels()? I've tried below, but I must be refering to the two paramaters incorrectly.

fit_1<- df %>% 
  lm(formula = mpg ~ disp + cyl) %>% 
  use_labels(data = .x, expr = .y)
1
Looking at the source, it uses substitute() to delay evaluation of expr, which I don't think will work with a pipe because the left hand side is always evaluated before the right hand side. Your last pipeline wouldn't work regardless, because df is gone by the time you get to use_labels(), and .x and .y aren't anything; the only special variable with pipes is . which is the evaluated LHS being passed in and used for specifying what parameter it should be passed to.alistaire
You could use magrittrs %T>% pipe , but then you would have to call your model twice: fit_3 <- df %T>% lm(mpg ~ disp + cyl, .) %>% use_labels(., lm(mpg ~ disp + cyl, .))TimTeaFan

1 Answers

1
votes

use_labels works in a very simple and straightforward way. It just replaces in the expression all variables names with their labels. Variables are searched in the first argument (data.frame). As @alistaire already said all this actions are performed before the evaluation of the supplied expression, e. g. before calculating result of the lm(formula = mpg ~ disp + cyl). So answer is on your question is 'No'. You cannot apply use_labels on the already calculated result.