在带有 R 的 Win7 64 位上使用 rJava 包 [英] Using the rJava package on Win7 64 bit with R

查看:74
本文介绍了在带有 R 的 Win7 64 位上使用 rJava 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在装有 Win 7 64 位的计算机上安装 rJava.当我跑

I'm trying to install rJava on a computer with Win 7 64 bit. When I run

install.packages("rJava")

一切似乎都很好:

Installing package(s) into ‘C:/Users/djq/Documents/R/win-library/2.13’
(as ‘lib’ is unspecified)
trying URL 'http://software.rc.fas.harvard.edu/mirrors/R/bin/windows/contrib/2.13/rJava_0.9-1.zip'
Content type 'application/zip' length 654936 bytes (639 Kb)
opened URL
downloaded 639 Kb

package 'rJava' successfully unpacked and MD5 sums checked

The downloaded packages are in
    C:\TEMP\RtmpW2eAUw\downloaded_packages

但是当我尝试加载库 library(rJava) 时,我收到以下错误:

but when I tried to load the library library(rJava) I get the following error:

Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: inDL(x, as.logical(local), as.logical(now), ...)
  error: unable to load shared object 'C:/Users/me/Documents/R/win-library/2.13/rJava/libs/x64/rJava.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.

Error: package/namespace load failed for 'rJava'

我已经安装了 Java JRE(尝试了 32 位和 64 位版本)和 JDK(同时使用了 32 位和 64 位).我的 R 版本是 R version 2.13.1 (2011-07-08),我使用的是 RStudio.

I have installed the Java JRE (trying both 32 and 64 bit versions) and the JDK (using both 32 and 64 bit). My R version is R version 2.13.1 (2011-07-08) and I'm using RStudio.

如果我尝试使用 32 位 R,我可以很好地加载 rJava.问题仅仅是 rJava 不能在 R 64 位中运行吗?如何判断一个包裹是否只适用于 32 而不是 64?

If I try using 32 bit R, I can load rJava fine. Is the problem just that rJava does not run in R 64 bit? How can I tell if a package is just for 32 instead of 64?

刚刚发布R.version():

> R.Version()
$platform
[1] "x86_64-pc-mingw32"

$arch
[1] "x86_64"

$os
[1] "mingw32"

$system
[1] "x86_64, mingw32"

$status
[1] ""

$major
[1] "2"

$minor
[1] "13.1"

$year
[1] "2011"

$month
[1] "07"

$day
[1] "08"

$`svn rev`
[1] "56322"

$language
[1] "R"

$version.string
[1] "R version 2.13.1 (2011-07-08)"

推荐答案

更新(2018 年 7 月):

最新的CRAN版本的rJava会自动找到jvm.dll,无需手动设置PATHJAVA_HOME.但是请注意:

Update (July 2018):

The latest CRAN version of rJava will find the jvm.dll automatically, without manually setting the PATH or JAVA_HOME. However note that:

  • 要在 32 位 R 中使用 rJava,您需要 Windows x86 的 Java
  • 要在 64 位 R 中使用 rJava,您需要 Windows x64 的 Java
  • 要使用多架构(默认)构建或检查 R 包,您需要安装 Java For Windows x64 以及 Java for Windows x86.在 Win 64 上,前者安装在 C:\Program files\Java\ 中,后者安装在 C:\Program Files (x86)\Java\ 中,因此它们不会冲突.
  • To use rJava in 32-bit R, you need Java for Windows x86
  • To use rJava in 64-bit R, you need Java for Windows x64
  • To build or check R packages with multi-arch (the default) you need to install both Java For Windows x64 as well as Java for Windows x86. On Win 64, the former installs in C:\Program files\Java\ and the latter in C:\Program Files (x86)\Java\ so they do not conflict.

从 Java 版本 9 开始,已停止支持 x86 (win32).因此,最新的多架构设置是同时安装 jdk-8u172-windows-i586.exejdk-8u172-windows-x64.exe 然后是 CRAN 的二进制包:

As of Java version 9, support for x86 (win32) has been discontinued. Hence the latest working multi-arch setup is to install both jdk-8u172-windows-i586.exe and jdk-8u172-windows-x64.exe and then the binary package from CRAN:

install.packages("rJava")

来自 CRAN 的二进制包应该自己在 jvm 上获取.仅限专家:要从源代码构建 rJava,您需要 --merge-multiarch 标志:

The binary package from CRAN should pick up on the jvm by itself. Experts only: to build rJava from source, you need the --merge-multiarch flag:

install.packages('rJava', type = 'source', INSTALL_opts='--merge-multiarch')

<小时>

旧答案:

(注意:其他答案/评论中的许多人都说要删除 JAVA_HOME,所以考虑一下.我最近没有重新讨论这个问题,不知道是否仍然需要以下所有步骤.)


Old anwser:

(Note: many of folks in other answers/comments have said to remove JAVA_HOME, so consider that. I have not revisited this issue recently to know if all the steps below are still necessary.)

这里有一些关于如何在 64 位 Windows 7 上启动和运行 R + rJava 的快速建议.有几种可能性,但大多数都有致命的缺陷.以下是对我有用的方法:

Here is some quick advice on how to get up and running with R + rJava on Windows 7 64bit. There are several possibilities, but most have fatal flaws. Here is what worked for me:

将 jvm.dll 添加到您的 PATH

rJava,R<->Java 桥接器,需要 jvm.dll,但 R 很难找到那个 DLL.它驻留在像

rJava, the R<->Java bridge, will need jvm.dll, but R will have trouble finding that DLL. It resides in a folder like

C:\Program Files\Java\jdk1.6.0_25\jre\bin\server

C:\Program Files\Java\jre6\jre\bin\client

无论您在哪里,都将该目录添加到您的 Windows PATH 变量中.(Windows -> "Path" -> "Edit environment variables to for your account" -> PATH -> 编辑值.)

Wherever yours is, add that directory to your windows PATH variable. (Windows -> "Path" -> "Edit environment variables to for your account" -> PATH -> edit the value.)

您的 PATH 上可能已经安装了 Java.如果是这样,您应该在与 PATH 中已有的相同的 Java主"目录中找到客户端/服务器目录.

You may already have Java on your PATH. If so you should find the client/server directory in the same Java "home" dir as the one already on your PATH.

为了安全起见,请确保您的架构匹配.如果您在 Program Files 中有 Java,则它是 64 位的,因此您应该运行 R64.如果您在 Program Files (x86) 中有 Java,那就是 32 位,所以您使用普通的 32 位 R.

To be safe, make sure your architectures match.If you have Java in Program Files, it is 64-bit, so you ought to run R64. If you have Java in Program Files (x86), that's 32-bit, so you use plain 32-bit R.

从 Windows 菜单重新启动 R

如果 R 正在运行,请退出.

If R is running, quit.

从开始菜单,启动 R/RGUI,RStudio.这非常重要,可以让 R 接受您的 PATH 更改.

From the Start Menu , Start R / RGUI, RStudio. This is very important, to make R pick up your PATH changes.

安装 rJava 0.9.2.

早期版本不起作用!镜像不是最新的,因此请访问 www.rforge.net 上的源:http://www.rforge.net/rJava/files/.注意那里的建议

Earlier versions do not work! Mirrors are not up-to-date, so go to the source at www.rforge.net: http://www.rforge.net/rJava/files/. Note the advice there

"Please use

`install.packages('rJava',,'http://www.rforge.net/')`

to install."

这几乎是正确的.这确实有效:

That is almost correct. This actually works:

install.packages('rJava', .libPaths()[1], 'http://www.rforge.net/')

注意标点符号!神秘的.libPaths()[1]"只是告诉 R 将包安装在主库目录中.出于某种原因,将该值留空是行不通的,即使它应该是默认值.

Watch the punctuation! The mysterious ".libPaths()[1]," just tells R to install the package in the primary library directory. For some reason, leaving the value blank doesn’t work, even though it should default.

这篇关于在带有 R 的 Win7 64 位上使用 rJava 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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