How to load rJava into RStudio? - r

Although I'm running the risk of duplicating questions, I have yet to find a solution to my configuration.
I'm trying to load rJava into RStudio.
I am using the latest Mac OS x, with a build of Rstudio that's only a couple of months old, and I've just installed JDK x64 12.0.1
The version of rJava is 0.9-11
On executing:
library(rJava)
I am confronted with the usual:
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
I've tried setting my Java home path to:
Sys.setenv(JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home')
But that doesn't make any difference.
I've clearly missed the rJava instructions somewhere which lays out how to install the package on all computer setups. I'd appreciate some assistance.
EDIT #1
Following the link provided in the comments below, I managed to get the latest version of Java JDK running through rJava in RStudio. The document "R, Java, rJava and macOS adventures" isn't complete and doesn't work off the cuff. You'll probably end up hacking about to get it working.
I needed rJava to run venneuler, unfortunately, this was not work using the latest Oracle JDK version 12:
WARNING: Initial Java 12 release has broken JNI support and does NOT work. Use stable Java 11 (or watch for 12u if available).
ERROR: Java exception occurred during rJava bootstrap - see stderr for Java stack trace.
I'm going to try (wish me luck) to get two versions of Java on my machine and have RStudio recognise the right one.
Edit 2
Pure luck I think I have Java 11 working:

I had the this issue as well on my mac (OSX High Sierra v10.13.6) and could not solve it by following previous suggestions (reinstall JDK, R CMD javareconf or setting JAVA_HOME). The same error message persisted:
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
After looking closely at the error message, I found that I had JDK12, but R kept looking for libjvm.dylib under the JDK11 folder which does not exist in my system. The path to JDK11 appeared in Sys.get('DYLD_FALLBACK_LIBRARY_PATH'), but overwriting this variable did not help. Neither did dyn.load('PATH_TO_YOUR_JDK').
And then the second to last line of the error message caught my eye: the path to libjvm.dylib was referenced from rJava.so. Why not take a look at this file? At the end, I opened the rJava.so, found the JDK11 path and replaced it with JDK12 path, and rJava loaded perfectly. Hope this helps other people with the same issue.
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tabulizer_0.2.2 rJava_0.9-11
loaded via a namespace (and not attached):
[1] tabulizerjars_1.0.1 compiler_3.5.2 assertthat_0.2.1 cli_1.1.0 tools_3.5.2 withr_2.1.2
[7] rstudioapi_0.10 yaml_2.2.0 crayon_1.3.4 png_0.1-7 sessioninfo_1.1.1

I had the same issue and I was able to fix it by searching, but I don't remember exactly the step that I've done but I looked again and I found this answer and I hope it'll work for you. You'll have to run this command in a terminal. I don't know if I can post link but I just typed "rjava lib Rstudio error" and I find a couple of different solutions so you might give them a look if this doesn't work.
sudo R CMD javareconf

Related

install_github(): "system error 267, The directory name is invalid"

I am trying to install package performance development version with devtools but am getting this error:
> devtools::install_github("easystats/performance")
Downloading GitHub repo easystats/performance#HEAD
Error: Failed to install 'performance' from GitHub:
create process 'C:/PROGRA~1/R/R-40~1.3/bin/x64/Rcmd.exe' (system error 267, The directory name is invalid.
) #win/processx.c:1040 (processx_exec)
Same thing with alternative remotes command:
> remotes::install_github("easystats/performance")
Downloading GitHub repo easystats/performance#HEAD
Error: Failed to install 'performance' from GitHub:
create process 'C:/PROGRA~1/R/R-40~1.3/bin/x64/Rcmd.exe' (system error 267, The directory name is invalid.
) #win/processx.c:1040 (processx_exec)
Edit: Note that I am getting the same error no matter what package I've tried to install through devtools (e.g., cardiomoon/processR or r-lib/crayon). This started to happen suddenly a few weeks ago yet never had this issue before. So it seems not related to the package but to something else.
So I tried changing my default library to the simplest ever location that doesn't have any special characters or require any admin rights, with those instructions. I can confirm that C:/Rpackages is now indeed my default library path, and that it is first (on the left):
> .libPaths()
[1] "C:/Rpackages" "C:/Program Files/R/R-4.0.3/library"
However, I am still getting the same error. The weird thing is that the error still seems to refer to the second library path rather than the first one, which seems off to me. From the help documentation, I don't see how to specify the library location explicitly for neither devtools nor remotes. Also interesting, notice that the error seems to mention R-40~1.3 instead of R-4.0.3 as it should(?). Might this be the issue? Then how to fix?
I also tried reinstalling devtools and remotes, to no avail.
Yet, it works if I install the regular CRAN version:
> install.packages("performance")
Installing package into ‘C:/Rpackages’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/performance_0.7.2.zip'
Content type 'application/zip' length 2487172 bytes (2.4 MB)
downloaded 2.4 MB
package ‘performance’ successfully unpacked and MD5 sums checked
Here my session info if useful:
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C
[5] LC_TIME=English_Canada.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.0.3 tools_4.0.3 yaml_2.2.1
The issue was with the processx package and the development version has since received a fix (in version v3.5.3). Please see the following discussion for more info: https://github.com/r-lib/processx/issues/313

Error: package or namespace load failed for ‘gmm’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object

I have uploaded the version of R to 4.0.2 (2020-06-22).
After installing the gmm package when I require it I got the message below. I have also taken this message for the other packages like TropFishR. I have Xcode 10.3 . I reinstalled R studio and R several times. thank you very much for your time.
Error message:
Error: package or namespace load failed for ‘gmm’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/gmm/libs/gmm.so':
dlopen(/Library/Frameworks/R.framework/Versions/4.0/Resources/library/gmm/libs/gmm.so, 6): Library not loaded: /usr/local/gfortran/lib/libgomp.1.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/gmm/libs/gmm.so
Reason: image not found
session info:
version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils
[5] datasets methods base
other attached packages:
[1] sandwich_2.5-1
loaded via a namespace (and not attached):
[1] zoo_1.8-8 compiler_4.0.2
[3] tools_4.0.2 grid_4.0.2
[5] lattice_0.20-41
The problem is caused because Library not loaded: /usr/local/gfortran/lib/libgomp.1.dylib is missed.
To solve it, I went to this Github release page, downloaded gfortran-10.2-Catalina.dmg, and installed it. Then I can finally load gmm package in R.
On MacOS 13, download this file, and build a hard link to /usr/local/gfortran/lib/libgomp.1.dylib in /opt/R/arm64/gfortran/lib, e.g., using the following command:
sudo ln libgomp.1.dylib /opt/R/arm64/gfortran/lib/libgomp.1.dylib
maybe the directory /gfortran/lib is required to be built manually. Hope helpful~
Using this answer I found a (temporary) way to fix this issue without messing around with installations.
Finding the file
It seems that the program can't find libgomp.1.dylib, however, your system may already have this file installed. Using:
locate libgomp.1.dylib
you get a list of places where this library has been found. If you are running this command for the first time, you may have to run another command first to create the index.
For me, this was:
/usr/local/Cellar/gcc/12.1.0/lib/gcc/current/libgomp.1.dylib
However, it might be different on your system.
If nothing is returned, you may need to install gfortran on your system, e.g. using brew install gcc.
Linking the file
My error message showed me some places where it tried to look for the file, one of which was in my user directory:
/Users/overground/lib
So, using ln, create a link from this directory to the file you have found in the previous step. First, ensure that the lib folder exists in your user directory. Then, link the files with:
ln /usr/local/Cellar/gcc/12.1.0/lib/gcc/current/libgomp.1.dylib /Users/overground/lib
Replace the first file path with the file path you have found in the first step, and the username in the second path. Now, you should be able to see the file in the lib directory.
This would sound crazy, but I had the same problem running a bash script (say masterPipeline.sh) that I wrote. When I wrote that script it was working fine, however a couple of days ago I got this error:
Error: package or namespace load failed for ‘stats’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/home/hemiptero/lib/R/library/stats/libs/stats.so':
The issue came after masterPipeline.sh invoke R to make a subroutine (R < ./scripts/measure2sd.r --no-save) but when I type that command by myself in the terminal I don't get the error.
So, I don't know how but I get with the idea of running my script as superuser.
sudo masterPipeline.sh
It works!
So maybe running R as superuser would works for those working on linux

rJava doesn't load in R 3.4.2 and MacOS Sierra 10.12.6

Here's the error I get:
> library(rJava)
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: #rpath/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
Reason: image not found
I see this is by no means a unique problem.. I've tried the solutions here, here and here. No success.
Some more details: I'm using R GUI, but I also can't load rJava through R terminal. I also didn't have any issue with rJava on R 3.3.s
You have two options here:
get older release of R and use precompiled version of rJava
get rJava sources and compile it for yourself
Note that rJava (most recent sources) require some features that are not embedded inside XCode and clang that is available via AppStore. You will need to do some low level stuff when it comes to building rJava package.
You can find detailed instruction here: http://www.owsiak.org/r-3-4-rjava-macos-and-even-more-mess/
If you decide to use older release of R, it's still a struggle to get it working, but way less to do: http://www.owsiak.org/r-java-rjava-and-macos-adventures/
JAVA_HOME path is different for different version of MAC OS.
In some cases there is /jre folder and in some cases it is not after /Home director manually go towards /server folder with CD command in terminal and using pwd copy the entire path use that to set the java.home then you can easily load rJava library. This is how I was able to fix the issue
options("java.home"="/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/lib")
Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')
dyn.load('/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/lib/server/libjvm.dylib')
library(rJava)

R TCLTK not loading on windows

I have installed R on countless windows machines and never had a problem with the tcltk package until now.
I have freshly installed R 3.3.2 onto Windows 10 Pro v1607 and can't load tcltk. I used the windows install binaries from CRAN, so no custom builds.
capabilities("tcltk")
returns TRUE. When I try to load tcltk:
library("tcltk")
I get:
Error : .onLoad failed in loadNamespace() for 'tcltk', details:
call: inDL(x, as.logical(local), as.logical(now), ...)
error: unable to load shared object 'C:/Program Files/R/R-3.3.2/library/tcltk/libs/x64/tcltk.dll':
LoadLibrary failure: The specified module could not be found.
Error: package or namespace load failed for ‘tcltk’
Navigating to the location of the DLL confirms that it is there. This fails when calling both x64 and i386 versions of R. I have tried running as admin and same thing. I also tried rolling back to version 3.3.1 and had the same issues.
This PC has an AMD FX-8120 processor.
I couldn't find much on R tcltk problems on Windows. Any ideas would be great.
Cheers
EDIT: added info:
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.3.2
To fix the problem do the following tests:
Test the Tcl/Tk installation of R:
Find and start wish85.exe which is contained in one of the sub folders of the R installation ("85" is the version number and my differ depending on your R version).
An empty Tk window must pop up and a tcl/tk console must open.
If not the installation is damaged or parts have been deleted
(reinstall it)
Check if R is using the right Tcl/Tk version:
Check the value of the environment variable MY_TCLTK
If it is defined: Does the path point to the bin folder of Tcl/Tk?
If not: Fix it.
If it is not defined: It should work out of the box but you could
set the value to the bin folder.
Source:
https://mirrors.dotsrc.org/cran/bin/windows/base/rw-FAQ.html#Package-TclTk-does-not-work_002e
MY_TCLTK was defined as "Yes" in my environment variables. I changed it to the location of the tcl exe that was installed with R and it works now.

tcltk error in R [duplicate]

After upgrading to OS X 10.10 Yosemite I started to receive the following error log when running my Rcmdr package in R using library(Rcmdr):
Error : .onLoad failed in loadNamespace() for 'tcltk', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcltk/libs/tcltk.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcltk/libs/tcltk.so, 10): Library not loaded: /usr/X11/lib/libXft.2.dylib
Referenced from: /usr/local/lib/libtk8.6.dylib
Reason: image not found
Error: package or namespace load failed for 'Rcmdr'
Could you help me understand how to fix it?
This mac os x troubleshooting didn't help.
Execute the following in a terminal window:
locate libXft.2.dylib
or do it from an R console:
system("locate libXft.2.dylib")
On my Yosemite install it's in /opt/X11/lib/libXft.2.dylib.
If your setup is similar, then:
sudo ln -s /opt/X11 /usr/X11
should help fix this temporarily, but I'd keep looking for a Yosemite R package (since that will accommodate all the changes Apple made to the filesystem structure).
Of note: On my work Mac (Mavericks) this symlink is already there.
Reinstalling XQuartz worked for me.
I added a symlink from /usr/X11 to /opt/X11 and this resolved the problem without the locate command.

Resources