11

The knit button of Rstudio is failing to knit rmarkdown (Rmd) files which includes the rJava package. However, if I use rmarkdown::render(), the same Rmd files render into html without problems.

I can reproduce the error with the following code:

content of test.Rmd:

---
title: "test"
output: html_document
---

```{r}
library(rJava)
```

Clicking the Knit button button would return:

processing file: test.Rmd

Error : .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.3/Resources/library/rJava/libs/rJava.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava/libs/rJava.so
  Reason: image not found
Quitting from lines 7-8 (test.Rmd) 
Error: package or namespace load failed for 'rJava'
Execution halted

And rmarkdown::render("test.Rmd") works fine. Also, running library(rJava) normally (not knitting) works fine too (within RStudio).

Using RStudio version 1.0.136, knitr 1.15.1, rmarkdown 1.3, rJava 0.9-8, R 3.3.2 in macOS Sierra 10.12.

Update: Adding output of Sys.getenv() as requested by @r2evans. There is a difference in the value of DYLD_FALLBACK_LIBRARY_PATH, but I don't know if this is significant. I replace some private info with XXX.

In the html file resulting from Knit button:

## __CF_USER_TEXT_ENCODING
##                       0x1F5:0x0:0x52
## Apple_PubSub_Socket_Render
##                       /private/tmp/com.apple.launchd.HvKScioyGU/Render
## DISPLAY               :0
## DYLD_FALLBACK_LIBRARY_PATH
##                       /Library/Frameworks/R.framework/Resources/lib
## EDITOR                vi
## GIT_ASKPASS           rpostback-askpass
## HOME                  XXX
## LANG                  en_CA.UTF-8
## LC_CTYPE              en_CA.UTF-8
## LN_S                  ln -s
## LOGNAME               XXX
## MAKE                  make
## NOT_CRAN              true
## PAGER                 /usr/bin/less
## PATH                  /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin
## PWD                   XXX
## R_ARCH                
## R_BROWSER             /usr/bin/open
## R_BZIPCMD             /usr/bin/bzip2
## R_DOC_DIR             /Library/Frameworks/R.framework/Resources/doc
## R_GZIPCMD             /usr/bin/gzip
## R_HOME                /Library/Frameworks/R.framework/Resources
## R_INCLUDE_DIR         /Library/Frameworks/R.framework/Resources/include
## R_LIBS                /Library/Frameworks/R.framework/Versions/3.3/Resources/library
## R_LIBS_SITE           
## R_LIBS_USER           ~/Library/R/3.3/library
## R_PAPERSIZE           a4
## R_PAPERSIZE_USER      a4
## R_PDFVIEWER           /usr/bin/open
## R_PLATFORM            x86_64-apple-darwin13.4.0
## R_PRINTCMD            lpr
## R_QPDF                /Library/Frameworks/R.framework/Resources/bin/qpdf
## R_RD4PDF              times,inconsolata,hyper
## R_SESSION_TMPDIR      /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T//RtmpiYlwpU
## R_SHARE_DIR           /Library/Frameworks/R.framework/Resources/share
## R_SYSTEM_ABI          osx,gcc,gxx,gfortran,?
## R_TEXI2DVICMD         /usr/local/bin/texi2dvi
## R_UNZIPCMD            /usr/bin/unzip
## R_ZIPCMD              /usr/bin/zip
## RMARKDOWN_MATHJAX_PATH
##                       /Applications/RStudio.app/Contents/Resources/resources/mathjax-26
## RMARKDOWN_PREVIEW_DIR
##                       /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T//RtmpLe3D9z
## RS_RPOSTBACK_PATH     /Applications/RStudio.app/Contents/MacOS/rpostback
## RS_SHARED_SECRET      XXX
## RSTUDIO               1
## RSTUDIO_PANDOC        /Applications/RStudio.app/Contents/MacOS/pandoc
## RSTUDIO_SESSION_PORT
##                       XXX
## RSTUDIO_USER_IDENTITY
##                       XXX
## RSTUDIO_WINUTILS      bin/winutils
## SED                   /usr/bin/sed
## SHELL                 /bin/bash
## SHLVL                 0
## SSH_AUTH_SOCK         XXX
## TAR                   /usr/bin/tar
## TMPDIR                /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T/
## USER                  XXX
## XPC_FLAGS             0x0
## XPC_SERVICE_NAME      0

In the file resulting from rmarkdown::render():

## __CF_USER_TEXT_ENCODING
##                       0x1F5:0x0:0x52
## Apple_PubSub_Socket_Render
##                       /private/tmp/com.apple.launchd.HvKScioyGU/Render
## DISPLAY               :0
## DYLD_FALLBACK_LIBRARY_PATH
##                       /Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Users/Jayme/lib:/usr/local/lib:/usr/lib::::::::
## EDITOR                vi
## GIT_ASKPASS           rpostback-askpass
## HOME                  XXX
## LANG                  en_CA.UTF-8
## LC_CTYPE              en_CA.UTF-8
## LN_S                  ln -s
## LOGNAME               XXX
## MAKE                  make
## PAGER                 /usr/bin/less
## PATH                  /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin
## R_BROWSER             /usr/bin/open
## R_BZIPCMD             /usr/bin/bzip2
## R_DOC_DIR             /Library/Frameworks/R.framework/Resources/doc
## R_GZIPCMD             /usr/bin/gzip
## R_HOME                /Library/Frameworks/R.framework/Resources
## R_INCLUDE_DIR         /Library/Frameworks/R.framework/Resources/include
## R_LIBS_SITE           
## R_LIBS_USER           ~/Library/R/3.3/library
## R_PAPERSIZE           a4
## R_PDFVIEWER           /usr/bin/open
## R_PLATFORM            x86_64-apple-darwin13.4.0
## R_PRINTCMD            lpr
## R_QPDF                /Library/Frameworks/R.framework/Resources/bin/qpdf
## R_RD4PDF              times,inconsolata,hyper
## R_SESSION_TMPDIR      /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T//RtmpLe3D9z
## R_SHARE_DIR           /Library/Frameworks/R.framework/Resources/share
## R_SYSTEM_ABI          osx,gcc,gxx,gfortran,?
## R_TEXI2DVICMD         /usr/local/bin/texi2dvi
## R_UNZIPCMD            /usr/bin/unzip
## R_ZIPCMD              /usr/bin/zip
## RMARKDOWN_MATHJAX_PATH
##                       /Applications/RStudio.app/Contents/Resources/resources/mathjax-26
## RS_RPOSTBACK_PATH     /Applications/RStudio.app/Contents/MacOS/rpostback
## RS_SHARED_SECRET      XXX
## RSTUDIO               1
## RSTUDIO_PANDOC        /Applications/RStudio.app/Contents/MacOS/pandoc
## RSTUDIO_SESSION_PORT
##                       XXX
## RSTUDIO_USER_IDENTITY
##                       XXX
## RSTUDIO_WINUTILS      bin/winutils
## SED                   /usr/bin/sed
## SHELL                 /bin/bash
## SSH_AUTH_SOCK         XXX
## TAR                   /usr/bin/tar
## TMPDIR                /var/folders/15/vdvn4fvx0zn0gc93_5krp6h00000gn/T/
## USER                  XXX
## XPC_FLAGS             0x0
## XPC_SERVICE_NAME      0

Update2 Following comments from @r2evans

Including the DYLD_FALLBACK_LIBRARY_PATH from render() in the .Rmd changes the result of Sys.getenv() as expected, but the behaviour of error with knit button (same error message as above) and no error in render() persists. The new .Rmd content is:

---
title: "test2"
output: html_document
---

```{r}
Sys.setenv(DYLD_FALLBACK_LIBRARY_PATH="/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Users/Jayme/lib:/usr/local/lib:/usr/lib::::::::")
library(rJava)
```

Interestingly, using render() in this test2.Rmd just above, and including Sys.getenv() now outputs two new variables, not present before and not present using the knit button:

## DYLD_LIBRARY_PATH     /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/server
## LD_LIBRARY_PATH       :@JAVA_LD@

I don't if it makes sense or not, but I tried a new .Rmd (below), which still gives the same error

---
title: "test3"
output: html_document
---

```{r}
Sys.setenv(DYLD_FALLBACK_LIBRARY_PATH="/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/lib:/Users/Jayme/lib:/usr/local/lib:/usr/lib::::::::")
Sys.setenv(DYLD_LIBRARY_PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/server")
Sys.setenv(LD_LIBRARY_PATH= ":@JAVA_LD@")
library(rJava)
```
JG11235
  • 563
  • 3
  • 11
  • I can't reproduce your error ... works all fine on my system, Ubuntu 16.04 LTS. The rest of the packages are the same as on my system. – J_F Feb 22 '17 at 14:33
  • It might be informative to run `Sys.getenv()` both in `render` and `knit`ting it. Look for differences, particularly in `PATH` and any of the `LIB`-related variables. – r2evans Mar 03 '17 at 16:53
  • @r2evans Updated question with the information requested. – JG11235 Mar 03 '17 at 20:06
  • 1
    Have you looked for the location of `libjvm.dylib`? I might start looking in the additional `DYLD_FALLBACK_LIBRARY_PATH` paths (from the second listing). If you find it there, try updating the variable with the additional path (likely in the `.Rmd` file, unfortunately). – r2evans Mar 03 '17 at 20:14
  • Thanks for the help @r2evans. There is no `libjvm.dylib` file in the `DYLD_FALLBACK_LIBRARY_PATH` from Knit button (`/Library/Frameworks/R.framework/Resources/lib`), but there is in one of the paths from the `render()` (`/usr/local/lib`). Should I copy this file (it is an Alias) into `/Library/Frameworks/R.framework/Resources/lib`? Or write something in the `.Rmd` file? Is this issue something expected with these packages, or just a misconfiguration in my machine? I tried adding `Sys.setenv(DYLD_FALLBACK_LIBRARY_PATH="/usr/local/lib")` before loading `rJava`, but the error when knitting remain – JG11235 Mar 03 '17 at 20:42
  • I suggest *against* copying the library into another directory: the moment you upgrade your system installation of java, you will get a plethora of other bugs due to mixed library versions. I would have thought assigning the full set of paths (not just the one) in the `.Rmd` would have been sufficient. Can you try with the full value of `DYLD_...`, and update your question with the sample Rmarkdown that is still triggering the same error? – r2evans Mar 03 '17 at 20:46
  • Having `libjvm.dylib` in `/usr/local/lib` seems odd, but what about `Sys.setenv(JAVA_HOME="/usr/local/lib")` instead? If you read the [source of `zzz.R.in`](https://github.com/s-u/rJava/blob/43c11ba09f6d962f1bf348e118bd11a9261dd0cb/R/zzz.R.in) from `rJava`, it has some workarounds for osx. Perhaps that will provide some insight for other things/places to search. – r2evans Mar 03 '17 at 21:26
  • I now tried `Sys.setenv(JAVA_HOME="/usr/local/lib")` but the error when knitting and not when rendering remains, with the same error message. In `/usr/local/lib` there is only an Alias for `libjvm.dylib`. The original is in `/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/server`. Tried using this `Library/Java.../ for JAVA_HOME, but the error remain – JG11235 Mar 03 '17 at 21:47
  • Try `Sys.setenv(JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre")`. If that doesn't work, try removing one dir at a time. – r2evans Mar 03 '17 at 22:19
  • Once again, thanks so much for your help. I saw an issue on github (https://github.com/rstudio/rmarkdown/issues/976), what makes me think it is not only some very crazy thing in my machine. I tried the JAVA_HOME full and removing one dir at a time, with and without also setting DYLD_FALLBACK_LIBRARY_PATH, DYLD_LIBRARY_PATH, LD_LIBRARY_PATH. Setting all this, the result of Sys.getenv is the same with the knit button or render(), but the error remains. I also tried using random characters in these values in Sys.setenv, and this has no impact. With render it still works, but knit button doesn't. – JG11235 Mar 03 '17 at 22:50
  • Perhaps related? http://stackoverflow.com/questions/30738974/rjava-load-error-in-rstudio-r-after-upgrading-to-osx-yosemite – neilfws Mar 08 '17 at 21:23

3 Answers3

8

Running the following on the command line resolved this issue for me:

sudo R CMD javareconf
Megatron
  • 15,909
  • 12
  • 89
  • 97
1

I was having the same issue as you. No problems loading rJava in the Rstudio R console, but experiencing a failure to load during knitting an HTML file. Like you, I also was able to load rJava on a "knit" using the render() command. I didn't do nearly the extensive level of testing that you did, however I eventually could successfully load rJava during a "knit" by following the link provided to you in the commments by neilfws -

http://stackoverflow.com/questions/30738974/rjava-load-error-in-rstudio-r-after-upgrading-to-osx-yosemite

Then I followed the suggestion by yuan (https://stackoverflow.com/users/5697305/yuan) to load the library directly in R using the command:

dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0[YOURVERSIONHERE].jdk/Contents/Home/jre/lib/server/libjvm.dylib')

Inserting this line in the RMarkdown file did not affect normal console behavior, but allowed for a knit using the "knit" button. Never figured out why, but the solution works.

scfurl
  • 36
  • 3
0

Do you know which Java you're using? I don't know enough about Java to know if my experience might help, but, when I tried to reproduce your error, I was pointed to this site:

https://support.apple.com/kb/DL1572?locale=en_US

where I downloaded and installed the legacy Java 6 runtime for OS X 10.11 El Capitan, OS X 10.10 Yosemite, OS X 10.9 Mavericks, OS X 10.8 Mountain Lion, and OS X 10.7 Lion. Does doing so resolve your error?

I'm working on a MacBook Pro with Sierra 10.12.3, RStudio 1.0.136, rJava_0.9-8.

Fred Boehm
  • 656
  • 4
  • 11
  • I am using Java Version 8 Update 121 (from the test in https://www.java.com/en/download/installed8.jsp). A while ago I had some issues relating to RJava and remember this legacy Java 6 as a proposed solution, but I solved them following some answers from http://stackoverflow.com/questions/30738974/rjava-load-error-in-rstudio-r-after-upgrading-to-osx-yosemite. I don't think it is a Java problem because the functions work fine outside of the knitting scope. – JG11235 Mar 03 '17 at 13:27
  • good point about it working outside of use with the RStudio knit button. – Fred Boehm Mar 04 '17 at 03:13