RMarkdown: how to get english error messages on a localised system - r

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.

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

Accent in my user folder generates an error in execution of read_csv()

I'm having trouble executing the routine read_csv(), I'm pretty sure it is because my user folder has an accent and that gives an 'file not found' error:
> statics <-read_csv("statics.csv")
Error: Timezone database not found at
"C:/Users/Administracién/Documents/R/win-library/4.1/tzdb/tzdata"
In the route there is a 'é' that I assume is causing the problem. I can't change the name of my user folder without making many of the programs of my system crash, so I don't know how to solve this... Any ideas how to solve it?
I tried editing the environmental variables, setting:
R_LIBS_USER C:/Users/Administración/Documents/R/win-library/4.1
And with this, as before, I can load libraries as tidyverse, installed there, without problems, but when I use that read_csv() routine crashes because it searches it in the folder changing the accent for those strange character.
The two symbols you are using in your pathway aren't recognized by R.
R can recognize them with the unicode symbol.
à is "\u0200" in R code
© is "\u00a9" in R code
Does this work with your pathway?
setwd(paste("C:/Users/Administraci","\u0200","\u00a9","n/Documents/R/win-library/4.1/tzdb/tzdata",sep=""))
read_csv("statics.csv")

I am getting an error that I do not understand when I try to knit documents on RStudio

I am a relatively new R user. I've been knitting documents in the past using RStudio to create HTML and PDF outputs of my R files.
I have no idea what changed, but approximately 1 month ago I tried to knit a document and got the error portrayed in the picture. I am borderline-competent at R on my best days but otherwise have virtually no knowledge of anything "computer" or "coding" outside of what I've learned from DataCamp regarding R. I have been trying to figure out what happened so that I can continue knitting files, but legitimately do not understand.
I would be more than happy to provide any additional information/context that you need to help me solve this problem but, unfortunately, I just don't know where to start or what's required.
I know this is a bit of a bullshit question and I've tried to avoid asking about it for a while b/c I know I don't know enough to ask it correctly, but I'm at a point where I just want to start knitting again and I'll do whatever it takes to get back to that!
The things I've tried so far are:
Downloading and installing the newest version of R
Downloading and installing RStudio again
Downloading and installing MacTeX again
Trying random solutions from Stack Overflow that involve writing things in my "Terminal" (which I subsequently erase when they don't work).
I am using a MacBook Pro running macOS Catalina 10.15.7 for what it's worth.
I've copied and pasted the text of the error message below:
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
Calls: source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'renv/activate.R': No such file or directory
Execution halted
I had the same error and didn't want to run my Rmd files from the console, so there is the solution for using the button again (which I found here):
> usethis::edit_r_profile()
* Modify 'C:/Users/User/Documents/.Rprofile'
* Restart R for changes to take effect
The output then shows you the path to your file. Just copy that path to the address bar in your explorer and choose the program you want to open it with. I chose the normal text editor. Delete "source('renv/activate.R.')" from this file, restart R, and everything should work fine again.

R Console Language Setting [duplicate]

My error messages are displayed in French. How can I change my system language setting so the error messages will be displayed in English?
You can set this using the Sys.setenv() function. My R session defaults to English, so I'll set it to French and then back again:
> Sys.setenv(LANG = "fr")
> 2 + x
Erreur : objet 'x' introuvable
> Sys.setenv(LANG = "en")
> 2 + x
Error: object 'x' not found
A list of the abbreviations can be found here.
Sys.getenv() gives you a list of all the environment variables that are set.
In the case of RStudio for Windows I succeeded in changing the language following the instructions found in R for Windows FAQ, in particular I wrote:
language = EN
inside the file Rconsole (in my installation it is C:\Program Files\R\R-2.15.2\etc\Rconsole); this works also for the command Rscript.
For example you can locate the Rconsole file with this two commands from a command prompt:
cd \
dir Rconsole /s
The first one make the root as the current directory, the second one looks for the Rconsole file.
In the following screenshot you have that Rconsole file is in the folder C:\Program Files\R\R-3.4.1\etc.
You may have more than one location, in that case you may edit all the Rconsole files.
After that you can open the Rconsole file with your favorite editor and look for the line language = and then append EN at the end of that line.
In the following screenshot the interesting line is the number 70 and you have to append EN at the end of it.
For mac users, I found this on the R for Mac FAQ
If you use a non-standard setup (e.g. different language than
formats), you can override the auto-detection performed by setting
`force.LANG' defaults setting, such as for example
defaults write org.R-project.R force.LANG en_US.UTF-8
when run in Terminal it will enforce US-english setting regardless of the system
setting. If you don't know what Terminal is you can use this R command
instead:
system("defaults write org.R-project.R force.LANG en_US.UTF-8")
but do not forget to quit R and start R.app again afterwards. Please
note that you must always use `.UTF-8' version of the locale,
otherwise R.app will not work properly.
This helped me to change my console language from Chinese to English.
This works from command line :
$ export LANG=en_US.UTF-8
None of the other answers above worked for me
If you use Ubuntu you will set
LANGUAGE=en
in /etc/R/Renviron.site.
You may also want to be aware of the difference between, for example, Sys.setenv(LANG = "ru") and Sys.setlocale(locale = "ru_RU.utf8").
> Sys.setlocale(locale = "ru_RU.utf8")
[1] "LC_CTYPE=ru_RU.utf8;LC_NUMERIC=C;LC_TIME=ru_RU.utf8;LC_COLLATE=ru_RU.utf8;LC_MONETARY=ru_RU.utf8;LC_MESSAGES=en_IE.utf8;LC_PAPER=en_IE.utf8;LC_NAME=en_IE.utf8;LC_ADDRESS=en_IE.utf8;LC_TELEPHONE=en_IE.utf8;LC_MEASUREMENT=en_IE.utf8;LC_IDENTIFICATION=en_IE.utf8"
If you are interested in changing the behaviour of functions that refer to one of these elements (e.g strptime to extract dates), you should use Sys.setlocale().
See ?Sys.setlocale for more details.
In order to see all available languages on a linux system, you can run
system("locale -a", intern = TRUE)
To permanently make it works, in both R and Rstudio (with Win 10),
one way to do this is to run the script every time automatically in the background initially.
No more changing the system language that influence the windows.
No more R only but fail in Rstudio. No more run a script every time manually. No more admin right but fail. No more short-cut setting but fail.
Step 1.
Use your system search, to find the file named "Rprofile"
My response is
C:\Program Files\R\R-4.0.5\library\base\R
C:\Program Files\R\R-4.0.5\etc
Step 2.
Edit C:\Program Files\R\R-4.0.5\library\base\R\Rprofile
The content:
This is the system Rprofile file. It is always run on startup.
Additional commands can be placed in site or user Rprofile files
(see ?Rprofile)
... and so on.
Step 3. Add Sys.setenv(LANGUAGE="en") at the end of the scrip
local({
Sys.setenv(LANGUAGE="en")
})
P.S. If you encounter the issue of authorization/saving,
move this file to desktop and replace the original file after editing.
type this first:
system("defaults write org.R-project.R force.LANG en_US.UTF-8")
then you will get a index number(in my case is 127)
then type:
Sys.setenv(LANG = "en")
then type the number and ENTER
127
For me worked:
Sys.setlocale("LC_MESSAGES", "en_US.utf8")
Testing:
> Sys.setlocale("LC_MESSAGES", "en_US.utf8")
[1] "en_US.utf8"
> x[3]
Error: object 'x' not found
Also working to get english messages:
Sys.setlocale("LC_MESSAGES", "C")
To reset to german messages I used
Sys.setlocale("LC_MESSAGES", "de_DE.utf8")
Here is the start of my sessionInfo:
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
A simple solution would be setting export Lang=C in your bash script.
I had a similar issue where the default language was german so it reverted back to english.
If you want to change R's language in terminal to English forever, this works fine for me in macOS:
Open terminal.app, and say:
touch .bash_profile
Then say:
open -a TextEdit.app .bash_profile
These two commands will help you open ".bash_profile" file in TextEdit.
Add this to ".bash_profile" file:
export LANG=en_US.UTF-8
Then save the file, reopen terminal and type R, you will find it's language has changed to english.
If you want language come back to it's original, just simply add a # before export LANG=en_US.UTF-8.
The only thing that worked for me was uninstalling R entirely (make sure to remove it from the Programs files as well), and install it, but unselect Message Translations during the installation process. When I installed R, and subsequently RCmdr, it finally came up in English.
In Ubuntu 14.04 LTS I had to remove the # from the comment #LANGUAGE=EN.
All other options din not work for me.
Change your current regional format to a different regional format in region settings on time&language settings in Windows by clicking on your time/date in lower right corner > adjust time/date > Region > change regional format to UK or US
This worked for me with a windows 10 laptop in German, where I wanted i.e. lubridate to return dates in English:
Sys.setlocale("LC_TIME", "English")
Im using R Studio on a Mac and I couldn't find the Rconsole file. So I took a more brutal way and just deleted the unwanted language files from the R app.
You just have to go to your Rapp in your application Folder, right click, show package content then /contents/Resources/. There are the language files e.g. English.lproj or in my case de.lproj wich I deleted. After restarting R, error messages appear in English..
Maybe thats helpful!
you simply have to change the basic language of microsoft on your computer!
press the windows button together with r, and tip the following code into the window that is opened
control.exe /name Microsoft.Language
load the language package you want to use and change the options. but take care, this will change also your keyboard layout!
on windows, when you have no admin right, just create a new program shortcut to Rgui.exe. Then in the properties of that shortcut, go to the 'Shortcut' tab and modify the target to include the system language of your choice, e.g. "C:\Program Files\R\R-3.5.3\bin\x64\Rgui.exe" LANGUAGE=en

Why does R source() sometimes work and sometimes gives an error

I have the API credentials in one separate R script to keep it out of Git. I want to run this script in the beginning of the scripts that actually interact with the different servers. I have successfully used the same strategy for all my global functions.
setwd("G:/script")
source("API_credentials.R") # gives always an error
# > source("API_credentials.R")
# Error: '\s' is an unrecognized escape in character string starting ""g:\s"
source("ProVeg_functions.R") # runs fine
Problem:
Why does the first source() not work, while the second one does? The error message does not make any sense to me.
Solutions tried:
I have tried different escape chars \.
I have tried writing full path & file names.
I have tried putting the file name in as a variable, which gets its
content from a dir() search, to make sure that the file exists and
the name is correctly written.
Order of source() does not change situation.
Isolating the piece of code with error, and restarting R.
upgraded all my packages and R to version 4.0.2.
The API_credentials.r script works fine when run on its own. the Sys.setenv() works fine and I can read the API keys with Sys.getenv().
I am not sure if it is related to my problem, but if I do usethis::edit_r_environ() I can not see my API keys.
Setup
Windows 10, R-Studio 1.3.1093, R version 4.0.2 (2020-06-22)
I mistakenly assumed that the error message was related to the script calling the API_credentials.R, but it actually was an error message indicating an error in the API_credentials.R script. Fixed a typo and all is good.

tryCatch error: the action can´t be completed because the folder is open in RStudio R session

When I download files (Windows 7) using
tryCatch(download.file(paste0(url_bv,arq), paste0(dir_bv,arq))
,error=function(cond) message(paste('erro:',arq,'não encontrado'))
,warning=function(cond) message(paste('warning:',arq,'não encontrado')))
I can´t delete files, getting the msg in the question title.
If I just do
download.file(paste0(url_bv,arq), paste0(dir_bv,arq))
there is no problem.
How can I release the folder/file in R?
I think this is likely a bug in R (windows version). I can faithfully reproduce it in R-3.2.5 (win10_64). I suggest you file a bug report (read R FAQ 9.2 for clear direction).
I tried it with several variations:
different url schemes ("http://" and "https://")
different download.file(..., method=...) options ("wininet", "internal", and "auto" all behaved in this manner; "libcurl" did not, but it merely downloaded the 404.html without telling me there was a problem)
triggers whenever warning=... or condition=... clauses are given to tryCatch and something weird happens in expr
does not trigger with error=... or finally=... arguments
32bit and 64bit variants
not in RStudio
as R --vanilla
All attempted on ubuntu-14.04 (R-3.2.3-64bit) as well, it would not trigger.

Resources