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)
```
Sys.getenv()
both inrender
andknit
ting it. Look for differences, particularly inPATH
and any of theLIB
-related variables. – r2evanslibjvm.dylib
? I might start looking in the additionalDYLD_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). – r2evanslibjvm.dylib
file in theDYLD_FALLBACK_LIBRARY_PATH
from Knit button (/Library/Frameworks/R.framework/Resources/lib
), but there is in one of the paths from therender()
(/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 addingSys.setenv(DYLD_FALLBACK_LIBRARY_PATH="/usr/local/lib")
before loadingrJava
, but the error when knitting remain – JG11235