Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

expand.tbl_lazy

Expand SQL tables to include all possible combinations of values


Description

This is a method for the tidyr::expand generics. It doesn't sort the result explicitly, so the order might be different to what expand() returns for data frames.

Usage

expand.tbl_lazy(data, ..., .name_repair = "check_unique")

Arguments

data

A lazy data frame backed by a database query.

...

Specification of columns to expand. See tidyr::expand for more details.

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence,

  • "unique": Make sure names are unique and not empty,

  • "check_unique": (default value), no name repair, but check they are unique,

  • "universal": Make the names unique and syntactic

  • a function: apply custom name repair (e.g., .name_repair = make.names for names in the style of base R).

  • A purrr-style anonymous function, see rlang::as_function()

This argument is passed on as repair to vctrs::vec_as_names(). See there for more details on these terms and the strategies used to enforce them.

Value

Another tbl_lazy. Use show_query() to see the generated query, and use collect() to execute the query and return data to R.

Examples

if (require("tidyr", quietly = TRUE)) {
  fruits <- memdb_frame(
    type   = c("apple", "orange", "apple", "orange", "orange", "orange"),
    year   = c(2010, 2010, 2012, 2010, 2010, 2012),
    size = c("XS", "S",  "M", "S", "S", "M"),
    weights = rnorm(6)
  )

  # All possible combinations ---------------------------------------
  fruits %>% expand(type)
  fruits %>% expand(type, size)

  # Only combinations that already appear in the data ---------------
  fruits %>% expand(nesting(type, size))
}

dbplyr

A 'dplyr' Back End for Databases

v2.1.1
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre], Maximilian Girlich [aut], Edgar Ruiz [aut], RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.