tl;dr
Is it possible to change the LANGUAGE environment setting (or the LC_MESSAGES component of the locale) platform-independently, and if not (which is probably the case) to detect whether the value requested was in fact legal/took effect?
Context
One of my functions tried to detect the occurrence of a particular error by grep()ing the text of the error message. As previously pointed on an R mailing list (can't find the reference right now), this approach is fragile: the text of the message can change based on the setting of the LANGUAGE environment variable.
So I'm changing my code to not do that, but I'd like to add a regression test to the package that tests whether the problem occurs. And I'd like it to work (or at least not fail) across platforms.
As described e.g. in this question, I can use Sys.setenv(LANGUAGE=...) or Sys.setlocale("LC_MESSAGES",.) to change the message language. But this will only work if I guess the right LANGUAGE/LC_MESSAGES value, and it's available on the current system. (It seems that LANGUAGE is a language code, and LC_MESSAGES is a locale, and that the locale need not be installed if I just want to switch languages?)
Sys.getenv("LANGUAGE") ## [1] "en_CA:en"
test_msg <- function(msg,silent=FALSE) {
grepl(msg,try(x ,silent=silent))
}
test_msg("not found") ## TRUE ("object 'x' not found")
Sys.setenv(LANGUAGE="fr")
test_msg("introuvable")
## Erreur : objet 'x' introuvable
So far, so good. But resetting the LANGUAGE variable doesn't seem to work ...
Sys.setenv(LANGUAGE="en")
Sys.getenv("LANGUAGE")=="en" ## TRUE
test_msg("not found") ## FALSE ('introuvable')
Sys.setenv(LANGUAGE="en_CA:en")
test_msg("not found") ## FALSE ('introuvable')
Setting LC_MESSAGES to C seems more reliable ...
Sys.setlocale("LC_MESSAGES", "C")
test_msg("not found")
FWIW I've found that this sequence of commands actually seems to be unreliable, i.e. the same attempt to set the environment variable seems to work sometimes depending on what pissing around I've done previously ... I have to go back and keep starting with a clean session to get reproducible results ...
In any case, I know that if I were to try this on a computer without a French locale available, or where the locale/language name was different, that it wouldn't work. I'd like to be able to figure out whether the language setting has worked. I can use test_msg("introuvable") as brute force, but I'm hoping there's a more elegant option ...
relevant bits of sessionInfo():
R Under development (unstable) (2018-11-26 r75681)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS
locale:
[1] LC_CTYPE=en_CA.UTF8 LC_NUMERIC=C
[3] LC_TIME=en_CA.UTF8 LC_COLLATE=en_CA.UTF8
[5] LC_MONETARY=en_CA.UTF8 LC_MESSAGES=en_CA.UTF8
Related
I'm struggling with creating some unit tests in R where I need to mock out functions like RCurl::getURL so they return canned output and don't required a network connection.
Here's an example:
library(mockthat)
test_that("API works", {
mock_response <- mockthat::mock('[{"foo":7}]')
mockthat::with_mock(
getURL=mock_response,
{
result <- myFunction()
})
...
This fails with the following error, but only when I run under devtools::check() or R CMD check (it succeeds under devtools::test()):
not ok 1 API works
Error: cannot change value of locked binding for 'getURL'
Backtrace:
1. mockthat::with_mock(...) test-yes.R:29:2
2. base::lapply(mocks, set_mock)
3. mockthat:::FUN(X[[i]], ...)
4. mockthat:::do_assign(mock$name, mock$new_value, mock$env)
5. base::assign(name, val, envir = env)
Is there something I can change to get this working, or a workaround I can use?
I'm using:
> packageVersion('mockthat')
[1] '0.2.6'
> cat(head(capture.output(sessionInfo()), 3), sep="\n")
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.4
but the problem seems to also exist under testthat's mocking stuff (which is now deprecated so I don't want to use it anyway) and mockery too.
Apologies for not creating a proper reprex - I'm not sure how to demonstrate the problem without having a whole package structure.
[adapted from an issue reported at https://github.com/nbenn/mockthat/issues/6]
I have a lot of unit tests using the testthat package that expect english error messages.
If other developer run the tests on a computer configured for a non-english locale the error message are emitted in a different language and my tests fail.
How can I initialize testthat to change the language settings only during the test run-time without manually or permanently changing the language or test environment from outside of R (like e. g. proposed here: in R how to get error messages in english)?
library(testthat)
# works only in english locales...
expect_error(log("a"), "non-numeric argument to mathematical function", fixed = TRUE)
Edit 1: Changing the locale during run-time does not change the language of the error messages (using Ubuntu and OSX High Sierra):
Sys.setlocale( locale = "en_US.UTF-8")
Sys.getlocale() # en_US is active now but messages are still in another language
Edit 2: It seems that Sys.setenv("LANGUAGE"="EN") seems to change the error message language immediately (tested using OSX). Where should I put this command for testthat? In the testthat.R file?
The R console is in German language, how can I set R to English?
Edit 3: As a first work-around I have put
Sys.setenv("LANGUAGE"="EN") # work-around to always create english R (error) messages
into my testthat.R file under the tests folder (it seems to work but I am not sure whether this is the right or best way...
Setting Sys.setenv("LANGUAGE" = "EN") works for me as well.
However, when testing with devtools::test() - as ctrl + shift + T in Rstudio will do - I had to call Sys.setenv() in the test scripts inside the tests/testthat/ directory. The reason being that devtools::test() will call testthat::test_dir() circumventing the tests/testthat.R file.
So far, this did not have undesirable side-effects. The environment variable will only be set for that particular R process as described in the help page:
Sys.setenv sets environment variables (for other processes called from within R or future calls to Sys.getenv from this R process).
For completeness, you can also unset the variable again on Windows (see comments).
Sys.setenv("LANGUAGE" = "DE")
expect_error(log("a"), "Nicht-numerisches Argument")
Sys.setenv("LANGUAGE" = "FR")
expect_error(log("a"), "argument non numérique ")
Sys.unsetenv("LANGUAGE")
RStudio might also give trouble (I was not able to change the language there interactively), but when executing with devtools::test() it works.
Finally, wrapping it in a helper function.
expect_error_lang <- function(..., lang = "EN") {
Sys.setenv("LANGUAGE" = lang)
expect_error(...)
Sys.unsetenv("LANGUAGE")
}
#...
expect_error_lang(log("a"), "non-numeric")
expect_error_lang(log("a"), "Nicht-numerisches", lang = "DE")
expect_error_lang(log("a"), "argument non", lang = "FR")
First of all, sorry for not providing a reproducible example and posting images, a word of explanation why I did it is at the end.
I'd really appreciate some help - comments or otherwise, I think I did my best to be as specific and concise as I can
Problem I'm trying to solve is how to set up (and where to do it) encoding in order to get polish letters after a .Rmd document is knitted to html.
I'm working with a labelled spss file imported to R via haven library and using sjPlot tools to make tables and graphs.
I already spent almost all day trying to sort this out, but I feel I'm stucked with no idea where to go.
My sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Polish_Poland.1250 LC_CTYPE=Polish_Poland.1250 LC_MONETARY=Polish_Poland.1250
[4] LC_NUMERIC=C LC_TIME=Polish_Poland.1250
Whenever I run (via console / script)
sjt.frq(df$sex, encoding = "Windows-1250")
I get a nice table with proper encoding in the rstudio viewer pane:
Trying with no encoding sjt.frq(df$sex) gives this:
I could live with setting encoding each time a call to sjt.frq is made, but problem is, that no matter how I set up sjt.frq inside a markdown document, it always gets knited the wrong way.
Running chunk inside .Rmd is OK (for a completely unknown reason encoding = "UTF-8 worked as well here and it didn't previously):
Knitting same document, not OK:
(note, that html header has all the polish characters)
Also, it looks like that it could be either html or sjPlot specific because knitr can print polish letters when they are in a vector and are passed as if they where printed to console:
Is there anything I can set up / change in order to make this work?
While testing different options I discovered, that manually converting sex variable to factor and assigning labels again, works and Rstudio knits to html with proper encoding
df$sex <- factor(df$sex, label = c("kobieta", "mężczyzna"))
sjt.frq(df$sex, encoding = "Windows-1250")
Regarding no reproducible example:
I tried to simulate this example with fake data:
# Get libraries
library(sjPlot)
library(sjlabelled)
x <- rep(1:4, 4)
x<- set_labels(x, labels = c("ąę", "ćŁ", "óŚŚ", "abcd"))
# Run freq table similar to df$sex above
sjt.frq(x)
sjt.frq(x, encoding = "UTF-8")
sjt.frq(x, encoding = "Windows-1250")
Thing is, each sjt.frq call knits the way it should (although only encoding = "Windows-1250" renders properly in rstudio viewer pane.
If you run sjt.frq(), a complete HTML-page is returned, which is displayed in a viewer.
However, for use inside markdown/knitr-documents, there are only parts of the HTML-output required: You don't need the <head> part, for instance, as the knitr-document creates an own header for the HTML-page. Thus, there's an own print()-method for knitr-documents, which use another return-value to include into the knitr-file.
Compare:
dummy <- sjt.frq(df$sex, encoding = "Windows-1250")
dummy$output.complete # used for default display in viewer
dummy$knitr # used in knitr-documents
Since the encoding is located in the <meta>-tag, which is not included in the $knitr-value, the encoding-argument in sjt.frq() has no effect on knitr-documents.
I think that this might help you: rmarkdown::render_site(encoding = 'UTF-8'). Maybe there are also other options to encode text, or you need to modify the final HTML-file, changing the charset encoding there.
Seems that runjags suddenly (after update to version 2.0.3-2) has trouble finding JAGS binary, issuing an error:
[1] "Error in system(\"where jags\", intern = TRUE) : 'where' not found\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in system("where jags", intern = TRUE): 'where' not found
I fixed this by putting this line to my Rprofile:
.runjags.options <- list(jagspath = "c:/Program Files/JAGS/JAGS-4.2.0/i386/bin/jags-terminal.exe")
This pretty much fixes the problem (although it is not ideal - previous versions of runjags could find the binary automatically).
However, when the Rgui (in Windows XP) is launched by opening an .Rdata file, which is associated to it, it stops working:
> .runjags.options # it was set in the Rprofile
$jagspath
[1] "c:/Program Files/JAGS/JAGS-4.2.0/i386/bin/jags-terminal.exe"
> require(runjags)
Loading required package: runjags
Warning message:
package ‘runjags’ was built under R version 3.1.3
> runjags.getOption("jagspath")
[1] "Error in system(\"where jags\", intern = TRUE) : 'where' not found\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in system("where jags", intern = TRUE): 'where' not found
Is this a bug? How to fix this?
I am currently calling runjags.options(jagspath = "c:/Program Files/JAGS/JAGS-4.2.0/i386/bin/jags-terminal.exe") in my source after require(runjags), but I would like to avoid this as much as possible!
The problem is in the findjags() function, more specifically use of the 'where' system command which was added to Windows in 2003 and therefore doesn't exist in Windows XP. There is some code in findjags() that is designed to detect earlier versions of Windows and revert to a different method of finding JAGS, but this hasn't changed since version 1.x so I don't know why this has 'suddenly' stopped working. Perhaps there has been a change in your system, or a change in the way the system() command works in R. I have modified this code to work around where I think the error is coming from, but I don't have access to Windows XP (I'm rather surprised that you do!) so I can't test it. I don't think it will do any harm though so I will include it in the next release anyway, after testing on more recent versions of Windows.
I also don't know exactly why the .runjags.options is not being found when you open a .Rdata file, but it is possible that opening a .Rdata file loads the library before setting objects. I will look into this but it is likely not something I can do anything about. The alternative to using runjags.options() is to set the jags argument in calls to run.jags etc.
I am looking for a way to test if R is being run from RStudio. For some reason I could find the answer on google yesterday but not today, but I think it had to do with testing if a certain system variable was set.
This is from ?rstudio:
# Test whether running under RStudio
isRStudio <- Sys.getenv("RSTUDIO") == "1"
There is also rstudioapi::isAvailable(), but checking this is not as reliable because RStudio doesn't seem to really need the rstudioapi package to work correctly.
Check the .Platform$GUI option for "RStudio"
is.rstudio = function(){
.Platform$GUI == "RStudio"
}
See:
http://thecoatlessprofessor.com/programming/detecting-if-r-is-in-rstudio-and-changing-rstudios-default-graphing-device/
There is no "running inside RStudio". RStudio is merely an IDE layer that wraps around R; at the end of the day it just launches the normal R executable you need to have on your $PATH anyway to operate RStudio.
As a proxy, and as R Studio You could test available.packages() for the 'manipulate' package though, or as a shorter version see if RStudio added itself to the .libPaths() content:
R> any(grepl("RStudio", .libPaths()))
[1] TRUE
R>
R>
Edit in May 2020 or eight years later The question does come up, and one can query a variety of things from within. Here is an example from the terminal of RStudio:
$ env | grep -i rstudio | sort
GIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/rstudio.desktop
PATH=[...redacted...]
RMARKDOWN_MATHJAX_PATH=/usr/lib/rstudio/resources/mathjax-27
RS_RPOSTBACK_PATH=/usr/lib/rstudio/bin/rpostback
RSTUDIO=1
RSTUDIO_CONSOLE_COLOR=256
RSTUDIO_CONSOLE_WIDTH=111
RSTUDIO_PANDOC=/usr/lib/rstudio/bin/pandoc
RSTUDIO_PROGRAM_MODE=desktop
RSTUDIO_PROJ_NAME=chshli
RSTUDIO_SESSION_ID=9C62D3D4
RSTUDIO_SESSION_PORT=13494
RSTUDIO_TERM=2BD6BB88
RSTUDIO_USER_IDENTITY=edd
RSTUDIO_WINUTILS=bin/winutils
$
Similarly, from within the R session:
R> se <- Sys.getenv()
R> se[grepl("rstudio",se,ignore.case=TRUE)]
GIO_LAUNCHED_DESKTOP_FILE /usr/share/applications/rstudio.desktop
PATH [...also redacted...]
RMARKDOWN_MATHJAX_PATH /usr/lib/rstudio/resources/mathjax-27
RS_RPOSTBACK_PATH /usr/lib/rstudio/bin/rpostback
RSTUDIO_PANDOC /usr/lib/rstudio/bin/pandoc
R>
Edit in Aug 2021 or nine years later As all the answers listed here in the different answer may still be too much for people, you can also install package rstudioapi from CRAN and then ask it via rstudioapi::isAvailable() which comes back TRUE for me inside RStudio and FALSE in ESS / standard R.
When I start RStudio it seems to have tools:rstudio in position 2 on the search path. This has a function "RStudio.version" which is undocumented but seems to return the RStudio version string:
> RStudio.version()
[1] "0.96.316"
So you can define:
is.RStudio <- function(){
if(!exists("RStudio.version"))return(FALSE)
if(!is.function(RStudio.version))return(FALSE)
return(TRUE)
}
and maybe use that.
As of today, there are a few packages which include functions to check whether RStudio is running:
rstudioapi::isAvailable()
assertive::is_rstudio()
(list is non-exhaustive)
The assertive and assertive.reflections packages, resp., do include additional functions to check for other IDEs, desktop/server versions of RStudio, and various R releases (e.g., alpha, beta, devel, release, patched, etc.)
To add to the number of nice guesses, here is a message from 2011 (Ice Age)
http://support.rstudio.org/help/discussions/problems/413-location-of-installed-packages
if (Sys.getenv("RSTUDIO_USER_IDENTITY")!= ""){
.libPaths(.Library) # Avoid additional libraries } else { # not rstudio ...
I find the following works for me
checkRstudio <- function () {
return ("tools:rstudio" %in% search())
}
I am sort of new to R myself, but I believe Rstudio necessarily loads the
package "tools:rstudio" in order to run.
Neat solution is now available through the startup package via the is_rstudio_console function:
startup:::is_rstudio_console()
[1] TRUE
It may be worth adding that this function checks for two environment variables, RSTUDIO, which was mentioned in the answer by #krlmr and RSTUDIO_TERM that doesn't seem to be mentioned across the preceding answers at the moment.
function ()
{
(Sys.getenv("RSTUDIO") == "1") && !nzchar(Sys.getenv("RSTUDIO_TERM"))
}
The most reliable way to detect whether code is running in the main RStudio R session (without relying on the rstudioapi package) is:
commandArgs()[[1L]] == "RStudio"
Some comments on other answers:
Checking environment variables may be insufficient, as the "RSTUDIO" environment variable will also be inherited by child R processes launched from the main R session.
RStudio does not initialize its R infrastructure until after R profile scripts are run (e.g. ~/.Rprofile), so checking things like .Platform$GUI == "RStudio" or "tools:rstudio" %in% search() or rstudioapi::isAvailable() won't give what you expect in that context.
On a Mac only the Sys.getenv answer works
platform x86_64-apple-darwin10.8.0
version.string R version 3.1.0 (2014-04-10)
Sys.getenv("RSTUDIO")=="1"
[1] TRUE
RStudio.version()
Error: could not find function "RStudio.version"
any(grepl("RStudio", .libPaths()))
[1] FALSE
.libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library"