### Load Required Packages
```{r, setup}
# install.packages("tidyverse")
# install.packages("gapminder")

library(tidyverse)  
library(gapminder)  
data(gapminder)  
	
# Check dplyr install (should be >= 0.7.0)  
packageVersion("dplyr")
```


### Example 1: quo(), !! and UQ()
```{r, ex1}
my_var <- quo(continent)
# not my_var <- "continent"

gapminder %>% 
	group_by(!!my_var) %>% 	summarise_at(vars(gdpPercap), mean)

gapminder %>% filter(UQ(my_var) == "Asia")
# not filter(!!my_var == "Asia")
```


### Example 1': quo(), !!! and UQS()
```{r, ex1prime}
# install.packages("stringr")
# stringr comes with tidyverse
library(stringr)
start <- quo(1)
end <- quo(4)
args <- list(start = start, end = end)

gapminder %>% 
  mutate(ShortName = str_sub(country, !!!args))
```


### Example 2: Setting variable names
```{r, ex2}
# Use strings or quo() derivatives
str_name <- "neato_gdp"
name <- quo_name(quo(gdp))
custom <- paste0("neato_", name)


gapminder %>%
	mutate(!!custom := pop * gdpPercap)

gapminder %>%
	mutate(!!str_name := pop * gdpPercap)
```


### Example 3: enquo()
```{r, ex3}
# write functions that take barewords
# as parameters

make_real_log <- function(df, incol){
	incol <- enquo(incol)
	name <- paste0(quo_name(incol), "_log")
	df %>% 
		mutate(!!name := log2(1 + !!incol))
}

gapminder %>% make_real_log(pop)
```


### Example 4: the .data pronoun
```{r, ex4}
# use strings instead of quo()
my_var <- "continent"

gapminder %>% 
	group_by(.data[[my_var]]) %>% 	summarise_at(vars(gdpPercap), mean)

# prevent silent failure / R CMD check error
mutate_gdp <- function(df) {
  mutate(df, gdp = .data$gdpPercap * .data$pop)
} 

gapminder %>%
  mutate_gdp()
```


### Example 5: tidbits
```{r, ex5}
# pull() returns a vector
gapminder %>%
	pull(country) %>%
	unique()

# "_if"" verb suffix
gapminder %>% summarise_if(is.numeric, mean)
gapminder %>% mutate_if(is.numeric, mean)

# and tidyeval is coming to ggplot2 & tidyr!
```

### Example 6: my problem / Shiny user inputs
```{r, ex6}
user_input = "country"

gapminder %>% group_by(.data[[user_input]]) %>% summarise(new = mean(lifeExp))

gapminder %>% group_by(!!user_input := .data[[user_input]]) %>% summarise(new = mean(lifeExp)) # dplyr issue 2991

library(rlang)
gapminder %>% group_by_(user_input) %>% summarise(new = mean(lifeExp))
```