write.xlsx function not working - r

I am trying to use .xlsx library but function write.xlsx is returning error that such can not be found.
When I am installing library(xlsx) in log I can read:
Error : .onLoad nie powiodło się w funkcji 'loadNamespace()' dla pakietu 'rJava', szczegóły:
wywołanie: fun(libname, pkgname)
błąd: No CurrentVersion entry in Software/JavaSoft registry! Try re-installing Java and make sure R and Java have matching architectures.
In addition: Warning messages:
1: pakiet ‘xlsx’ został zbudowany w wersji R 3.3.2
2: pakiet ‘rJava’ został zbudowany w wersji R 3.3.3
Error: pakiet ‘rJava’ nie mógł zostać załadowany
Java is up to date.

The code in the original post fails because the xlsx package uses the Apache POI Java API to Excel, and therefore requires the rJava package. In turn, the rJava package requires a working, compatible version the Java Runtime Environment to be installed on the machine and accessible from R.
One can tell whether Java is accessible from R / RStudio via the system() function.
> 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)
>
There are at least four sets of R packages used for working with Excel files, including:
xlsx -- requires rJava package
XLConnect -- requires rJava package
openxlsx -- does not require rJava package
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)
A Working Example: Write to Excel File
library(writexl)
data <- data.frame(matrix(runif(100),nrow=10,ncol=10))
write_xlsx(data,"./data/simpleExcel.xlsx")
...and the output:
If You Must Use rJava...
Unfortunately, options 1 and 2 are considerably more complicated than "install Java." If one must use xlsx or needs the rJava package to support other R packages, installation of Java varies significantly by operating system.
Windows: one must install a version of Java whose architecture is compatible with R (i.e. 32-bit vs. 64-bit). One may consider installing both 32-bit and 64-bit versions because some Windows programs installed on the computer may require 32-bit Java vs. 64-bit. With RStudio, one can configure R to use the 32-bit version of R if only 32-bit Java is installed on the machine.
Mac OS X: one must install Java and run a series of commands that are documented on the rJava Issues GitHub page, including executing an R script to reconfigure Java for R.
Linux: one needs to install Java using the package installer tool appropriate for the version of Linux, and then configure R to use it. For example, in Ubuntu one would install with the advanced packaging tool.
sudo apt-get install openjdk-8-jdk # openjdk-9-jdk has some installation issues
sudo R CMD javareconf

xlsx needs Java. Please install the current Java version from https://www.java.com/de/
and watch out, that both R and java are either 32bit or 64bit as it is stated in the error message
... and make sure R and Java have matching architectures.
Or use writexls or openxlsx. They are not depending on Java (Thanks #Len)

Related

Exporting excel files from R in windows [duplicate]

I am trying to load the rJava using the command:
library(rJava)
But I get the following error:
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: fun(libname, pkgname)
error: JAVA_HOME cannot be determined from the Registry
Error: package or namespace load failed for ‘rJava’
I tried reinstalling both the Java and the R program with the latest versions
but still get these errors..
May I know how I can resolve this? Thank you very much!
This error is often resolved by installing a Java version (i.e. 64-bit Java or 32-bit Java) that fits to the type of R version that you are using (i.e. 64-bit R or 32-bit R). This problem can easily effect Windows 7 users, since they might have installed a version of Java that is different than the version of R they are using.
http://www.r-statistics.com/2012/08/how-to-load-the-rjava-package-after-the-error-java_home-cannot-be-determined-from-the-registry/
Install 64 bit Java from https://java.com/en/download/manual.jsp.
Then in windows cmd run
setx PATH "C:\Program Files\Java\jre1.8.0_211\bin\server;%PATH%"
(make sure your path is correct).
Then in RStudio run
Sys.setenv(JAVA_HOME="")
Now, you should be able to run rJava!!
I got the same error resolved by installing same version of R and Java i.e. 64 bits and manually updating the path i.e. ,
Sys.setenv(JAVA_HOME='C:/Program Files/Java/jre1.8.0_121') # for 64-bit version
R for Windows installer installs by default both 32-bit and 64-bit files. Reinstalling R and unticking 32-bit part solved the problem for me.
Under Windows 10, first check your (running) R-Version:
R.version
If you see something like x86_64, a 64-bit version of Java is needed.
Simply downloading and installing the matching Java version (https://java.com/en/download/manual.jsp) solved the problem for me. No reinstall of R needed (as suggested by other users here). There also was no need to set the path explicitly.
As mentioned in the r-statistics link cited above, you have to manually download the 64-bit version of Java. If not, then the 32-bit version will be installed.
So, if you have a 64-bit operating system, then ensure that both R and Java are using the 64-bit installs. Otherwise, this problem will persist.
This has been killing me and I have tried multiple methods above, it doesn't work. After, I mix all solution to try and below process works for me.
Install Java for 64 bit
Run below code in windows command
setx PATH "C:\Program Files\Java\jre1.8.0_171\bin\server;%PATH%"
(please change the address acorrding to your dvm.dll real address)
Run below in R studio
Sys.setenv(JAVA_HOME="")
Then I finally can run
library(rJava)
library(xlsx)
I faced the same issue and was feeling very down as I couldnt get my analysis done.
This worked for me :
check your operating system. 64 bit or 32 bit.
https://helpx.adobe.com/x-productkb/global/determine-whether-computer-running-32.html
uninstall previous versions of Java.
delete rJAVA library from Doc>R>win-library. If you have more than 1 versions of R, make sure you delete rJAVA package from all of them.
install Java based on system requirement (64 bit or 32 bit).
and finally restart R before and after installing rJava package.
If R is 64-bit version make sure to install Java 64-bit version!
If R is x86-bit version make sure to install Java x86-bit version!
You can add INSTALL_opts=c("--no-multiarch") to your install_github() call:
devtools::install_github("mne-tools/mne-r", INSTALL_opts=c("--no-multiarch"))
For more info: https://github.com/mne-tools/mne-r/issues/5#issuecomment-489588267
Create a new directory in C: drive as "library"
set your libPath this directory. you need to type
.libPath("C:/library")
reinstall packages which requires java (xlsx, rJava, RJDemetra)
it will give error message again but it will work.

Rserve installation on macos 11 Big Sur

I have upgraded mac OS to version 11 - Big Sur. Since then, I'm not able to install Rserve package. I've tried the installation using the following command:
install.packages('Rserve',,'http://www.rforge.net/')
Unfortunately, the result is a non-zero exit and the output contain the following messages:
configure error: R was configured without --enable-R-shlib or --enable-R-static-lib```
*** Rserve requires R (shared or static) library.
*** Pleae install R library or compile R with either --enable-R-shlib
or --enable-R-static-lib support.
Alternatively use --without-server if you wish to build only Rserve client.
No Rserve client is totally no option for me. Do you have any suggestions what can I do go get Rserve installed?
Thanks a lot for your help!

Although the xlsx package is installed in R, it does not work

Although the xlsx package is installed, it does not work.
I get the following error. What should I do? I use the latest version of R
install.packages("xlsx")
Installing package into ‘C:/Users/Cgdm/Documents/R/win-library/4.0’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/xlsx_0.6.5.zip'
Content type 'application/zip' length 374910 bytes (366 KB)
downloaded 366 KB
package ‘xlsx’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Cgdm\AppData\Local\Temp\RtmpCy2Eng\downloaded_packages
library(xlsx)
Error: package or namespace load failed for ‘xlsx’:
onLoad failed in loadNamespace() for 'rJava', details:
call: fun(libname, pkgname)
error: JAVA_HOME cannot be determined from the Registry
Common Problems -- Java and xlsx package
R users who have not previously used packages that rely on Java often have problems when attempting to use the xlsx package that is used to read Excel spreadsheets.
Java runtime not installed
First, many new R users have not previously needed to install a Java runtime on their computers. The xlsx package depends on the rJava and xlsxjars packages. rJava requires the Java Runtime Environment 1.2 or above to also be present on one's computer.
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(...)
Solution 2: Install Java and required R packages
That said, for people who still want to use the xlsx package, 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.
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.
One can identify the version of Java that is installed, and whether it is 32 or 64-bit, by executing the following function within R / RStudio.
system("java -version")
...which on a Windows-based PC will return something like this.
Extracting Java settings from Windows registry
Another approach to validating the version of Java that is installed on a Windows-based machine is to use utils::readRegistry() (h/t Access Windows Registry inside R).
readRegistry("SOFTWARE\\JavaSoft\\Java Runtime Environment","HLM",maxdepth=3)
returns the following:
> readRegistry("SOFTWARE\\JavaSoft\\Java Runtime Environment","HLM",maxdepth=3)
$BrowserJavaVersion
[1] "11.261.2"
$CurrentVersion
[1] "1.8"
$`1.8`
$`1.8`$JavaHome
[1] "C:\\Program Files\\Java\\jre1.8.0_261"
$`1.8`$MicroVersion
[1] "0"
$`1.8`$RuntimeLib
[1] "C:\\Program Files\\Java\\jre1.8.0_261\\bin\\server\\jvm.dll"
$`1.8.0_261`
$`1.8.0_261`$JavaHome
[1] "C:\\Program Files\\Java\\jre1.8.0_261"
$`1.8.0_261`$MicroVersion
[1] "0"
$`1.8.0_261`$RuntimeLib
[1] "C:\\Program Files\\Java\\jre1.8.0_261\\bin\\server\\jvm.dll"
$`1.8.0_261`$MSI
$`1.8.0_261`$MSI$AUTOUPDATECHECK
[1] "1"
$`1.8.0_261`$MSI$AUTOUPDATEDELAY
[1] ""
$`1.8.0_261`$MSI$EULA
[1] ""
$`1.8.0_261`$MSI$FROMVERSION
[1] "NA"
$`1.8.0_261`$MSI$FROMVERSIONFULL
[1] ""
$`1.8.0_261`$MSI$FullVersion
[1] "1.8.0_261-b12"
$`1.8.0_261`$MSI$INSTALLDIR
[1] "C:\\Program Files\\Java\\jre1.8.0_261\\"
$`1.8.0_261`$MSI$JAVAUPDATE
[1] "1"
$`1.8.0_261`$MSI$JU
[1] ""
$`1.8.0_261`$MSI$OEMUPDATE
[1] ""
$`1.8.0_261`$MSI$PRODUCTVERSION
[1] "8.0.2610.12"
This is a lot of output, but we can still see that the current version of Java is 1.8, and the JavaHome setting is C:\\Program Files\\Java\\jre1.8.0_261. Since the registry tree is relatively complicated, it takes a fair amount of knowledge to write the correct key to reduce the amount of output returned.
# more specific extract, given that $CurrentVersion is 1.8
readRegistry("SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.8","HLM",maxdepth=3)
...returns a more targeted set of registry settings.
> readRegistry("SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.8","HLM",maxdepth=3)
$JavaHome
[1] "C:\\Program Files\\Java\\jre1.8.0_261"
$MicroVersion
[1] "0"
$RuntimeLib
[1] "C:\\Program Files\\Java\\jre1.8.0_261\\bin\\server\\jvm.dll"
Java / R compatibility with non-English versions of Windows 10
Note that as of July 2020, users on Stackoverflow.com have reported problems installing Java and rJava in the scenario where the version of Windows is a non-English language version (e.g. Chinese, Polish, etc.). It appears that the way the Java installer works with these versions of Windows, R and the rJava package are not able to access the JAVA_HOME directory correctly.
To correct the problem, reinstall R with the same language used by Windows. That is, on the Chinese version of Windows, install R with Chinese langauge support. Once installed, you can change the language to English by setting language = "en" in the .Rconsole file.
Reference: Common Problems: Java and xlsx package, originally posted by me on my Johns Hopkins Data Science Specialization Community Mentor Repository, January 2017.
It seems that your JAVA_HOME is not configured correctly, hence
from https://www.rdocumentation.org/packages/xlsx:
"Ensure that the system environment variable JAVA_HOME is configured appropriately and points to your jdk of choice. Typically, this will be included in your PATH environment variable as well. Options and system environmental variables that are available from R can be seen with Sys.getenv()."
Check your config by doing
d <- Sys.getenv()
d[names(d) == "JAVA_HOME"]
It should point to your Java installation, e.g. "C:\Program Files\Java\jdk1.8.0_104"
On Windows 10, this can be set in "Advanced System Settings, System Properties, Environment Variables"

R package was installed by version with different internals

On my Win 7 Ent x64 I installed RStudio R-3.4.2.
I installed few packages that I need (RPostgreSQL, sqldf, etc..)
But when I'm executing code, these libraries give me errors:
library(RPostgreSQL)
Error: package ‘RPostgreSQL’ was installed by an R version with different internals; it needs to be reinstalled for use with this R version
In addition: Warning message:
package ‘RPostgreSQL’ was built under R version 3.5.0
All of the packages are downloaded from CRAN. RPostgrSQL is 0.6-2 version.
I'm pretty sure that solution is quite simple, but I searched a lot and still cannot find the solution.
How did you install the package RPostgreSQL? If you used
install.packages("RPostgreSQL") on your Windows it installed using binaries, which may have been built using a different R version. In your case, 3.5.0 vs 3.4.2.
Try using install.packages("RPostgreSQL", type="source"). This way your machine will compile the package from source using your version of R, and it should work.
If the package needs C/C++/Fortran compilation, you will als need RTools installed on your machine.
You can try to find all packages installed with the old version R like this:
grep 'Built: R 3.4' /usr/local/lib/R/site-library/*/DESCRIPTION > temp
and then you can uninstall the packages with remove.packages()

R - XLConnectJars install fails because rJava is not installed correctly

I am trying to install XLConnectJars to R and I keep getting the following error despite having Java DK and rJava installed?
Error: Package as namespace load failed for 'XLConnectJars'
.onLoad failed om LoadNamespace() for 'rJava', details:
call: library.dynam("rJava",pkgname,libname)
error: DLL 'rJava' not found: maybe not installed for this architecture?
Any help greatly appreciated!
XLConnectJars is a support package used by XLConnect, a platform-independent interface to Microsoft Excel. In order for XLConnectJars to install correctly, one must also install the rJava package.
The error noted in the OP indicates that rJava did not install correctly.
error: DLL 'rJava' not found: maybe not installed for this architecture?
Many people encounter problems installing rJava because it cannot access the Java Runtime from the operating system. Solutions to the "unable to access Java runtime" problem vary by operating system.
Windows
People often have 32-bit Java installed and then use 64-bit R. The 64-bit version of R requires the 64-bit version of Java.
Solution: Install the 64-bit version of the Java Runtime for Windows from the Java Download web page.
MacOS
Details to configure rJava on MacOS are covered in another SO answer I posted, Unable to load rJava in RStudio, which I am reposting here for convenience.
There is a very specific sequence of steps that must be taken to get rJava to work on a Macbook, as documented in rJava Issues #86.
Download and install Java from Oracle
Uninstall any previously installed version of rJava
Add JAVA_HOME to your .bashrc
Close & restart terminal, R and RStudio sessions so they pick up the updated JAVA_HOME
Use install.packages() to install rJava
See the URL link above for additional details on each step.
Ubuntu Linux
Use the Advanced Packaging Tool to install Java, then reconfigure Java support in R.
sudo apt-get install openjdk-8-jdk # openjdk-9-jdk has some installation issues
sudo R CMD javareconf
Once these steps are completed, install the XLConnectJars package with install.packages("XLConnectJars").
NOTE: some of this content is adapted from an article I previously posted on my Github site, Common Problems with Java and the xlsx Package.

Resources