不能使用R xlsx软件包

问题描述 投票:0回答:1

我的R Studio版本是4.0。我安装了xlsx软件包,但是当我需要使用它时,出现错误:

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)

因此,根据此注释,我安装了'rJava',并希望首先加载此软件包,但仍然出现错误:

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'软件包的建议吗?谢谢。

r xlsx rjava
1个回答
2
投票

xlsx程序包取决于rJava程序包,它需要有效安装Java运行时环境(JRE)。在安装rJava软件包之前,必须先安装Java Runtime Environment(JRE)。安装JRE的过程因操作系统而异,由于Oracle从2018年开始对Java的许可要求进行了更改,因此Oracle使此过程变得更加困难。

这里是我在约翰霍普金斯大学数据科学专业课程获取和清理数据

课程中编写的说明的摘要,早在2017年Common Problems: Java and xlsx package

解决方案1:使用不需要Java的Excel Reader程序包

PRO TIP:解决此问题的最简单方法是使用不依赖Java的R包,例如openxlsxreadxl

对于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(...)

请注意,有一个补充包writexl,使人们可以编写Excel文件。

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

也就是说,对于那些想使用xlsx包处理Excel文件的人,有一些适用于Windows,Mac OSX和Ubuntu Linux的解决方案。

SOLUTION(Windows):

从Oracle下载并安装最新版本的Java Runtime Environment。请注意,如果您正在运行R的64位版本,则需要安装Java运行时的64位版本。

解决方案(Mac OSX):

从Mac OSX的较新版本开始,这变得更加复杂。在计算机上安装Java Development Kit后,需要遵循一组特定的命令。这些记录在rJava Issue 86 github page上。我提供了此解决方案的屏幕截图,供人们直接参考。

enter image description here

解决方案(Ubuntu):

使​​用Ubuntu Advanced Packaging Tool安装Java,然后在R中重新配置Java。
  sudo apt-get install openjdk-8-jdk # openjdk-9-jdk has some installation issues
  sudo R CMD javareconf

然后在R / RStudio中安装xlsx软件包。

  install.packages("xlsx")

Windows中的32位和64位Java

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

例如,如果已安装R的64位版本,但已安装32位版本的Java Runtime Environment,则R将无法看到Java Runtime Environment,并生成与以下相同的“未安装Java错误”:如上所述。

SOLUTION:

可以通过installing the 64-bit version of Java Runtime for Windows或通过更改RStudio配置以使用R的32位版本来解决此问题。
© www.soinside.com 2019 - 2024. All rights reserved.