1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++からRInsideを使えるようにする

Posted at

C++内部からRを呼び出すインターフェイスを提供するRInsideをUbuntuにインストールする手順です。
(RInside自体の使用方法はこの記事では扱っていません。)

RInside

RInsideはC++からRを呼び出すことを目的としたパッケージです。
RからC++を扱うためのパッケージであるRcppを前提として使用できます。

(抜粋)
The RInside package provides a few classes for seamless embedding of R inside of C++ applications by relying on Rcpp.
https://cran.r-project.org/web/packages/RInside/index.html

GPL(>=2)ライセンスのもとで提供されているOSSであり、Linux/OS X/Windowsのクロスプラットフォームで使用できます。

インストール手順

環境

Ubuntu : 18.04.3 LTS
R : 3.4.4
Rcpp : 1.0.3
RInside : 0.2.15
g++ : 7.4.0
※ Ubuntuはdockerのコンテナーとして使用

Rのインストール

aptでインストールする。必要に応じて事前にupdateを行っておく。
途中ロケーション・TimeZoneの選択を促されるので適当に選択する。

$ apt update
$ apt install r-base

OSのターミナルからRコマンドを実行して、プロンプトが表示されればOK。

$ R
R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 

Rcppのインストール

RInsideの前提条件となっているRcppをCRANから導入する。
Rのコマンドラインからinstall.packages()でインストールする。RcppがロードできればOK。

> install.packages('Rcpp')
> library('Rcpp')
> 

RInsideのインストール

RInsideをこちらからソースコードをダウンロードする。

$ wget https://cran.r-project.org/src/contrib/RInside_0.2.15.tar.gz

以下のコマンドを実行して、ビルドしインストールする。

$ R CMD INSTALL RInside_0.2.15.tar.gz 
* installing to library '/usr/local/lib/R/site-library'
* installing *source* package 'RInside' ...
** package 'RInside' successfully unpacked and MD5 sums checked
** libs
/usr/lib/R/bin/Rscript tools/RInsideAutoloads.r > RInsideAutoloads.h
/usr/lib/R/bin/Rscript tools/RInsideEnvVars.r   > RInsideEnvVars.h
g++  -I/usr/share/R/include -DNDEBUG -I. -I../inst/include/ -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c MemBuf.cpp -o MemBuf.o
g++  -I/usr/share/R/include -DNDEBUG -I. -I../inst/include/ -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c RInside.cpp -o RInside.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o RInside.so MemBuf.o RInside.o -L/usr/lib/R/lib -lR
g++  -o libRInside.so MemBuf.o RInside.o -shared -Wl,-Bsymbolic-functions -Wl,-z,relro   -L/usr/lib/R/lib -lR
ar qc libRInside.a MemBuf.o RInside.o
cp libRInside.so ../inst/lib
cp libRInside.a ../inst/lib
rm libRInside.so libRInside.a
installing to /usr/local/lib/R/site-library/RInside/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (RInside)

確認

Eddelbuettel氏が提供しているgitリポジトリーからサンプルコードを入手できる。
rinside/inst/examples/standardの下にサンプルコードおよびGNUmakefileが用意されているので、ビルドし実行する。

# 単一ファイルのビルド
$ make rinside_sample0
g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RInside/include -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -Wall    rinside_sample0.cpp  -Wl,--export-dynamic -fopenmp -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -licuuc -licui18n  -lblas -llapack  -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o rinside_sample0

$ ./rinside_sample0
Hello, world!

他のファイルも同様にビルドして使用することができます。

以上です。

参考リンク

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?