繁体   English   中英

R xlsx package 不能用

[英]R xlsx package can not be used

我的 R 工作室版本是 4.0。 我安装了xlsx package,但是当我需要使用它时,我得到一个错误:

library(xlsx)
Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.
Error: package or namespace load failed for ‘xlsx’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: fun(libname, pkgname)
  error: JVM could not be found
In addition: Warning messages:
1: In system("/usr/libexec/java_home", intern = TRUE) :
  running command '/usr/libexec/java_home' had status 1
2: In fun(libname, pkgname) :
  Cannot find JVM library 'NA/lib/server/libjvm.dylib'
Install Java and/or check JAVA_HOME (if in doubt, do NOT set it, it will be detected)

所以根据this note,我安装了'rJava'并想首先加载这个package,但我仍然得到一个错误:

library(rJava)
Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.
Error: package or namespace load failed for ‘rJava’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: fun(libname, pkgname)
  error: JVM could not be found
In addition: Warning messages:
1: In system("/usr/libexec/java_home", intern = TRUE) :
  running command '/usr/libexec/java_home' had status 1
2: In fun(libname, pkgname) :
  Cannot find JVM library 'NA/lib/server/libjvm.dylib'
Install Java and/or check JAVA_HOME (if in doubt, do NOT set it, it will be detected)

有人能给我一些关于如何加载“xlsx”和“rJava”包的建议吗? 谢谢你。

xlsx package 依赖于rJava package,这需要有效安装 Java 运行时环境 (JRE)。 在安装rJava package 之前,必须安装 Java 运行时环境 (JRE)。 安装 JRE 的过程因操作系统而异,Oracle 使此过程更加困难,因为他们从 2019 年开始对 Java 的许可要求进行了更改

如何判断是否安装了 JRE?

如对该问题的评论中所述,可以使用命令行java -version验证安装在机器上的 Java 运行时的版本。 还必须确认 Java 运行时可从 R / RStudio 访问。 我们可以通过在 RStudio 中执行system() function 来做到这一点,也如问题评论中所述。

> system("java -version")
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
[1] 0
> 

将 Excel 文件读入 R

有两种不同的方法来解决这个问题。 这是我在 2017 年为约翰霍普金斯大学数据科学专业化获取和清理数据课程常见问题:Java 和 xlsx package撰写的关于此主题的说明摘要。

解决方案 1:使用不需要 Java 的 Excel 阅读器 Package

专业提示:解决此问题的最简单方法是使用不依赖于 Java 的R package,例如 openxlx

对于openxlsx ,这很容易。

  install.packages("openxlsx")
  library(openxlsx)
  # read the help file to identify the arguments needed to 
  # correctly read the file
  ?openxlsx
  theData <- read.xlsx(...)

相同的过程可用于readxl

  install.packages("readxl")
  library(readxl)
  # read the help file to identify the arguments needed to 
  # correctly read the file 
  ?readxl
  theData <- read_excel(...)

请注意,还有一个互补的 package writexl ,它使人们能够编写 Excel 文件。

解决方案 2:安装 Java 和所需的 R 包

That said, for people who want to use the xlsx package to work with Excel files, there are workable solutions for Windows, Mac OSX, and Ubuntu Linux.

解决方案 (Windows):从 Oracle 下载并安装最新版本的Java 运行时环境 请注意,如果您运行的是 64 位版本的 R,则需要安装 64 位版本的 Java Runtime。

解决方案 (Mac OSX):随着 Mac OSX 的更新版本,这变得更加复杂。 在计算机上安装Java 开发套件后,需要遵循一组特定的命令。 这些记录在rJava 问题 86 github 页面上 我已经包含了这个解决方案的截图供人们直接参考。

在此处输入图像描述

SOLUTION (Ubuntu): Use the Ubuntu Advanced Packaging Tool to install Java, then reconfigure Java in R.

  sudo apt-get install openjdk-8-jdk # openjdk-9-jdk has some installation issues
  sudo R CMD javareconf

然后在 R / RStudio 中安装xlsx package。

  install.packages("xlsx")

Windows 中的 32 位与 64 位 Java

人们可能遇到的另一个常见问题是安装在他们计算机上的 Java 运行时环境版本与 32 位或 64 位 R 版本之间的不兼容。

For example, if one has installed the 64-bit version of R but has the 32-bit version of Java Runtime Environment installed, R will not have visibility to the Java Runtime Environment, generating the same "Java not installed error" as noted above .

SOLUTION: This problem can be resolved by either installing the 64-bit version of Java Runtime for Windows , or by changing the RStudio configuration to use the 32-bit version of R.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM