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替换了一些私有(private)信息。

在由“编织”按钮产生的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()现在会输出两个新变量,这些变量以前并没有出现,也没有使用knit按钮出现:
## 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)
```

最佳答案

在命令行上运行以下命令为我解决了此问题:

sudo R CMD javareconf

关于Rstudio编织按钮无法加载rJava,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42394244/

10-11 22:56