How to assign options("browser") at startup without being modified by RStudio? - r

Consider this in ~/.Rprofile or path/to/project/.Rprofile:
.First <- function () {
options(browser="firefox")
cat("Browser:", getOptions("browser"), "\n")
}
(Re)Starting R within RStudio prints at the console at least the following:
Browser: firefox
Unfortunately, the option browser seems to become overwritten:
getOption("browser")
function (url)
{
.Call("rs_browseURL", url)
}
<environment: 0x4688c30>
This does not happen in an R session started from e.g. bash.
System information: RStudio 0.98.953, R 3.1.1, Linux Mint x86_64
So my question is:
How to assign options("browser") at startup without being modified by RStudio?
Also I like to know which other options RStudio sets to its defaults.

Related

readline does not prompt user input from Rprofile.site in Rstudio

I have this little function in a file:
library(grDevices) # needed in Rprofile.site
readfun <- function()
{
message("interactive: ", interactive()) # tells TRUE
rl <- readline("Write something: ")
message("rl value is: ", rl)
}
readfun()
I can source it in the R console witin Rstudio just fine.
I can write source("thatfile.R") in the Rrofile.site and calling Rterm via R.exe prompts input as expected. (I'm on Windows, btw).
But starting R with Rstudio, it will not prompt for user input.
Instead, the first typed command will not be executed but messaged back.
This could be related to Wait for user input from keyboard in R before next line of code - readline - Rstudio, but I can't find a way to get it to work...

How to ensure english error messages in testthat unit tests

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")

Stop R executing System or Shell commands

i'd like to disable commands that can execute other non R related stuff like System(), Shell() e.g.
for (year in 2010:2915){
system("calc")
}
from running within R.
any suggestions other than locking down the user executing?
thanks
edit: to add more context, we allow the users to create R scripts in our solution which are passed to the R Engine to execute, we then process those results.
Short of editing the R source code to remove the undesirable functions, which would be tedious and probably a bit dangerous, I would override these functions:
# override system()
env <- as.environment("package:base")
unlockBinding("system", env) # bindings in the base R are write-protected
assign(
"system",
function(...){stop("This is a forbidden command!")},
envir=env
)
lockBinding("system", env)
This would give the following when a user runs system():
> system()
Error in system() : this is a forbidden command
So that the changes take effect each time R is started, you could override as many functions as you want this way, adding them to .First() in your (write-protected) "Rprofile.site" file:
.First <- function(){
# code to override system() here
# code to override shell() here
# ...
}
Note that this will not prevent an ill-intentioned determined user from re-implementing the forbidden functionality though.

execute functions immediately when opening R

I was wondering how I can execute some pre-defined functions when I open R or R-studio?
I know that it sounds silly, but I installed package praise and sort of want to try executing praise() automatically every time I open R or R studio, without actually typing in praise().
For this you can use .First() and .Last() in the .Rprofile.
It's a typical R file, launched on startup and used primarly to export some stuff by default.
Example .Rprofile:
# .First() run at the start of every R session.
# Use to load commonly used packages?
.First <- function() {
library(ggplot2)
cat("\nSuccessfully loaded .Rprofile at", date(), "\n")
}
# .Last() run at the end of the session
.Last <- function() {
cat("\nGoodbye at ", date(), "\n")
}
Related: Expert R users, what's in your .Rprofile?

How can I fork a new process using mcfork in RStudio?

I seem to be unable to fork a new process in RStudio using mcfork from parallel, though this works in a standard R console on linux / OS X. Is there an alternative workaround to forking a subprocess in RStudio?
For example, consider this function:
library(parallel)
f <- function() {
p <- parallel:::mcfork()
if (inherits(p, "masterProcess")) {
cat("I'm a child!")
parallel:::mcexit()
}
cat("I'm the master\n")
}
In my standard R console I get the expected output:
I'm the master
> I'm a child!
But in Rstudio, I get:
I'm the master
Answer converted from a comment by Kevin Ushey:
It's specifically a quirk with how stdout/stderr redirection is handled in child processes forked from R within RStudio, but note that this would work if e.g. you were to redirect that output to a file, e.g. cat("I'm a child!", file = "~/child.txt")
- Kevin Ushey Dec 6 '14 at 5:00 AM

Resources