Search code examples
rloggingoutputmute

R: mute output of Sys.setlocale()


I run a script Rscript script.R > logfile.of.script.txt

in the script i set

Sys.setlocale("LC_ALL", "en_US.UTF-8")

which returns

[1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C"

however this text persists in the log files which i don't want to have. How can I have Sys.setlocale("LC_ALL", "en_US.UTF-8") run quietly without any output to the logfile. Sys.setlocale() does not see, to have a quiet switch to suppress output.


Solution

  • Assign it or wrap in invisible(.):

    Sys.setlocale("LC_TIME", "English_United States.1252")
    # [1] "English_United States.1252"
    invisible(Sys.setlocale("LC_TIME", "English_United States.1252"))
    ign <- Sys.setlocale("LC_TIME", "English_United States.1252")
    

    While this function does have side-effect, it also returns the new value. Since it is doing partial-matching to find the best, the return value is not always the same as the argument provided. For instance,

    Sys.setlocale("LC_TIME", "English")
    # [1] "English_United States.1252"
    

    shows what I wanted and what I got ... so the most-paranoid approach might be to make sure you got what you requested (in case the partial-matching is over-eager). I've never been in a situation where that level of paranoia was required, but then again I'm fortunate that all of my work is in a single language (my primary language) with no multi-lingual users.