Temporarily change locale settings.
with_locale(new, code)
local_locale(.new = list(), ..., .local_envir = parent.frame())
[any]
The results of the evaluation of the code
argument.
Setting the LC_ALL
category is currently not implemented.
withr
for examples
## Change locale for time:
df <- data.frame(
stringsAsFactors = FALSE,
date = as.Date(c("2019-01-01", "2019-02-01")),
value = c(1, 2)
)
with_locale(new = c("LC_TIME" = "es_ES"), code = plot(df$date, df$value))
#> Warning: OS reports request to set locale to "es_ES" cannot be honored
## Compare with:
# plot(df$date, df$value)
## Month names:
with_locale(new = c("LC_TIME" = "en_GB"), format(ISOdate(2000, 1:12, 1), "%B"))
#> Warning: OS reports request to set locale to "en_GB" cannot be honored
#> [1] "January" "February" "March" "April" "May" "June"
#> [7] "July" "August" "September" "October" "November" "December"
with_locale(new = c("LC_TIME" = "es_ES"), format(ISOdate(2000, 1:12, 1), "%B"))
#> Warning: OS reports request to set locale to "es_ES" cannot be honored
#> [1] "January" "February" "March" "April" "May" "June"
#> [7] "July" "August" "September" "October" "November" "December"
## Change locale for currencies:
with_locale(new = c("LC_MONETARY" = "it_IT"), Sys.localeconv())
#> Warning: OS reports request to set locale to "it_IT" cannot be honored
#> decimal_point thousands_sep grouping int_curr_symbol
#> "." "" "" "USD "
#> currency_symbol mon_decimal_point mon_thousands_sep mon_grouping
#> "$" "." "," "\003\003"
#> positive_sign negative_sign int_frac_digits frac_digits
#> "" "-" "2" "2"
#> p_cs_precedes p_sep_by_space n_cs_precedes n_sep_by_space
#> "1" "0" "1" "0"
#> p_sign_posn n_sign_posn
#> "1" "1"
with_locale(new = c("LC_MONETARY" = "en_US"), Sys.localeconv())
#> Warning: OS reports request to set locale to "en_US" cannot be honored
#> decimal_point thousands_sep grouping int_curr_symbol
#> "." "" "" "USD "
#> currency_symbol mon_decimal_point mon_thousands_sep mon_grouping
#> "$" "." "," "\003\003"
#> positive_sign negative_sign int_frac_digits frac_digits
#> "" "-" "2" "2"
#> p_cs_precedes p_sep_by_space n_cs_precedes n_sep_by_space
#> "1" "0" "1" "0"
#> p_sign_posn n_sign_posn
#> "1" "1"
## Ordering:
x <- c("bernard", "bérénice", "béatrice", "boris")
with_locale(c(LC_COLLATE = "fr_FR"), sort(x))
#> Warning: OS reports request to set locale to "fr_FR" cannot be honored
#> [1] "bernard" "boris" "béatrice" "bérénice"
with_locale(c(LC_COLLATE = "C"), sort(x))
#> [1] "bernard" "boris" "béatrice" "bérénice"