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)
```
Running the following on the command line resolved this issue for me:
sudo R CMD javareconf
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With