Home > front end >  RMarkdown: how to get english error messages on a localised system
RMarkdown: how to get english error messages on a localised system

Time:08-15

I am writing a vignette in RMarkdown and I want to point out a programming mistake.

listofnumbers <- as.list(1:4)
listofnumbers[2]   listofnumbers[3]

This code snippet produces an error:

Error in listofnumbers[2] listofnumbers[3] : non-numeric argument to binary operator

However, I am working on a system localised to German. The error message I get is in German:

Error in listofnumbers[2] listofnumbers[3]: nicht-numerisches Argument für binären Operator

If anyone renders the vignette themselves that may be good. But to pre-render it with the package I would need the English message.

Also, I do not want to change my system settings permanently to English.

I investigated using an output hook. But I do not know how to translate the German message to English.

I am working on Windows 10.

CodePudding user response:

If the environment variable LANGUAGE is set to en, error messages will be displayed in English. This needs to be in place at the time R starts.

If you are using RStudio, then it starts a new copy of R to process vignettes when you click on Knit, so you can do this in the current session using

Sys.setenv(LANGUAGE="en")

That should also work if the vignette is processed when you build the package as long as you start a new session to do the build. By default devtools::build() and related functions do that.

If you are running RMarkdown within the current session (e.g. by calling rmarkdown::render()) that won't work, because it happens too late. In that case you can use OS features to set the environment variable, or set it in the .Renviron file in your home directory as

LANGUAGE=en

Since you say you are on Windows, you have to do this carefully: Windows doesn't like filenames like .Renviron, and may try to change it to something else.

This method should work if you use R CMD build to build your packages outside of RStudio.

  • Related