I found a way to use rename with tidyeval to allow renaming columns with a named vector or a named list. I think it is quite elegant and quite obvious when written down, but it took me a little while to get my head around it, as it uses the new tidyeval paradigm. And as I do not have a blog or anything, I thought that I would post it here in case anyone is interested.
library(rlang)
library(dplyr)
iris <- as_tibble(iris)
named_vector <- c('id' = 'Species',
'sepal' = 'Sepal.Length')
iris %>% rename(!!! named_vector) %>% head(2)
# A tibble: 2 x 5
sepal Sepal.Width Petal.Length Petal.Width id
<dbl> <dbl> <dbl> <dbl> <fctr>
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
named_list <- list('id' = 'Species',
'sepal' = 'Sepal.Length')
# If you want to select and rename at the same time, you can use select the same way
iris %>% select(!!! named_vector) %>% head(2)
# A tibble: 2 x 2
id sepal
<fctr> <dbl>
1 setosa 5.1
2 setosa 4.9
I think something of this kind could be included in the examples of dplyr::select / dplyr::rename because it is a use case that is not directly related to programming with dplyr. I think a user looking for that feature shouldn't have to go through the programming with dplyr vignette.
So my question is, should I make an issue to ask for it to be included in the examples? Or should I make a PR to propose it myself directly? Are those kind of PRs welcomed?
I would not recommend using seplyr. You will be much better off relying on a system that has a rich underlying theory - even if you don't understand all of it, over time you will find it easier to internalise because it's internally consistent, and it avoids a large class of potential bugs.
Yes, but I wanted to do so using a named vector or just instead of typing the argument directly. For that, !!! or UQS works perfectly and I wanted yo share that
Speaking of the underlying theory, could you point to resources giving more details? Not about the R implementation, but the principles of quasi quotation, if I get that right
Perhaps I'm missing something, but how is trying to apply a standard evaluation approach internally inconsistent? Scopes seem much more well defined if you're passing around strings that refer to columns that rather than barewords. Just to be clear, I don't advocating putting entire expressions into strings, but rather basic things like rename_se(), group_by_se(), which takes strings, rather than barewords.
Because it only solves a small set of problems that you can already solve in other ways. Learning to use symbols instead of strings is not hard, and leads more naturally to more advanced techniques. I believe string manipulation is fundamentally the wrong approach for code generation.
Overall, I think seplyr makes the problem worse by providing yet another way to work with dplyr functions. It's not officially supported by the tidyverse team, we're not going to teach people how to use it, fewer people are going to understand it, and hence fewer people will be able to work with code that uses it.
it seems like rename from seplyr is using tidy eval and !!! under the hood to achieve what it does. I think it is better to learn than the underlying principle how things are achieved, which in turn can open more doors in other situations.