# Interface with R in Mathematica 12 and R 3.6.1?

Posted 2 years ago
18315 Views
|
37 Replies
|
24 Total Likes
|
 Hello everybody!I wrote a number of notebooks calling R packages. But using up-to-date versions (Mathematica 12 and R 3.6.1), nothing works any more! For instance, it seems the default distribution doesn't exist now: In[2]:= InstallR[] REvaluate["sample(1:100,10)"] During evaluation of In[2]:= InstallR::invldrhome: The specified path to R home directory does not point to a valid directory Out[2]= $Failed I tried to use my own distribution, but it is not recognized! In[4]:= InstallR["RHomeLocation" -> "C:/PROGRA~1/R/R-36~1.1", "RVersion" -> "3.6.1"] During evaluation of In[5]:= InstallR::fail: Failed to install R. The following error was encountered: crash in low-level RLink component or in R runtime Out[5]=$Failed It seems there is an issue with the R.dll file (dll: 3.6.1, expecting 3.5.0). What can I do? Install an older version of R? Which one?Best regards, Claude
37 Replies
Sort By:
Posted 2 years ago
 This indeed looks like the problem of incompatible libraries. You will have to go through a number of steps to fix this. Make sure you have JDKFirst, make sure you have some relatively recent JDK (Java development kit) installed. Java 8 and above should do. This can be downloaded from Oracle web site and installed. Start your R distribution interactive console (command line R interface). For your current R session, set the environmental variable JAVA_HOME to the location of your JDK / JRE. In my case, the corresponding command looked like Sys.setenv(JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre").  Install the rJava R package: install.packages('rJava') You will be prompted by R to pick a mirror, and then the package should get installed. You will need to locate where R installed the rJava / jri package. Normally, it will be installed into the folder given by (execute this in R prompt): .libPaths() In my case, it was located at /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/jri where the part before /rJava/jri was given by .libPaths() You should find the jri.dll library inside the jri sub-folder in rJava package. If there is an x86-64 sub-folder there, you should get the one from there (assuming your architecture is 64 bit). You can either use that file directly or copy it to some more convenient location. Now you are ready to call InstallR. On a fresh Mathematica kernel, the call will look similarly to this (except this is for Mac OS X, but the same should also work for Windows): InstallR["RHomeLocation" -> "/Library/Frameworks/R.framework/Versions/3.6/Resources", "RVersion" -> "3.6.1" , "NativeLibLocation" -> "/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/jri" ] where an undocumented option "NativeLibLocation" was used to indicate an alternative location of the JRI library, and the value should be the path to the folder containing jri.dll (which you have located previously). The steps 1 through 6 are only necessary to be performed once. After that, you will always need to call InstallR with this extra option, to use more recent versions of R than those for which the library is available out of the box. Of course, future versions of Mathematica will eventually catch up with more recent R versions.Hope this helps.
Posted 1 year ago
 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 1 year ago
 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 1 year ago
 I was able to follow Leonid Shifrin's directions for Mathematica 12.2 and R 4.0. (I verified that R packages installed by me are accessible through RLink.) See the attached screenshot.
Posted 2 years ago
 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 Answer Posted 2 years ago  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 Answer Posted 2 years ago  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 2 years ago
 Claude,The "NativeLibLocation" option works for all platforms, not just Mac. Your latest issue was due to picking the wrong (32 bit) version of the JRI library. You need to use the library from /x64 sub-folder (I did mention that). I just tested on Windows, the following works (make sure to execute this on a fresh kernel for a clean test): InstallR["RHomeLocation" -> "C:/Program Files/R/R-3.6.1", "RVersion" -> "3.6.1", "NativeLibLocation" -> "C:\\Program Files\\R\\R-3.6.1\\library\\rJava\\jri\\x64"] pay attention to the \x64 part of the path.Here is a simple test that we get it working: In[7]:= REvaluate["R.Version()"] Out[7]= 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"}]] HTH,Leonid
Posted 1 year ago
 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:JLinkUninstallJava[]before the InstallR command. That did the trick for me.
Posted 1 year ago
 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 JLinkUninstallJava[] 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 2 years ago
 Thanks Lenonid, now it works!Claude
Posted 2 years ago
 You are welcome, Claude. I am happy to help.Leonid
Posted 2 years ago
 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 2 years ago
 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 Answer Posted 2 years ago  Hi Leonid!That's strange. FactoMine works correctly under the R console: 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 2 years ago
 Hi Claude,Looking at your library path, I suspect it isn't a standard one for R on Windows, so it might be that it is not added automatially when R is started via RLink. I would try to evaluate REvaluate[".libPaths()"],, which for me returns: In[5]:= REvaluate[".libPaths()"] Out[5]= {"C:/Program Files/R/R-3.6.1/library"} Try evaluating this on your system and see if your library path where FactoMineR is installed, is among the returned paths. If it is not, then you might need to add it manually, something like this might work: REvaluate[".libPaths( c( .libPaths(), \"C:/Users/Claude/Documents/R/win-library/3.6\") )"] This has to be run first, before you try to load the library. If my guess is right, then the call to library() should work after this.Leonid
Posted 2 years ago
 Thanks Leonid, it works now!Claude
Posted 2 years ago
 Good to know, Claude. Was happy to help.Leonid
Posted 1 year ago
 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:
Posted 1 year ago
 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 1 year ago
 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 1 year ago
 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 }"]  Answer Posted 1 year ago  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. Answer Posted 1 year ago  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]:= JLinkJavaNew::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 1 year ago
 I have a code snippet to reproduce the issue. For now you may use Chop as a workaround.
Posted 1 year ago
 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 1 year ago
 I have notified our developer to take a look at the inconsistence between WL expression and R object about the double-complexed mixed matrix. 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 1 year ago
 Thanks a lot.
Posted 1 year ago
 Sorry for such a late response. I am aware of the problem, and will try to get it fixed soon.
Posted 1 year ago
 Thanks for looking into this.Regards, Hamood
Posted 1 year ago
 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 1 year ago
 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 1 year ago
 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. A similar issue is occurring with loading libraries, I would appreciate any help on the issue! -Dmitry
Posted 1 year ago
 You know what, I worked around this by wrapping everything I do into a function like soCheers,-Dmitry
Posted 1 year ago
 Yeah, you need curly braces as closure to evaluate the compound expression in R, just like what is documented in REvaluate`.