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.
Error in reading xlsx file using `read.xlsx` function in R
For your existing code, you might have to check that your have the right version of Java installed on your computer. On the Java download page, pick the version that is right for your operating system and your machine. (And also check that your R packages are up to date, which you can do in RStudio with "Packages tab > Update".)
Alternatively, as Bappa Das suggested, I recommend using an alternative function that doesn't depend on Java (like the package xlsx does, which is I assume the package you are using). For example, try with the readxl package instead:
setwd("D:\\11\\2020-2021\\Diplom\\research\\hranalytic")
add <- "train.xlsx"
library(readxl)
df <- read_xlsx(add, sheet = "train1")
Related Topics
Libavcodec.So: Has Text Relocations
How to Transform Currenttimemillis to a Readable Date Format
Preload Multiple Images with Glide
Using Httpclient and Httppost in Android with Post Parameters
How to Move/Rename File from Internal App Storage to External Storage on Android
Caused By: Java.Lang.Classnotfoundexception: Didn't Find Class in Firebase Phone Authentication
Networksecurityconfig: No Network Security Config Specified -- Android 7.0 Error
How to Update Xml File from Another Xml File Dynamically
Setting Android Images from String Value
Android Calculate Days Between Two Dates
How to Adjust Microphone Sensitivity While Recording Audio in Android
Android Proguard, Keep Inner Class
Pixel-Perfect Collision Detection Android