Group Abstract Group Abstract

Message Boards Message Boards

Interface with R in Mathematica 12 and R 3.6.1?

Posted 6 years ago
POSTED BY: Claude Mante
37 Replies
POSTED BY: Leonid Shifrin
POSTED BY: Leonid Shifrin
POSTED BY: Anton Antonov

I got it working on Win10 after a lot of trial and error and with your good advice.

In R Console:

> .libPaths()
[1] "D:/Users/Chris/Documents/R/win-library/4.0"
[2] "C:/Program Files/R/R-4.0.3/library" 

In Wolfram:

Exit
JLink`UninstallJava[]
Needs["RLink`"]
InstallR[
"RHomeLocation" -> "C:/Program Files/R/R-4.0.3/",
"RVersion" -> "4.0.3",
"NativeLibLocation" -> 
"D:/Users/Chris/Documents/R/win-library/4.0/rJava/jri/x64"]
POSTED BY: Chris Van Damme

I have a code snippet to reproduce the issue. For now you may use Chop as a workaround.

chop

POSTED BY: Shenghui Yang

Just in case someone may find it useful, I was still having problems installing R version 3.6.2 in Mathematica 12.1 until I executed:

JLink`UninstallJava[]

before the InstallR command. That did the trick for me.

Thank very much Shenghui, I see now where the mistake was. I was grouping the parenthesis in the wrong way leading to an objective function that was returning a vector instead of scalar (how careless of me :D). spg requires an objective function that is scalar. Thank you very much for running the example for me.

POSTED BY: Hamood Khan
POSTED BY: Leonid Shifrin

First configuration step is impossible with current JDK, in my case 15.0.1, because there is no jre !

POSTED BY: Murray Eisenberg

Sorry for such a late response. I am aware of the problem, and will try to get it fixed soon.

POSTED BY: Leonid Shifrin
POSTED BY: Dmitry Shemetov

Yeah, you need curly braces as closure to evaluate the compound expression in R, just like what is documented in REvaluate.

POSTED BY: Shenghui Yang

You know what, I worked around this by wrapping everything I do into a function like so

enter image description here

Cheers,

-Dmitry

POSTED BY: Dmitry Shemetov

Hi Brian,

I recently got this error when my JDK crashed while working with R, so I would suggest making sure Java is functioning properly.

Best,

-Dmitry

POSTED BY: Dmitry Shemetov

Hi all,

This seems like a place to post RLink issues, so even though my question is not an RLink installation issue, I hope it is appropriate here.

I managed to get RLink to work with an external R distribution on Windows and I installed the packages I want in R. I'm now trying to call them in Mathematica and my peculiar issue is that I can't seem to define a variable and use it in the same code cell. See below for an example.

enter image description here

A similar issue is occurring with loading libraries, I would appreciate any help on the issue! -Dmitry

POSTED BY: Dmitry Shemetov

I have a very simple problem that I'd like to utilize R for from a Mathematica (12.1) interface. I'd like to avoid using an external R distribution (the old version of R should be fine for my purposes). However,

<< "RLink`"
RLinkResourcesInstall[]
InstallR[]

gives me the error:

InstallR::fail: Failed to install R. The following error was encountered: crash in low-level RLink component or in R runtime

All of the troubleshooting I've seen online considers how to fix the problem with an external R distribution. What might cause this to fail with only the defaults?

POSTED BY: Brian Lerch

Thanks a lot.

POSTED BY: Hamood Khan

I have notified our developer to take a look at the inconsistence between WL expression and R object about the double-complexed mixed matrix.

issue

Meanwhile you may try a work around from R side based on this link

wmat =RSet["w",{{1,2},{3I,4}}];
RSet["x",Range[2]];
REvaluate["w <- matrix(unlist(w),nrow=2,ncol=2,byrow=TRUE)"];
REvaluate["w %*% x"]
POSTED BY: Shenghui Yang

Dear Shenghui, here is another problem. I think this is malfunctioning with RLink:

mat = Chop@FourierMatrix[10, WorkingPrecision -> MachinePrecision];
RSet["mat", mat];
X = Transpose[{Range[10]}];
RSet["X", Chop@X];
REvaluate["mat%*%X"];

Produces the following error:

REvaluate::rerr: Failed to retrieve the value for variable or piece of code mat%*%X. The following R error was encountered: Error in mat %*% X : requires numeric/complex matrix/vector arguments.

Checking the type of mat gives:

In[61]:= REvaluate["class(mat)"]

Out[61]= {"list"}

While:

In[54]:= REvaluate["class(X)"]

Out[54]= {"matrix"}

Why is that? I think this is the root of the matter.

Regards, Hamood

POSTED BY: Hamood Khan

Thanks for looking into this.

Regards, Hamood

POSTED BY: Hamood Khan

Hi Shenghui and Leonid,

I posted this question in another post but no one seems to be responding, so I am reposting it here. I think it's an important question regarding R and Mathematica.

I am having trouble transferring complex matrices from Mathematica to R. In particular, the Fourier matrix, mat1, in the following code is presenting a problem.

w0 = N@2 Pi/101
mat1 = Table[(Exp[I w0]^Range[3])^i, {i, 0, 2}]
mat2 = Table[(Exp[I w0]^Range[3]), {3}]

Now the following line presents no problem

In[479]:= RSet["mat2", mat2]

Out[479]= {{0.998066 + 0.0621696 I, 0.99227 + 0.124099 I, 
  0.982635 + 0.185548 I}, {0.998066 + 0.0621696 I, 
  0.99227 + 0.124099 I, 
  0.982635 + 0.185548 I}, {0.998066 + 0.0621696 I, 
  0.99227 + 0.124099 I, 0.982635 + 0.185548 I}}

However, for some reason, mat1 fails to be RSet.

In[486]:= RSet["mat1", mat1]

During evaluation of In[486]:= JLink`JavaNew::argx: Incorrect number or type of arguments to constructor for class com.wolfram.links.rlink.dataTypes.inTypes.RComplexVectorInType. The arguments, shown here in a list, were {{1.,0.998066 +0.0621696 I,0.99227 +0.124099 I,1.,<<19>> +<<20>> I,0.969199 +0.246279 I,1.,0.982635 +0.185548 I,0.931144 +0.364652 I},{},«<<1>>»}.

During evaluation of In[486]:= RSet::puterr: Error putting the expression {{1. +0. I,1. +0. I,1. +0. I},{0.998066 +0.0621696 I,<<19>> +<<20>> I,0.982635 +<<20>> I},{0.99227 +0.124099 I,0.969199 +0.246279 I,0.931144 +0.364652 I}} into a variable mat1 in R. The last error message issued by R was Error: 'list' object cannot be coerced to type 'complex'

Out[486]= $Failed

There seem to be some type conversion issues between lists and complex data types. Why is this issue cropping up?

Regards, Hamood

POSTED BY: Hamood Khan

According to the doc https://www.rdocumentation.org/packages/BB/versions/2019.10-1/topics/spg you can extract the components one by one:

REvaluate["sc2.f <- function(x){ sum((1:length(x)) * (exp(x) - x)) / 10}"];
REvaluate["p0 <- rnorm(50)"];

REvaluate["{
 ans.spg1 <- spg(par=p0, fn=sc2.f)
 ans.spg1$par
 }"]

comp

POSTED BY: Shenghui Yang

Hi Leonid,

I have a similar problem here, although it is not about RLink or installing a package. I have been able to successfully do both of these. However, there is an optimization package called BB which has the function spg (Spectral Projection Gradient) for large scale nonlinear optimization. When I call the following code in R it runs perfectly fine. Following is the R Code.Here sc2.f is the objective function for optimization and p0 gives a vector of the initial guess for the optimization variables.

require("BB") 
sc2.f <- function(x){ sum((1:length(x)) * (exp(x) - x)) / 10}
p0 <- rnorm(50)
ans.spg1 <- spg(par=p0, fn=sc2.f)

when this code is run it gives prints the following. f-value is the value of the objective function and pgrad is the projected gradient.

iter:  0  f-value:  323.6774  pgrad:  52.38542 
iter:  10  f-value:  127.5661  pgrad:  0.1005832 
iter:  20  f-value:  127.5039  pgrad:  0.02815128 
iter:  30  f-value:  127.5001  pgrad:  0.01830088 
iter:  40  f-value:  127.5  pgrad:  0.0004423839 
iter:  50  f-value:  127.5  pgrad:  8.910206e-05 
iter:  60  f-value:  127.5  pgrad:  1.847411e-05 

The point is, it does its job with ans.spg1 variable giving the values of the optimized variables

 ans.spg1
 $par
 [1]  1.843400e-04  4.004529e-07 -1.403083e-08 -5.314239e-08
 [5]  3.031558e-08 -4.387552e-08  3.866317e-08  4.334743e-08
 [9] -1.020669e-07  1.170379e-07  3.592466e-08  2.992959e-08
[13]  8.591248e-08 -2.758630e-07 -4.951071e-08 -4.020634e-08
[17]  8.010105e-08  6.033852e-08 -4.763555e-08  4.880627e-08
[21]  1.070940e-06  1.178099e-07  1.077314e-06  3.870496e-07
[25]  6.317214e-08 -3.753983e-08  3.771117e-07  3.925886e-07
[29]  3.955844e-07 -5.079168e-08  1.070812e-07  3.868255e-07
[33]  7.694439e-07  1.264478e-07  1.185176e-07 -4.721000e-08
[37]  7.853781e-07  7.736863e-07 -3.392351e-07  1.357027e-07
[41]  7.629153e-07  7.618032e-07 -4.939840e-08 -4.701460e-08
[45]  1.367395e-07  9.510446e-07  1.037346e-07 -5.175414e-07
[49] -4.894020e-08 -5.040282e-07

$value
[1] 127.5

$gradient
[1] 5.627498e-05

$fn.reduction
[1] 196.1774

$iter
[1] 62

$feval
[1] 63

$convergence
[1] 0

$message
[1] "Successful convergence"

Now the problem is the RLink is unable to handle this form of output, so when I run the following:

REvaluate["sc2.f <-function(x){sum(1:length(x))*(exp(x)-x)/10}"];
REvaluate["p0 <- rnorm(50)"];
REvaluate["library(\"BB\")"];
REvaluate["func <- spg(par = p0, fn = sc2.f, quiet=TRUE)"]

I get the following error

REvaluate::rerr: Failed to retrieve the value for variable or piece of code func <- spg(par = p0, fn = sc2.f, quiet=TRUE). The following R error was encountered: Error in spg(par = p0, fn = sc2.f, quiet = TRUE) : 
  function must return a scalar numeric value!

What can I do? Please advise. Since this is a thread for R in Mathematica I thought it would be best to post this question here so that it might catch your attention. I am attaching my notebook.

Attachments:
POSTED BY: Hamood Khan

I am working to address multiple RLink issues right now, most likely including the one that causes the failure on some Macs. Have not yet tested on Catalina, but we will certainly do that. With some luck, we may be able to have these fixes in 12.1.1, or may be even earlier as a paclet update.

POSTED BY: Leonid Shifrin

Some obstacles (Mathematica 12.1 on macOS Catalina):

  • With current versions OpenJDK, there is no jre!
  • I cannot get rJava to install; ends up with a compile error.
POSTED BY: Murray Eisenberg

Hello Vijaya,

Apparently, you simply didn't escape the string argument properly. This worked for me (on Mac OS X, R 3.6.2):

REvaluate[ 
   "morris(model=NULL,factors=4,r=2,design=list(type=\"oat\",levels=5, 
    grid.jump=3),binf=0,bsup=1,scale=TRUE)"
]

(note the "oat" argument).

POSTED BY: Leonid Shifrin

Hello, I followed all the steps as indicated in the discussion above to use package ODEsensitivity in R using the Mathematica interface. I installed the package and ran library(packagename) It appears the package is found but I am not able to run it. The commands used are given in the attached notebook. Any help would be appreciated. I did not start a new discussion as my problem is related to above discussion. Thanks Vijaya

Attachments:

Good to know, Claude. Was happy to help.

Leonid

POSTED BY: Leonid Shifrin

Thanks Leonid, it works now!

Claude

POSTED BY: Claude Mante

Hi Leonid!

That's strange. FactoMine works correctly under the R console: enter image description here

Nevertheless,

In[15]:= REvaluate["R.Version()"]

Out[15]= RObject[{{"x86_64-w64-mingw32"}, {"x86_64"}, {"mingw32"}, \
{"x86_64, mingw32"}, {""}, {"3"}, {"6.1"}, {"2019"}, {"07"}, {"05"}, \
{"76782"}, {"R"}, {"R version 3.6.1 (2019-07-05)"}, {"Action of the \
Toes"}}, RAttributes[
  "names" :> {"platform", "arch", "os", "system", "status", "major", 
    "minor", "year", "month", "day", "svn rev", "language", 
    "version.string", "nickname"}]]

In[26]:= REvaluate["library(\"FactoMineR\")"]

During evaluation of In[26]:= REvaluate::rerr: Failed to retrieve the value for variable or piece of code library("FactoMineR"). The following R error was encountered: Error in library("FactoMineR") : 
  aucun package nommé 'FactoMineR' n'est trouvé

Out[26]= $Failed

I'll try with other packages, later...

Claude

POSTED BY: Claude Mante

Hi Claude,

Make sure you have correctly installed that R library together with all its dependencies (of which there are a lot). To be specific, the following packages were also installed in my case:

{
 "backports", "digest", "glue", "zeallot", "ellipsis", "magrittr",  "vctrs", "R6",
 "clipr", "BH", "rematch", "prettyunits", "assertthat", "utf8", "forcats", "hms",
 "readr", "cellranger", "progress", "zip", "cli", "crayon", "fansi", "pillar", 
 "pkgconfig", "rlang", "SparseM", "MatrixModels", "sp", "haven", "curl", 
 "data.table", "readxl", "openxlsx", "tibble", "minqa", "nloptr", "Rcpp", "RcppEigen",
 "carData", "abind", "pbkrtest", "quantreg", "maptools", "rio", "lme4", "car",
 "ellipse", "flashClust", "leaps", "scatterplot3d", "FactoMineR"
}

Check that first using R console:

> library('FactoMineR')

I am using the same R version as you, also on Windows:

In[12]:= REvaluate["R.Version()"]

Out[12]= RObject[{{"x86_64-w64-mingw32"}, {"x86_64"}, {"mingw32"}, \
{"x86_64, mingw32"}, {""}, {"3"}, {"6.1"}, {"2019"}, {"07"}, {"05"}, \
{"76782"}, {"R"}, {"R version 3.6.1 (2019-07-05)"}, {"Action of the \
Toes"}}, RAttributes[
  "names" :> {"platform", "arch", "os", "system", "status", "major", 
    "minor", "year", "month", "day", "svn rev", "language", 
    "version.string", "nickname"}]]

I have installed FactoMineR under Windows, and for me everything works fine, at least as far as just testing the package being loaded is concerned:

In[13]:= REvaluate["library(\"FactoMineR\")"]

Out[13]= {"FactoMineR", "stats", "graphics", "grDevices", "utils",  "datasets", "methods", "base"}

Using long paths or $Path isn't correct and won't help.

I hope you will manage to get this working.

Leonid

POSTED BY: Leonid Shifrin

Hi Leonid! It's not completely finished, unfortunately... I'm now able to load R and execute basic R commands:

In[13]:= InstallR["RHomeLocation" -> "C:/Program Files/R/R-3.6.1", 
 "RVersion" -> "3.6.1", 
 "NativeLibLocation" -> 
  "C:\\Users\\Claude\\Documents\\R\\win-library\\3.6\\rJava\\jri\\x64"]
REvaluate["sample(1:100,10)"]

Out[14]= {52, 54, 40, 35, 44, 77, 47, 53, 46, 79}

but I have more ambition! I would like to perform a multivariate analysis with the excellent package "FactoMineR", which I installed today; it is located there:

C:\Users\Claude\Documents\R\win-library\3.6\FactoMineR

but I can't load it:

In[26]:= REvaluate["library(\"FactoMineR\")"]

During evaluation of In[26]:= REvaluate::rerr: Failed to retrieve the value for variable or piece of code library("FactoMineR"). The following R error was encountered: Error in library("FactoMineR") : 
  aucun package nommé 'FactoMineR' n'est trouvé

Out[26]= $Failed

I tried several variants, without any positive result:

In[27]:= (*REvaluate["library(\"C:\Users\Claude\Documents\R\win-\
library\3.6\FactoMineR\R\")"]*)
REvaluate["library(\"C:\\Users\\Claude\\Documents\\R\\win-library\\3.\
6\\FactoMineR\")"]

During evaluation of In[27]:= REvaluate::crash: Crash in low-level RLink component or in R runtime. Please reinstall RLink via InstallR

Out[27]= $Failed

What should I type to load this package? I also tried to alter $Path, with something like

(*REvaluate["install.packages(\"FactoMineR\")"]*)
CLa = "C:\\Users\\Claude\\Documents\\R\\win-library\\3.6\\";
AppendTo[$Path, CLa]

but is it useful?

Regards, Claude

POSTED BY: Claude Mante

You are welcome, Claude. I am happy to help.

Leonid

POSTED BY: Leonid Shifrin

Thanks Lenonid, now it works!

Claude

POSTED BY: Claude Mante

Hi Leonid!

I didn't understood: I thought "NativeLibLocation" was specific to Mac! But

In[13]:= InstallR["RHomeLocation" -> "C:/Program Files/R/R-3.6.1", 
 "RVersion" -> "3.6.1", 
 "NativeLibLocation" -> 
  "C:/Users/Claude/Documents/R/win-library/3.6/rJava/jri"]

During evaluation of In[13]:= InstallR::fail: Failed to install R. The following error was encountered: Unable to load dynamic libraries

Out[13]= $Failed

is wrong, too... Nevertheless, "jri.dll" is here:

C:\Users\Claude\Documents\R\win-library\3.6\rJava\jri

I could touch it (but I prefer clean screens)!

Regards,

Claude

POSTED BY: Claude Mante

Hi Claude,

I think you missed the option "NativeLibLocation" - it is that one that has to point out to that folder with the library, while "RHomeLocation" should point at the root of your R distribution, as before. Have a look at the example of InstallR[] call I provided, and adopt the right paths for your system - hopefully it should work.

HTH,

Leonid

POSTED BY: Leonid Shifrin

Hi Leonid, and thanks a lot, first! Java was not installed on my recent machine.... I just installed everything; here is the (default) location of rJava/jri:

C:\Users\Claude\Documents\R\win-library\3.6\rJava\jri

Unfortunately, it doesn't work, yet:

In[2]:= InstallR[
 "RHomeLocation" -> 
  "C:/Users/Claude/Documents/R/win-library/3.6/rJava/jri", 
 "RVersion" -> "3.6.1"]

During evaluation of In[2]:= InstallR::fail: Failed to install R. The following error was encountered: Unable to load dynamic libraries

Out[2]= $Failed

Any suggestion? Best regards,

Claude

POSTED BY: Claude Mante
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard