withLocale.RdEvaluate an R expression with locale set temporarily.
withLocale(expr, category, locale, ..., substitute=TRUE, envir=parent.frame())The R expression to be evaluated.
A character string specifying the category to use.
character vector specifying the locale to used. The
first successfully set one will be used.
Not used.
If TRUE, argument expr is
substitute():ed, otherwise not.
The environment in which the expression should be evaluated.
Returns the results of the expression evaluated.
Internally, eval() is used to evaluate the expression.
and Sys.setlocale() to set locale.
# Vector
cat("Original vector:\n")
#> Original vector:
x <- c(letters[1:8], LETTERS[1:8])
print(x)
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "A" "B" "C" "D" "E" "F" "G" "H"
cat("Sorting with 'C' locale:\n")
#> Sorting with 'C' locale:
y1 <- withLocale(sort(x), "LC_COLLATE", "C")
print(y1)
#> [1] "A" "B" "C" "D" "E" "F" "G" "H" "a" "b" "c" "d" "e" "f" "g" "h"
cat("Sorting with an 'English' locale:\n")
#> Sorting with an 'English' locale:
y2 <- withLocale(sort(x), "LC_COLLATE", c("en_US", "en_US.UTF8", "English_United States.1252"))
print(y2)
#> [1] "a" "A" "b" "B" "c" "C" "d" "D" "e" "E" "f" "F" "g" "G" "h" "H"