Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
115 lines (95 sloc) 3.54 KB
---
layout: post
categories: blog
title: "Tidy evaluation, most common actions"
base-url: https://EdwinTh.github.io
date: "2017-08-25 20:30:00"
output: html_document
tags: [R, dplyr, tidy evaluation, programming]
---
Tidy evaluation is a bit challenging to get your head around. Even after reading [programming with dplyr](https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html) several times, I still struggle when creating functions from time to time. I made a small summary of the most common actions I perform, so I don't have to dig in the vignettes and on stackoverflow over and over. Each is accompanied with a minimal example on how to implement it. I thought others might find this useful too, so here it is in a blog post. This list is meant to be a living thing so additions and improvements are most welcome. Please do a PR on [this file](https://github.com/EdwinTh/EdwinTh.github.io/tree/master/_source/2017-08-24-dplyr-recipes.Rmd) or send an email.
```{r, warning=FALSE, message=FALSE}
library(tidyverse)
```
#### bare to quosure: `quo`
```{r}
bare_to_quo <- function(x, var){
x %>% select(!!var) %>% head(1)
}
bare_to_quo(mtcars, quo(cyl))
```
#### bare to quosure in function: `enquo`
```{r}
bare_to_quo_in_func <- function(x, var) {
var_enq <- enquo(var)
x %>% select(!!var_enq) %>% head(1)
}
bare_to_quo_in_func(mtcars, mpg)
```
#### quosure to a name: `quo_name`
```{r}
bare_to_name <- function(x, nm) {
nm_name <- quo_name(nm)
x %>% mutate(!!nm_name := 42) %>% head(1) %>%
select(!!nm)
}
bare_to_name(mtcars, quo(this_is_42))
```
#### quosure to text: `quo_text`
```{r}
quo_to_text <- function(x, var) {
var_enq <- enquo(var)
glue::glue("The following column was selected: {rlang::quo_text(var_enq)}")
}
quo_to_text(mtcars, cyl)
```
#### character to name: `sym` (edited)
```{r}
char_to_quo <- function(x, var) {
var_enq <- rlang::sym(var)
x %>% select(!!var_enq) %>% head(1)
}
char_to_quo(mtcars, "vs")
```
#### multiple bares to quosure: `quos`
```{r}
bare_to_quo_mult <- function(x, ...) {
grouping <- quos(...)
x %>% group_by(!!!grouping) %>% summarise(nr = n())
}
bare_to_quo_mult(mtcars, vs, cyl)
```
#### multiple characters to names: `syms` (edited)
```{r}
bare_to_quo_mult_chars <- function(x, ...) {
grouping <- rlang::syms(...)
x %>% group_by(!!!grouping) %>% summarise(nr = n())
}
bare_to_quo_mult_chars(mtcars, list("vs", "cyl"))
```
#### quoting full expression
Although quoting column names is most often used, it is by no means the only option. We can use the above to quote full expressions.
```{r}
filter_func <- function(x, filter_exp) {
filter_exp_enq <- enquo(filter_exp)
x %>% filter(!!filter_exp_enq)
}
filter_func(mtcars, hp == 93)
```
#### quoting full expression in a character: parse_expr
```{r}
filter_by_char <- function(x, char) {
func_call <- rlang::parse_expr(char)
x %>% filter(!!func_call)
}
filter_by_char(mtcars, "cyl == 6") %>% head(1)
```
#### Edit notes
1) I mistakingly thought that `rlang::sym(s)` created quosures. However, as pointed out to me by a reader, this creates a `name`, not a `quosure`. A `name` however can also be unquoted. See this [github discussion](https://github.com/tidyverse/rlang/issues/116).
```{r}
just_a_name <- rlang::sym("cyl")
class(just_a_name)
mtcars %>% select(!!just_a_name) %>% head(1)
```
2) Quoting expressions in a character was added roughly a year after the first apperance of this blog.
3) The `quo_text` example used to be with `ggplot2`. However, since v.3.0.0 it supports tidy evaluation. Therefore, the example was changed and now it uses `glue`.
You can’t perform that action at this time.