R xlsx package can not be used
The xlsx
package depends on the rJava
package, which requires a valid installation of the Java Runtime Environment (JRE). Before installing the rJava
package, one must install the Java Runtime Environment (JRE). The process for installing the JRE varies by operating system, and Oracle has made this process more difficult due to changes they made in the licensing requirements for Java starting in 2019.
How do I tell whether the JRE is installed?
As noted in the comments on the question, one can verify the version of the Java runtime that is installed on a machine with the command line java -version
. It is important to also confirm that the Java runtime is accessible from R / RStudio. We can do this by executing the system()
function within RStudio, also as noted in the comments for the question.
> 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
>
Reading Excel files into R
There are two distinct approaches to solving this problem. Here is a summary of instructions that I wrote on this topic for the Johns Hopkins Data Science Specialization Getting and Cleaning Data course, Common Problems: Java and xlsx package back in 2017.
Solution 1: Use an Excel Reader Package that Doesn't Require Java
PRO TIP: The easiest way to work around this problem is to use an R package that does not depend on Java, such as openxlsx or readxl.
For openxlsx
, it's very easy.
install.packages("openxlsx")
library(openxlsx)
# read the help file to identify the arguments needed to
# correctly read the file
?openxlsx
theData <- read.xlsx(...)
The same process can be used for readxl
.
install.packages("readxl")
library(readxl)
# read the help file to identify the arguments needed to
# correctly read the file
?readxl
theData <- read_excel(...)
Note that there is a complementary package, writexl
, that enables people to write Excel files.
Solution 2: Install Java and Required R Packages
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.
SOLUTION (Windows): Download and install the latest version of the Java Runtime Environment from Oracle. Note that if you are running the 64-bit version of R, you need to install the 64-bit version of the Java Runtime.
SOLUTION (Mac OSX): As of newer releases of Mac OSX, this has become more complicated. A specific set of commands needs to be followed after installing the Java Development Kit on the computer. These are documented on the rJava Issue 86 github page. I have included a screenshot of this solution for people to reference directly.
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
Then in R / RStudio install the xlsx
package.
install.packages("xlsx")
32-bit vs. 64-bit Java in Windows
Another common problem people may encounter is an incompatibility between the version of the Java Runtime Environment that is installed on their computer and the version of R, either 32-bit or 64-bit.
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.
.onLoad failed in loadNamespace() for 'rJava
I added the following and then rJava loaded without error.
Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre1.8.0_221')
Related Topics
Code Folding for Individual Chunks in R Markdown
Change Standard Error Color for Geom_Smooth
%>% .$Column_Name Equivalent for R Base Pipe |>
R: Need Finite 'Ylim' Values in Function
Creating "Word" Cloud of Phrases, Not Individual Words in R
Calculate Centroid Within/Inside a Spatialpolygon
R Cmd Check Not Looking for Gcc in Rtools Directory
Can Ggplot2 Find the Intersections - or Is There Any Other Neat Way
Regex to Remove All Non-Digit Symbols from String in R
Check Which Elements of a Vector Is Between the Elements of Another One in R
Duplicate Couples (Id-Time) Error in Plm with Only Two Ids
How to Scrape Website with Form Using Rvest
How to Do a Glm When "Contrasts Can Be Applied Only to Factors with 2 or More Levels"