Skip to content

Commit 90f10c3

Browse files
authored
Deprecate options(dplyr.legacy_locale =) usage (#7766)
* Deprecate `options(dplyr.legacy_locale =)` usage * Add advice on figuring out what to put for `.locale`
1 parent 70cd3cd commit 90f10c3

File tree

14 files changed

+172
-60
lines changed

14 files changed

+172
-60
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@
159159

160160
* Using `by = character()` to perform a cross join. Deprecated in 1.1.0, use `cross_join()` instead.
161161

162+
* The following are newly deprecated:
163+
164+
* The `dplyr.legacy_locale` global option. If you used this to affect the ordering of `arrange()`, use `arrange(.locale =)` instead. If you used this to affect the ordering of `group_by() |> summarise()`, follow up with an additional call to `arrange(.locale =)` instead (#7760).
165+
162166
### Newly stable
163167

164168
* `.by` has moved from experimental to stable (#7762).

R/arrange.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#' grouped data frames only.
4242
#' @param .locale The locale to sort character vectors in.
4343
#'
44-
#' - If `NULL`, the default, uses the `"C"` locale unless the
44+
#' - If `NULL`, the default, uses the `"C"` locale unless the deprecated
4545
#' `dplyr.legacy_locale` global option escape hatch is active. See the
4646
#' [dplyr-locale] help page for more details.
4747
#'

R/group-by.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@
5555
#'
5656
#' ## Legacy behavior
5757
#'
58+
#' `r lifecycle::badge("deprecated")`
59+
#'
5860
#' Prior to dplyr 1.1.0, character vector grouping columns were ordered in the
59-
#' system locale. If you need to temporarily revert to this behavior, you can
60-
#' set the global option `dplyr.legacy_locale` to `TRUE`, but this should be
61-
#' used sparingly and you should expect this option to be removed in a future
62-
#' version of dplyr. It is better to update existing code to explicitly call
63-
#' `arrange(.locale = )` instead. Note that setting `dplyr.legacy_locale` will
64-
#' also force calls to [arrange()] to use the system locale.
61+
#' system locale. Setting the global option `dplyr.legacy_locale` to `TRUE`
62+
#' retains this legacy behavior, but this has been deprecated. Update existing
63+
#' code to explicitly call `arrange(.locale = )` instead. Run
64+
#' `Sys.getlocale("LC_COLLATE")` to determine your system locale, and compare
65+
#' that against the list in [stringi::stri_locale_list()] to find an appropriate
66+
#' value for `.locale`, i.e. for American English, `"en_US"`.
6567
#'
6668
#' @export
6769
#' @examples

R/locale.R

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#' ## Default locale
88
#'
99
#' The default locale used by `arrange()` is the C locale. This is used when
10-
#' `.locale = NULL` unless the `dplyr.legacy_locale` global option is set to
11-
#' `TRUE`. You can also force the C locale to be used unconditionally with
12-
#' `.locale = "C"`.
10+
#' `.locale = NULL` unless the deprecated `dplyr.legacy_locale` global option is
11+
#' set to `TRUE`. You can also force the C locale to be used unconditionally
12+
#' with `.locale = "C"`.
1313
#'
1414
#' The C locale is not exactly the same as English locales, such as `"en"`. The
1515
#' main difference is that the C locale groups the English alphabet by _case_,
@@ -31,15 +31,17 @@
3131
#'
3232
#' ## Legacy behavior
3333
#'
34-
#' Prior to dplyr 1.1.0, character columns were ordered in the system locale. If
35-
#' you need to temporarily revert to this behavior, you can set the global
36-
#' option `dplyr.legacy_locale` to `TRUE`, but this should be used sparingly and
37-
#' you should expect this option to be removed in a future version of dplyr. It
38-
#' is better to update existing code to explicitly use `.locale` instead. Note
39-
#' that setting `dplyr.legacy_locale` will also force calls to [group_by()] to
40-
#' use the system locale when internally ordering the groups.
34+
#' `r lifecycle::badge("deprecated")`
4135
#'
42-
#' Setting `.locale` will override any usage of `dplyr.legacy_locale`.
36+
#' Prior to dplyr 1.1.0, character columns were ordered in the system locale.
37+
#' Setting the global option `dplyr.legacy_locale` to `TRUE` retains this legacy
38+
#' behavior, but this has been deprecated. Update existing code to explicitly
39+
#' call `arrange(.locale = )` instead. Run `Sys.getlocale("LC_COLLATE")` to
40+
#' determine your system locale, and compare that against the list in
41+
#' [stringi::stri_locale_list()] to find an appropriate value for `.locale`,
42+
#' i.e. for American English, `"en_US"`.
43+
#'
44+
#' Setting `.locale` directly will override any usage of `dplyr.legacy_locale`.
4345
#'
4446
#' @name dplyr-locale
4547
#' @keywords internal
@@ -64,30 +66,48 @@
6466
#'
6567
#' # Using `"da"` for Danish ordering gives the expected result
6668
#' arrange(df, x, .locale = "da")
67-
#'
68-
#' # If you need the legacy behavior of `arrange()`, which respected the
69-
#' # system locale, then you can set the global option `dplyr.legacy_locale`,
70-
#' # but expect this to be removed in the future. We recommend that you use
71-
#' # the `.locale` argument instead.
72-
#' rlang::with_options(dplyr.legacy_locale = TRUE, {
73-
#' arrange(df, x)
74-
#' })
7569
NULL
7670

7771
dplyr_legacy_locale <- function() {
7872
# Used to determine if `group_by()` and `arrange()` should use
7973
# base R's `order()` for sorting, which respects the system locale and was
8074
# our sorting engine pre-1.1.0.
81-
out <- peek_option("dplyr.legacy_locale") %||% FALSE
75+
option <- peek_option("dplyr.legacy_locale")
76+
77+
if (is_null(option)) {
78+
# Default behavior uses C locale
79+
return(FALSE)
80+
}
81+
82+
# This deprecation is a bit special. Since it is a global option that only the
83+
# end user would ever set, we set `user_env = globalenv()` so that it always
84+
# looks like a "direct" usage to lifecycle. This also makes our lives easier,
85+
# because `user_env` would have to be threaded all the way up through the
86+
# exported `grouped_df()` function, which is then used in many places
87+
# throughout dplyr. Additionally, we've bypassed `deprecate_soft()` and gone
88+
# straight to `deprecate_warn()` since this is only an end user facing option.
89+
lifecycle::deprecate_warn(
90+
when = "1.2.0",
91+
what = I("`options(dplyr.legacy_locale =)`"),
92+
details = c(
93+
i = "If needed for `arrange()`, use `arrange(.locale =)` instead.",
94+
i = "If needed for `group_by() |> summarise()`, follow up with an additional `arrange(.locale =)` call.",
95+
i = cli::format_inline(paste0(
96+
"Use {.run Sys.getlocale(\"LC_COLLATE\")} to determine your system locale, ",
97+
"and compare against {.run stringi::stri_locale_list()} to determine the `.locale` value to use."
98+
))
99+
),
100+
user_env = globalenv()
101+
)
82102

83-
if (!is_bool(out)) {
103+
if (!is_bool(option)) {
84104
abort(
85105
"Global option `dplyr.legacy_locale` must be a single `TRUE` or `FALSE`.",
86106
call = NULL
87107
)
88108
}
89109

90-
out
110+
option
91111
}
92112

93113
has_minimum_stringi <- function() {

man/arrange.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/arrange_all.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dplyr-locale.Rd

Lines changed: 13 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/group_by.Rd

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/arrange.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,14 @@
6565
Error in `arrange()`:
6666
! `desc()` must be called with exactly one argument.
6767

68+
# legacy - using the deprecated global option `dplyr.legacy_locale` forces the system locale
69+
70+
Code
71+
out <- arrange(df, x)$x
72+
Condition
73+
Warning:
74+
`options(dplyr.legacy_locale =)` was deprecated in dplyr 1.2.0.
75+
i If needed for `arrange()`, use `arrange(.locale =)` instead.
76+
i If needed for `group_by() |> summarise()`, follow up with an additional `arrange(.locale =)` call.
77+
i Use `Sys.getlocale("LC_COLLATE")` to determine your system locale, and compare against `stringi::stri_locale_list()` to determine the `.locale` value to use.
78+

tests/testthat/_snaps/grouped-df.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,25 @@
103103
Error in `grouped_df()`:
104104
! `vars` must be a character vector.
105105

106+
# using the deprecated global option `dplyr.legacy_locale` forces the system locale
107+
108+
Code
109+
result <- compute_groups(df, "x")
110+
Condition
111+
Warning:
112+
`options(dplyr.legacy_locale =)` was deprecated in dplyr 1.2.0.
113+
i If needed for `arrange()`, use `arrange(.locale =)` instead.
114+
i If needed for `group_by() |> summarise()`, follow up with an additional `arrange(.locale =)` call.
115+
i Use `Sys.getlocale("LC_COLLATE")` to determine your system locale, and compare against `stringi::stri_locale_list()` to determine the `.locale` value to use.
116+
117+
---
118+
119+
Code
120+
result <- group_by(df, x)
121+
Condition
122+
Warning:
123+
`options(dplyr.legacy_locale =)` was deprecated in dplyr 1.2.0.
124+
i If needed for `arrange()`, use `arrange(.locale =)` instead.
125+
i If needed for `group_by() |> summarise()`, follow up with an additional `arrange(.locale =)` call.
126+
i Use `Sys.getlocale("LC_COLLATE")` to determine your system locale, and compare against `stringi::stri_locale_list()` to determine the `.locale` value to use.
127+

0 commit comments

Comments
 (0)