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

with_locale

Locale settings


Description

Temporarily change locale settings.

Usage

with_locale(new, code)

local_locale(.new = list(), ..., .local_envir = parent.frame())

Arguments

new, .new

[named character]
New locale settings

code

[any]
Code to execute in the temporary environment

...

Additional arguments with locale settings.

.local_envir

[environment]
The environment to use for scoping.

Details

Setting the LC_ALL category is currently not implemented.

Value

[any]
The results of the evaluation of the code argument.

See Also

withr for examples

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))
## Compare with:
#  plot(df$date, df$value)

## Month names:
with_locale(new = c("LC_TIME" = "en_GB"), format(ISOdate(2000, 1:12, 1), "%B"))
with_locale(new = c("LC_TIME" = "es_ES"), format(ISOdate(2000, 1:12, 1), "%B"))

## Change locale for currencies:
with_locale(new = c("LC_MONETARY" = "it_IT"), Sys.localeconv())
with_locale(new = c("LC_MONETARY" = "en_US"), Sys.localeconv())

## Ordering:
x <- c("bernard", "bérénice", "béatrice", "boris")
with_locale(c(LC_COLLATE = "fr_FR"), sort(x))
with_locale(c(LC_COLLATE = "C"), sort(x))

withr

Run Code 'With' Temporarily Modified Global State

v2.4.2
MIT + file LICENSE
Authors
Jim Hester [aut, cre], Kirill Müller [aut], Kevin Ushey [aut], Hadley Wickham [aut], Winston Chang [aut], Jennifer Bryan [ctb], Richard Cotton [ctb], RStudio [cph]
Initial release

We don't support your browser anymore

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