在 OS X Mavericks 上安装 RODBC/ROracle 包

2021-12-05 00:00:00 r oracle rodbc

我一直在尝试安装一些软件包,例如 RODBC、ROracle 或 RGtk2 - 用于拨浪鼓.如果有人能帮助我解决这个问题,我将不胜感激,这样我就可以在带有 Mavericks 和 3.1.1 R 的 Mac 上使用这些应用程序

I have been trying to install some packages such as RODBC, ROracle or RGtk2 - used for rattle. I would really appreciate it if someone could help me get around this problem so that I could use these applications on a Mac with mavericks and with 3.1.1 R

RODBC 和 RGtk2 都出现这种错误:

I get this sort of error for both RODBC and RGtk2:

包‘RGtk2’可作为源包而不是二进制包install.packages 中的警告:包RGtk2"不可用(对于R 版本 3.1.1)

package ‘RGtk2’ is available as a source package but not as a binary Warning in install.packages : package ‘RGtk2’ is not available (for R version 3.1.1)

当我尝试使用 type='source' 时,我可以下载或开始下载,但随后我收到此类错误消息...

When I try to use type='source', I can download or start to download but then I get this sort of error message...

install.packages 中的警告:安装包‘RODBC’有非零退出状态

Warning in install.packages : installation of package ‘RODBC’ had non-zero exit status

推荐答案

此答案将涵盖在 OS X 上安装 RODBC 和 ROracle,特别是在 Mavericks 及更高版本上.

This answer will cover installing RODBC and ROracle on OS X, specifically on Mavericks and later.

这两个包现在都需要从源代码构建(编译).这意味着您需要的第一件事是下载 XCode 和相关的命令行工具",以便您实际上拥有一个编译器.基本上每个版本的 OS X/XCode 都改变了你这样做的方式,但如果你开始谷歌搜索,你可能最终会登陆这里.

Both packages now need to be built (compiled) from source. This means that the first thing you need is to download XCode and the associated "command line tools" so that you actually have a compiler. How you do this has changed with basically every version of OS X/XCode, but if you start Googling you will probably end up landing here.

一旦你有了它......

Once you have that in place...

RODBC

自 OS X 10.9 (Mavericks) 起,Apple 停止包含 iODBC SQL 头文件以及OS X 上的 R 用户为了构建 R 习惯安装的命令行工具"源代码包.

Since OS X 10.9 (Mavericks) Apple stopped including the iODBC SQL header files along with the "command line tools" that R users on OS X are accustomed to installing in order to build R packages from source.

因此,如果此时您尝试在 Mavericks 上从源代码构建 RODBC,您应该会收到如下错误:

So if you try to build RODBC from source on Mavericks at this point you should get an error like:

配置:错误:找不到 ODBC 标头 sql.h 和 sqlext.h"

configure: error: "ODBC headers sql.h and sqlext.h not found"

要解决此问题,您需要下载最新版本的 iODBC (www.iodbc.org),然后解压缩文件并设置标题和库搜索路径到您放置解压缩包的位置.如果你不知道怎么做要设置搜索路径,您可以将头文件(sql.h 和 sqlext.h)放在 /usr/include 目录中,和 /usr/lib 目录中的 libiodbc.a 文件.

To fix this, you need to download the latest version of iODBC (www.iodbc.org), and then unzip the file and set your header and library search paths to the location where you put the unzipped package. If you don't know how to set your search paths, you could just put the header files (sql.h and sqlext.h) in the /usr/include directory, and the libiodbc.a file in the /usr/lib directory.

那么你应该可以做到

install.packages("RODBC",type = "source")

没有任何问题.据推测,这也适用于其他主要的开源 ODBC 项目 unixODBC,但我还没有尝试过.

without any problems. Presumably this would work as well with the other major open source ODBC project, unixODBC, as well, but I have not tried that.

我已经在 El Capitan 上测试了这个(以及另一个答案中的自制方法),并且都仍然有效.

I have tested this (and the homebrew method in another answer) on El Capitan and both still work.

ROracle

这个比较复杂.但是,ROracle 可以安装在 OS X 上(我已经通过 El Capitan 在 Mountain Lion 上对此进行了测试).由于 ROracle 依赖于 Oracle Instant Client,因此没有可用的二进制文件任何平台.此处有详细的安装说明.

This is more complicated. However, ROracle can be installed on OS X (I have tested this on Mountain Lion through El Capitan). Since ROracle relies on the Oracle Instant Client, there are no binaries available for any platform. There are detailed installation instructions for the package here.

对于 OS X,我们需要(大部分)遵循 Linux 说明.

For OS X, we need to (mostly) follow the Linux instructions.

首先,下载合适的Oracle Instant Client 以及 SDK,如 Linuz 说明中所述.

First, download the appropriate Oracle Instant Client, as well as the SDK, as directed in the Linuz instructions.

Linux 安装说明提到从 RPM 安装客户端,这会将所有内容放在正确的位置".我无法弄清楚这是否适用于 OS X,所以我只是将客户端解压缩到我创建的目录中,并将 SDK 放在该目录中的 /sdk 中.

The Linux install instructions mention installing the client from an RPM, which will place everything "in the right place". I wasn't able to figure out whether that even applied for OS X, so I simply unzipped the client in a directory I created and placed the SDK inside that directory in /sdk.

Linux 安装说明然后指导我们为 libclntsh.so.11.1 创建符号链接.自从这是 OS X,它实际上被称为 libclntsh.dylib.11.1.所以我们需要cd到目录我们解压客户端然后运行

The Linux install instructions then direct us to create a symbolic link for libclntsh.so.11.1. Since this is OS X, it's actually called libclntsh.dylib.11.1. So we need to cd to the directory where we unzipped the client and then run

ln -s libclntsh.dylib.11.1 libclntsh.dylib

在 OS X 上,我们然后设置 DYLD_LIBRARY_PATH,而不是 LD_LIBRARY_PATH:

On OS X, we then set the DYLD_LIBRARY_PATH, not the LD_LIBRARY_PATH:

export DYLD_LIBRARY_PATH=/scratch/instantclient_11_2:$DYLD_LIBRARY_PATH

使用适合您机器的任何路径.

using whatever path is correct for your machine.

我一直无法让 ROracle 使用 OCI_LIB 环境变量进行编译.反而,我使用了 --with-oci-lib 编译器标志选项:

I was never able to get ROracle to compile with an OCI_LIB environment variable. Instead, I used the --with-oci-lib compiler flag option:

R CMD INSTALL --configure-args='--with-oci-lib=/scratch/instantclient_11_2' ROracle_1.1-11.tar.gz

再次使用任何合适的路径和 ROracle 版本号.

again using whatever path and ROracle version number is appropriate.

如果您使用 tnsnames.ora 文件连接到 Oracle 数据库,您还需要设置 TNS_ADMIN 环境变量以指向该文件的位置.

If you are connecting to an Oracle database using a tnsnames.ora file, you'll also need to set a TNS_ADMIN environment variable to point to the location of that file.

最后一个问题:你可能会发现(正如我所做的那样)只有当你启动 R 时,这一切才会奏效从命令行(即终端).具体来说,如果您启动 RGui.app 或GUI 中的 RStudio.app,然后尝试运行 library(ROracle) 你会得到一个错误大意是

One last gotcha: you may find (as I did) that this will all work only if you launch R from the command line (i.e. Terminal). Specifically, if you launch either RGui.app or RStudio.app from the GUI, and then try to run library(ROracle) you will get an error saying something to the effect of

> library("ROracle")
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/Library/Frameworks/R.framework/Versions/3.1/Resources/library/ROracle/libs/ROracle.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.1/Resources/library/ROracle/libs/ROracle.so, 6): Library not loaded: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
  Referenced from: /Library/Frameworks/R.framework/Versions/3.1/Resources/library/ROracle/libs/ROracle.so
  Reason: image not found
Error: package or namespace load failed for ‘ROracle’

即使Sys.getenv("DYLD_LIBRARY_PATH")如实报告,这也会发生正确的路径.

This will happen even though Sys.getenv("DYLD_LIBRARY_PATH") faithfully reports the correct path.

有一段时间,我编写了一个解决方案",在其中我创建了一个启动的 shell 脚本RStudio/RGui 在启动时,只需要记住随后总是通过

For a while, I hacked together a "solution" where I created a shell script that launched RStudio/RGui upon startup, and just had to remember to always subsequently launch them via

open -a R.app
open -a RStudio.app

这个问题显然与如何使环境变量可用有关到 OS X 中的 GUI 启动应用程序.通常情况下,我最终找到了StackOverflow 上的解决方案.编辑/etc/launchd.conf 文件解决了这个问题,所以即使 RStudio/RGui 现在也能加载 ROracle从 Finder 启动.尽管请注意 此处 不再支持 /etc/launchd.conf 解决方案优胜美地.该答案表明您现在需要设置一个启动 plist 文件,以便在启动时为 launchctl 设置环境.

The problem apparently has to do with how environment variables are made available to GUI launched applications in OS X. As is usually the case, I eventually found the solution on StackOverflow. Editing the /etc/launchd.conf file solved this problem, so ROracle now loads even when RStudio/RGui are launched from the Finder. Though note here that the /etc/launchd.conf solution is no longer supported in Yosemite. That answer suggests you now need to set up a start-up plist file just to set the environment for launchctl on startup.

ROracle 的 El Capitan 更新

我现在已经在 El Capitan 中完成了这个实验,它的工作原理如上所述,但前提是您首先禁用系统完整性保护!这样做相当快,而且可以通过 Google 轻松找到说明.

I've now gone through the crucible on this one in El Capitan and it works as described above, but only if you disable System Integrity Protection first! Doing that is fairly quick, and instructions are easily found via Google.

呸.

相关文章