<- function(data, var) {
var_summary |>
data summarise(
min = min(var),
max = max(var)
)
}
|>
mtcars group_by(cyl) |>
var_summary(mpg)
Using the tidyverse in your package π§Ή
Tidyverse
Demo - not part of package
Do in a new file eg scratch/tidyverse-testing.R and add scratch to Rbuildignore
data masking
Error: object mpg not found
<- function(data, var) {
var_summary |>
data summarise(
min = min({{ var }}),
max = max({{ var }})
)
}
|>
mtcars group_by(cyl) |>
var_summary(mpg)
<- function(var) {
big_cars_summary |>
mtcars filter(.data$cyl >= 6) |>
group_by(.data$cyl) |>
summarise(
n = n(),
mean = mean({{ var }}),
)
}
big_cars_summary(disp)
Your turn solution
<- function(data, group_var) {
height_sum |>
data ::group_by({{ group_var }}) |>
dplyr::summarise(
dplyrn = dplyr::n(),
mean_height = mean(.data$height)
)
}
height_sum(starwars, hair_color)
Your turn solution
<- function(data, ...) {
height_sum |>
data ::group_by(...) |>
dplyr::summarise(
dplyrn = dplyr::n(),
mean_height = mean(.data$height)
)
}
height_sum(starwars, hair_color, eye_color)
Dynamic dots
<- function(data, var) {
var_summary |>
data summarise(
"{{var}}_min" := min({{ var }})
)
}
|>
mtcars group_by(cyl) |>
var_summary(mpg)
Your turn solution
<- function(data, group_var, sum_var) {
dynamic_sum |>
data ::group_by({{ group_var }}) |>
dplyr::summarise(
dplyrn = dplyr::n(),
"mean_{{sum_var}}" := mean({{ sum_var }})
)
}
dynamic_sum(starwars, hair_color, mass)
tidyselect
|>
mtcars ::group_by(cyl) |>
dplyr::summarise(
dplyr::across(c("mpg", "disp"), mean)
dplyr
)
<- c("mpg", "disp", "xyz")
cols |>
mtcars ::group_by(cyl) |>
dplyr::summarise(
dplyr::across(dplyr::all_of(cols), mean)
dplyr
)
<- c("mpg", "disp", "xyz")
cols |>
mtcars ::group_by(cyl) |>
dplyr::summarise(
dplyr::across(dplyr::any_of(cols), mean)
dplyr )
Using a character vector:
<- function(df, cols) {
summy |>
df group_by(.data$cyl) |>
summarise(
across(all_of(cols), .fns = mean)
) }
summy(mtcars, c("mpg", "disp"))
Using bare names or tidy-select helpers:
<- function(df, cols) {
summy |>
df group_by(.data$cyl) |>
summarise(
across({{ cols }}, .fns = mean)
) }
summy(mtcars, c(mpg, disp))
summy(mtcars, starts_with("mp"))
summy(mtcars, where(is.numeric))
Tidyversify libminer
use_package("dplyr")
We are using the new base pipe, need R >= 4.1
<- function(by) {
lib_summary <- lib()
pkgs
::group_by(pkgs, by) |>
dplyr::count()
dplyr }
load_all()
lib_summary(LibPath)
Errors - no column called βbyβ
2a - use curly-curly with bare names
<- function(by) {
lib_summary <- lib()
pkgs
::group_by(pkgs, {{ by }}) |>
dplyr::count()
dplyr }
load_all()
lib_summary(LibPath)
2b - use .data[[x]] with character
use_import_from("rlang", ".data")
<- function(by) {
lib_summary <- lib()
pkgs
::group_by(pkgs, .data[[by]]) |>
dplyr::count()
dplyr }
load_all()
lib_summary("License")
3 - add sizes
<- function(by) {
lib_summary <- lib()
pkgs
<- calculate_sizes(pkgs)
pkgs
::group_by(pkgs, {{by}}) |>
dplyr::summarise(
dplyrn = dplyr::n(),
size = sum(size)
)
}
<- function(df) {
calculate_sizes |>
df ::mutate(
dplyrsize = purrr::map_dbl(
::path(LibPath, Package),
fssum(fs::file_size(fs::dir_ls(x, recurse = TRUE)))
\(x)
)
) }
load_all()
lib_summary(LibPath)
check() # should throw notes about undefined globals
4 - add .data
<- function(by) {
lib_summary <- lib()
pkgs
<- calculate_sizes(pkgs)
pkgs
::group_by(pkgs, {{by}}) |>
dplyr::summarise(
dplyrn = dplyr::n(),
size = sum(.data$size)
)
}
<- function(df) {
calculate_sizes |>
df ::mutate(
dplyrsize = purrr::map_dbl(
::path(.data$LibPath, .data$Package),
fssum(fs::file_size(fs::dir_ls(x, recurse = TRUE)))
\(x)
)
) }
5 use β¦ for multiple byβs
<- function(...) {
lib_summary lib() |>
calculate_sizes() |>
::group_by(...) |>
dplyr::summarise(
dplyrn = dplyr::n(),
size = sum(.data$size)
) }
Final - make sizes conditional again
Also, drop groups so we donβt get the message any more
<- function(..., sizes = FALSE) {
lib_summary if (!is.logical(sizes)) {
stop("'sizes' must be logical (TRUE/FALSE).", call. = FALSE)
}
lib() |>
calculate_sizes(do_calc = sizes) |>
::group_by(...) |>
dplyr::summarise(
dplyrn = dplyr::n(),
::across(dplyr::any_of("size"), sum),
dplyr.groups = "drop"
)
}
<- function(df, do_calc) {
calculate_sizes if (!isTRUE(do_calc)) return(df)
|>
df ::mutate(
dplyrsize = purrr::map_dbl(
::path(.data$LibPath, .data$Package),
fssum(fs::file_size(fs::dir_ls(x, recurse = TRUE)))
\(x)
)
)
}
<- function() {
lib ::installed.packages() |>
utils::as_tibble()
dplyr
}
lib_summary(LibPath, License)
lib_summary(LibPath, License, sizes = TRUE)
lib_summary(LibPath, License, sizes = 10)
test()
# update tests
check()
# update documentation
CLI
Final
<- function(..., sizes = FALSE) {
lib_summary if (!is.logical(sizes)) {
::cli_abort("You supplied {.val {sizes}} to {.var sizes}. It should be a {.cls logical} value, not {.obj_type_friendly {sizes}}.")
cli
}
lib() |>
calculate_sizes(do_calc = sizes) |>
::group_by(...) |>
dplyr::summarise(
dplyrn = dplyr::n(),
::across(dplyr::any_of("size"), sum),
dplyr.groups = "drop"
)
}
<- function(df, do_calc) {
calculate_sizes if (!isTRUE(do_calc)) return(df)
::cli_inform(c("i" = "Calculating sizes..."))
cli
|>
df ::mutate(
dplyrsize = purrr::map_dbl(
::path(.data$LibPath, .data$Package),
fssum(fs::file_size(fs::dir_ls(x, recurse = TRUE)))
\(x)
)
) }
lib_summary(LibPath, License)
lib_summary(LibPath, License, sizes = TRUE)
lib_summary(LibPath, License, sizes = 10)
lib_summary(LibPath, License, sizes = "hello")
lib_summary(LibPath, License, sizes = NULL)
update tests
- snapshot for error
- groups