Rstudio Knit按钮无法加载rJava

时间:2017-02-22 14:27:14

标签: r rstudio knitr r-markdown rjava

Rstudio的编织按钮无法编写包含rJava包的rmarkdown(Rmd)文件。但是,如果我使用rmarkdown::render(),相同的Rmd文件会呈现为html而不会出现问题。

我可以使用以下代码重现错误:

test.Rmd:

的内容
---
title: "test"
output: html_document
---

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

单击编织按钮按钮将返回:

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

rmarkdown::render("test.Rmd")工作正常。此外,正常运行library(rJava)(非编织)也可以正常运行(在RStudio内)。

在macOS Sierra 10.12中使用RStudio版本1.0.136,knitr 1.15.1,rmarkdown 1.3,rJava 0.9-8,R 3.3.2。

更新:根据@ r2evans的要求添加Sys.getenv()的输出。 DYLD_FALLBACK_LIBRARY_PATH的值有所不同,但我不知道这是否重要。我用XXX替换了一些私人信息。

在Knit按钮产生的html文件中:

## __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

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 以下来自@ r2evans的评论

render()中包含来自.Rmd的DYLD_FALLBACK_LIBRARY_PATH按预期更改Sys.getenv()的结果,但是使用编织按钮的错误行为(与上面相同的错误消息)并且没有错误render()仍然存在。新的.Rmd内容为:

---
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)
```

有趣的是,在上面render()中使用test2.Rmd,包括Sys.getenv()现在输出两个新变量,使用编织按钮之前不存在且不存在:

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

我不知道是否有意义,但我尝试了一个新的.Rmd(下方),仍然会出现同样的错误

---
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 个答案:

答案 0 :(得分:8)

在命令行上运行以下内容为我解决了这个问题:

sudo R CMD javareconf

答案 1 :(得分:1)

我和你有同样的问题。在Rstudio R控制台中加载rJava没有问题,但在编织HTML文件时遇到加载失败。和你一样,我也可以使用render()命令在“knit”上加载rJava。我没有做过你所进行的大量测试,但是我最终可以通过跟踪neilfws提交给你的链接在“knit”中成功加载rJava -

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

然后我按照元(https://stackoverflow.com/users/5697305/yuan)的建议,使用以下命令直接在R中加载库:

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

在RMarkdown文件中插入此行不会影响正常的控制台行为,但允许使用“编织”按钮进行编织。从来没有想出原因,但解决方案有效。

答案 2 :(得分:0)

你知道你正在使用哪种Java吗?我不太了解Java以了解我的经验是否有帮助,但是,当我尝试重现您的错误时,我被指向了这个网站:

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

我下载并安装了适用于OS X 10.11 El Capitan,OS X 10.10 Yosemite,OS X 10.9 Mavericks,OS X 10.8 Mountain Lion和OS X 10.7 Lion的旧版Java 6运行时。这样做是否可以解决您的错误?

我正在使用Sierra 10.12.3,RStudio 1.0.136,rJava_0.9-8在MacBook Pro上工作。