[Solved] Write.xlsx error in R

The xlsx package uses the Apache POI Java API to Excel, therefore the code in the original post doesn’t work because it needs the rJava package. The Java Runtime Environment must also be installed on the computer and accessible from R in order for the rJava package to function and be compatible with it.

The system() method can be used to determine whether Java is accessible from R or RStudio.

system("java -version")
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

here are at least four sets of R packages used for working with Excel files, including:

  1. xlsx — requires rJava package
  2. XLConnect — requires rJava package
  3. openxlsx — does not require rJava package
  4. readxl / writexl — does not require rJava package

For options 3 and 4, the solution is simply to use install.packages() to install the desired package (as noted in another answer by @Linus), once you’ve updated R to the latest version.

install.packages("openxlsx") 
library(openxlsx)

OR

install.packages(c("readxl","writexl")) 
library(readxl)
library(writexl)