Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

Needs["RLink`"]
SetEnvironment[
"DYLD_LIBRARY_PATH" -> 
"/Library/Frameworks/R.Framework/Resources/lib"];

InstallR["RHomeLocation" -> 
"/Library/Frameworks/R.Framework/Resources"];

REvaluate["R.version.string"]

REvaluate["library(GA)"]

REvaluate["f <- function(x)  abs(x)+cos(x)
           fitness <- function(x) -f(x)
           ga(type='real-valued',fitness=fitness,min=-20,max=20)
           summary(GA)"]

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)
ga(type='real-valued',fitness=fitness,min=-5,max=5)
summary(GA). The following R error was encountered: Error in typeof(myRandomVar123456740025) : 
object 'myRandomVar123456740025' not found >>
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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:

    REvaluate["
     {f <- function(x)  abs(x)+cos(x);
      fitness <- function(x) -f(x);
      x <- ga(type='real-valued',fitness=fitness,min=-20,max=20);
      summary(x)}"]
    

    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.

    Workaround:

    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.

    REvaluate["x@population"]
    
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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