Programming with dplyr

I have wirked through Hadley's paper called " Programming with dplyr" but can't get the following function to work. I have simplified it to illustrate the problem but it is part of a larger function that works except for this part.

library(dplyr) 

data <- tibble(a = c(1,2,3,4,5),
               b = c(1,2,3,4,5))
# This is the desired outcome
data %>%
    rename("b1" = b)

# This is the function
rename_var <- function(data, rename_text){
  data %>%
    rename(rename_text = b)
}

# call the function
rename_var(data = data, rename_text = "b1")

When I call the function then the variable is now named as rename_text and not b1.

Any assistance with this will be greatly appreciated.

Dawie

This seems to be an issue with non-standard-evaluation, dplyr, and functions.

I tweaked one line of your rename_var() function, and I think it works now

rename_var <- function(data, rename_text){
  data %>%
    rename({{rename_text}} := b)
}

I made two changes:

  1. Based on this article, I changed the = to a :=.
  2. Based on this article, I wrapped the rename_text variable with {{ ... }}
3 Likes

It works!!!

Thank you, appreciate it :grinning: