r-lib/withr

`with_collate` does not consider environment variable

Closed this issue · 1 comments

This is an offspring of r-lib/devtools#2377. Consider this example:

env -i LC_COLLATE=  R -q -e 'withr::with_collate("en_US", sort(c("_a", "a")))' # normal run
env -i LC_COLLATE=C R -q -e 'withr::with_collate("en_US", sort(c("_a", "a")))' # as run by R CMD check

On Linux, these two output different results:

> withr::with_collate("en_US", sort(c("_a", "a")))
[1] "_a" "a" 
> withr::with_collate("en_US", sort(c("_a", "a")))
[1] "a"  "_a"

Since the documentation of with_collate does not say anything about the need to take existing environment variables into account, I wonder if Sys.setenv should be added to ensure reproducibility regardless of environment variables.

I think this is because of https://github.com/wch/r-source/blob/0d8003afefdfbcfe95db51d88c6b6ab8925c4a05/src/main/util.c#L2458-L2468

R supports setting LC_COLLATE to C via an envvar. When that is the case, the envvar has precedence over the currently set locale.

Your suggestion to set the envvar in addition to the locale makes sense, I'll do that.