Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a wrapper function that uses tidyverse-like syntax

I’m trying to create a wrapper function that will let me use tidyverse-like syntax (i.e. replacing dat$col with dat, col) whilst simultaneously adding some default arguments. I’m struggling with the first part – likely because I don’t have a good grasp of base R (and maybe data masking?)

# What I would like to recreate
summary(mtcars$mpg)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   10.40   15.43   19.20   20.09   22.80   33.90

# My attempt
tidy_summary <- function(data, variable) {
        variable_name <- {{  variable  }}
        summary(data$variable_name)
}

tidy_summary(mtcars, mpg)
#> Error in tidy_summary(mtcars, mpg): object 'mpg' not found

Created on 2022-09-26 by the reprex package (v2.0.1)

I tried using the [ operator instead, or passing the arguments as strings, without luck.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do

tidy_summary <- function(data, variable) {
  
  summary(data[[deparse(substitute(variable))]])
}

tidy_summary(mtcars, mpg)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   10.40   15.43   19.20   20.09   22.80   33.90

The $ operator doesn’t work here because this is always interpreted as-is. That is, data$variable_name will always look for a column literally called variable_name. It will not be substituted for the name stored in the variable variable_name. Instead, we use the [[ operator and pass a string containing the actual column name, which we can get by deparse(substitute(variable))

Created on 2022-09-26 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading