write.xlsx 函数不起作用 [英] write.xlsx function not working

查看:68
本文介绍了write.xlsx 函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 .xlsx 库,但函数 write.xlsx 返回无法找到此类的错误.

当我在日志中安装库(xlsx)时,我可以阅读:

错误:.onLoad nie powiodło się w funkcji 'loadNamespace()' dla pakietu 'rJava', szczegóły:wywołanie: fun(libname, pkgname)błąd:Software/JavaSoft 注册表中没有 CurrentVersion 条目!尝试重新安装 Java 并确保 R 和 Java 具有匹配的体系结构.此外: 警告消息:1: pakiet ‘xlsx’ został zbudowany w wersji R 3.3.22: pakiet ‘rJava’ został zbudowany w wersji R 3.3.3错误:pakiet ‘rJava’ nie mógł zostać załadowany

Java 是最新的.

解决方案

原帖中的代码失败,因为 xlsx 包使用了

如果您必须使用 rJava...

不幸的是,选项 1 和 2 比安装 Java"要复杂得多.如果必须使用 xlsx 或需要 rJava 包来支持其他 R 包,Java 的安装因操作系统而异.

Windows: 必须安装架构与 R 兼容的 Java 版本(即 32 位与 64 位).可以考虑同时安装 32 位和 64 位版本,因为计算机上安装的某些 Windows 程序可能需要 32 位 Java 与 64 位.使用 RStudio,如果机器上只安装了 32 位 Java,则可以将 R 配置为使用 32 位版本的 R.

Mac OS X:必须安装 Java 并运行 rJava 问题 GitHub 页面,包括执行 R 脚本为 R 重新配置 Java.

Linux:需要使用适合 Linux 版本的软件包安装程序工具安装 Java,然后配置 R 以使用它.例如,在 Ubuntu 中,可以使用高级打包工具进行安装.

 sudo apt-get install openjdk-8-jdk # openjdk-9-jdk 有一些安装问题须藤 R CMD javareconf

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:

  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)

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

这篇关于write.xlsx 函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆