12
votes

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)
```
3
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
It might be informative to run Sys.getenv() both in render and knitting it. Look for differences, particularly in PATH and any of the LIB-related variables.r2evans
@r2evans Updated question with the information requested.JG11235
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
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 remainJG11235

3 Answers

8
votes

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

sudo R CMD javareconf
1
votes

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.

0
votes

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.