2019年5月27日 星期一

Mac上的R語言安裝xlsx套件

要在R裡面安裝xlsx套件,首先要先安裝rJava套件;而要安裝rJava套件,又需要安裝JDK(Java Development Kit)。但要注意的是,這個xlsx套件要求的JDK不能夠是Oracle的那套,要用OpenJDK,且還要求用stable Java 11

以下是安裝的過程,總共有5個步驟:

I. 移除系統上安裝的JDK(移出他原本的位置即可,也可用rm -rf刪除)
$ sudo mv /Library/Java/JavaVirtualMachines/jdk* ~/Desktop

II. 下載OpenJDK 11 binary,安裝到/Library/Java/JavaVirtualMachines/下面
$ cd ~/Sources
$ wget https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_osx-x64_bin.tar.gz
$ shasum -a 256 openjdk-11.0.2_osx-x64_bin.tar.gz 
f365750d4be6111be8a62feda24e265d97536712bc51783162982b8ad96a70ee  openjdk-11.0.2_osx-x64_bin.tar.gz
$ cd /Library/Java/JavaVirtualMachines/
$ sudo tar zxf ~/Sources/openjdk-11.0.2_osx-x64_bin.tar.gz
 
$ javac --version
javac 11.0.2
$ java --version
openjdk 11.0.2 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

III. 讓系統上的R去設定所需的Java環境,可以看到是Java 11.0.2
$ sudo R CMD javareconf
Java interpreter : /usr/bin/java
Java version     : 11.0.2
Java home path   : /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
Java compiler    : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
clang -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/include/darwin  -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c conftest.c -o conftest.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/Cellar/r/3.5.2_2/lib/R/lib -L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server -ljvm -L/usr/local/Cellar/r/3.5.2_2/lib/R/lib -lR -lintl -Wl,-framework -Wl,CoreFoundation


JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /usr/local/Cellar/r/3.5.2_2/lib/R
Done.

IV. 在R裡面安裝rJava套件,這裡的版本是0.9-11
> install.packages("rJava")
...
warning: [options] bootstrap class path not set in conjunction with -source 6
warning: [options] source value 6 is obsolete and will be removed in a future release
warning: [options] target value 1.6 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
Note: examples/rtest2.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
4 warnings
installing via 'install.libs.R' to /usr/local/lib/R/3.5/site-library/rJava
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (rJava)

V. 在R裡面安裝xlsx套件,這邊顯示xlsx套件的版本是0.6.1
> install.packages("xlsx")
> library(xlsx)
> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin18.2.0 (64-bit)
Running under: macOS Mojave 10.14.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /usr/local/Cellar/openblas/0.3.5/lib/libopenblasp-r0.3.5.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] xlsx_0.6.1

loaded via a namespace (and not attached):
[1] compiler_3.5.2 tools_3.5.2    xlsxjars_0.6.1 rJava_0.9-11  

參考文獻

_EOF_

沒有留言:

張貼留言