inst/doc/programming.R

## ----setup, echo = FALSE, message = FALSE-------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
options(tibble.print_min = 6L, tibble.print_max = 6L)
set.seed(1014)

# Manually "import"; only needed for old dplyr which uses old tidyselect
# which doesn't attach automatically in tidy-select contexts
all_of <- tidyselect::all_of

## -----------------------------------------------------------------------------
library(tidyr)

iris %>%
  nest(data = !Species)

## -----------------------------------------------------------------------------
packageVersion("tidyr")

mini_iris <- as_tibble(iris)[c(1, 2, 51, 52, 101, 102), ]
mini_iris

## -----------------------------------------------------------------------------
nest_egg <- function(df, cols) {
  nest(df, egg = {{ cols }})
}

nest_egg(mini_iris, !Species)

## -----------------------------------------------------------------------------
nest_egg <- function(df, cols) {
  nest(df, egg = all_of(cols))
}

vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
nest_egg(mini_iris, vars)

## -----------------------------------------------------------------------------
sel_vars <- function(df, cols) {
  tidyselect::eval_select(rlang::enquo(cols), df)
}
sel_vars(mini_iris, !Species)

Try the tidyr package in your browser

Any scripts or data that you put into this service are public.

tidyr documentation built on Feb. 16, 2023, 7:40 p.m.