style
style copied to clipboard
Enforce variable desambiguation in style
Hi,
The name collision might cause mistakes when using pipes or some tidyverse functions.
In general, I would suggest the use of .data
and .env
to be added in style recommandation.
I would also suggest the use of dplyr::across
and tidyselect
for the dplyr::group_by
or similar features.
Below are examples:
################################################################################
library(magrittr)
alphabet<- tibble::tibble(
rank= seq_len(26),
letter= c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x","y", "z"),
type = factor(c("v", "c", "c", "c", "v", "c", "c", "c", "v", "c", "c", "c",
"c", "c", "v", "c", "c", "c", "c", "c", "v", "c", "c", "c", "v", "c"))
)
rank <- 5
letter = "type"
# BAD
# 1
dplyr::filter(alphapet, rank <= rank)
# 2
dplyr::group_by(alphabet, type) %>% dplyr::group_by(letter) %>% dplyr::count()
# GOOD
# 1
dplyr::filter(alphabet, .data$rank <= .env$rank)
# 2
dplyr::group_by(alphabet, type) %>%
dplyr::group_by(
dplyr::across(tidyselect::all_of(.env$letter)
)
) %>%
dplyr::count()