Is there any updates regarding installing CRAN packages via RLink in Mathematica 10 on Mac OS X? Or are we still stuck with the base R package and that's it?

I was able to point to my R distribution using this answer but using external packages led to errors.

Here is my code:


InstallR["RHomeLocation" -> 



REvaluate["f <- function(x)  abs(x)+cos(x)
           fitness <- function(x) -f(x)

Out[15]= $Failed

and the error message:

REvaluate::rerr: Failed to retrieve the value for variable or piece of code f<- function(x)  abs(x)+cos(x)
fitness <- function(x) -f(x)
summary(GA). The following R error was encountered: Error in typeof(myRandomVar123456740025) : 
object 'myRandomVar123456740025' not found >>

You can use an external R, as described in the answer you link to, and you can install packages into this external R installation. The errors you see are not related to this.

Here are the problems with the code:

  1. REvaluate needs to be used with a single R expression. It's not possible to pass it multiple expressions. Enclose everything in { ... } to make it into a single expression.

    REvaluate["x <- 1; x+x"]  (* <-- INVALID *)
    REvaluate["{ x <- 1; x+x }"]  (* <-- good *)
  2. You were trying to evaluate summary(GA). There's no variable named GA.

  3. REvaluate returns the result of the evaluation as a Mathematica expression. It does not return the output that R would print. While using summary(something) as the last line of your input is not incorrect, it's probably not what you were looking for here. Please see here. You can use REvaluate["capture.output(summary(x))"] to see what R would print.

  4. This works:

     {f <- function(x)  abs(x)+cos(x);
      fitness <- function(x) -f(x);
      x <- ga(type='real-valued',fitness=fitness,min=-20,max=20);

    You might want to add caputre.output as in point 3. though.

  5. RLink has trouble transferring objects of class "ga". This is why

    REvaluate["x <- ga(type='real-valued',fitness=fitness,min=-20,max=20)"] 

    will give an error. The situation is not dissimilar to this.


    Use a semicolon to prevent RLink from trying to translate this object to a Mathematica expression. You probably wouldn't be able to use it in any other way than passing it back to an R function, anyway. So let's just store the result in x but no return it to Mathematica:

    REvaluate["x <- ga(type='real-valued',fitness=fitness,min=-20,max=20);"]

    What one would use in Mathematica is data extracted from the "ga" object, e.g.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.