我已經成功解決了這個問題。Here你可以找到我所做過的詳細解釋。也許,這對大多數人來說是微不足道的,但對於與我處於同一位置的人來說,這可能是一個很好的起點。我會在這裏發佈我的簡歷。
首先我安裝了檸檬圖(C++)庫(LGL)。我剛從主頁下載LGL(from here)。然後我繼續:
$ tar xvzf lemon-1.2.tar.gz
$ cd lemon-1.2
$ ./configure
$ make
$ make check # This is optional, but recommended. It runs a bunch of tests.
$ sudo make install
然後檢查它是否有效。因此,在一個文件名爲mycode.cc我把:
#include <iostream>
#include <lemon/list_graph.h>
using namespace lemon;
using namespace std;
int main()
{
ListDigraph g;
ListDigraph::Node u = g.addNode();
ListDigraph::Node v = g.addNode();
ListDigraph::Arc a = g.addArc(u, v);
cout << "Hello World! This is LEMON library here." << endl;
cout << "We have a directed graph with " << countNodes(g) << " nodes "
<< "and " << countArcs(g) << " arc." << endl;
return 0;
}
然後我編譯並運行它:
$ g++ -O2 mycode.cc -lemon
... BLA BLA BLA ...
$./a.out
Hello World! This is LEMON library here.
We have a directed graph with 2 nodes and 1 arc.
所以它的工作原理。現在,想法是通過Rcpp將該代碼集成到R中。因此,在某些目錄中我打開A R控制檯,我做的:
> require("Rcpp")
Loading required package: Rcpp
> Rcpp.package.skeleton("pkgwithlgl")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './pkgwithlgl/Read-and-delete-me'.
Adding Rcpp settings
>> added Depends: Rcpp
>> added LinkingTo: Rcpp
>> added useDynLib directive to NAMESPACE
>> added Makevars file with Rcpp settings
>> added Makevars.win file with Rcpp settings
>> added example header file using Rcpp classes
>> added example src file using Rcpp classes
>> added example R file calling the C++ example
>> added Rd file for rcpp_hello_world
所以,在這樣我就創建了一個名爲pkgwithlgl一個新的基於RCPP源碼包。現在在pkgwithlgl目錄(它是我想要修改和安裝的軟件包的源代碼)內有一個名爲src的目錄。裏面有包含C++代碼的文件。尤其是,存在一個名爲* rcpp_hello_world.cpp *包含:
#include "rcpp_hello_world.h"
SEXP rcpp_hello_world(){
using namespace Rcpp ;
CharacterVector x = CharacterVector::create("foo", "bar") ;
NumericVector y = NumericVector::create(0.0, 1.0) ;
List z = List::create(x, y) ;
return z ;
}
現在,我修改它,所以它變成了:
#include "rcpp_hello_world.h"
#include <lemon/list_graph.h>
using namespace lemon ;
SEXP rcpp_hello_world(){
using namespace Rcpp ;
int res = 1;
ListDigraph g;
ListDigraph::Node u = g.addNode();
ListDigraph::Node v = g.addNode();
ListDigraph::Arc a = g.addArc(u, v);
int i = countNodes(g);
int j = countArcs(g);
Rprintf("num nodes is %d , and num edges is %d \n",i,j);
return wrap(res) ;
}
然後,從Linux控制檯源的容器目錄包我寫:
$ R CMD INSTALL pkgwithlgl
返回:
* installing to library ‘/home/juan/R/x86_64-pc-linux-gnu-library/2.12’
* installing *source* package ‘pkgwithlgl’ ...
** libs
g++ -I/usr/share/R/include -I"/usr/local/lib/R/site-library/Rcpp/include" -fpic -O3 -pipe -g -c rcpp_hello_world.cpp -o rcpp_hello_world.o
g++ -shared -o pkgwithlgl.so rcpp_hello_world.o -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/lib64/R/lib -lR
installing to /home/juan/R/x86_64-pc-linux-gnu-library/2.12/pkgwithlgl/libs
** R
** preparing package for lazy loading
** help
Warning: /home/juan/Desktop/facu/investigacion_ensayos/Cosas_crudas/programming_coding/R-work- space/integrating_R_with_cpp_via_Rcpp/using_packages/pkgwithlgl/man/pkgwithlgl- package.Rd:32: All text must be in a section
Warning: /home/juan/Desktop/facu/investigacion_ensayos/Cosas_crudas/programming_coding/R-work- space/integrating_R_with_cpp_via_Rcpp/using_packages/pkgwithlgl/man/pkgwithlgl- package.Rd:33: All text must be in a section
*** installing help indices
converting help for package ‘pkgwithlgl’
finding HTML links ... done
pkgwithlgl-package html
rcpp_hello_world html
** building package indices ...
** testing if installed package can be loaded
* DONE (pkgwithlgl)
因此,軟件包已安裝(警告與我沒有以正確的方式填充.Rd文件,即包含有關軟件包的幫助文件的事實有關)。我打開一個R控制檯,並寫道:
> require("pkgwithlgl")
Loading required package: pkgwithlgl
Loading required package: Rcpp
> rcpp_hello_world()
num nodes is 2 , and num edges is 1
[1] 1
所以它的工作!就這些。
但是嘿!如果我構建這個包並將其上傳到(例如)CRAN(我不會這麼做)會發生什麼。如果有人從CRAN安裝這個軟件包,它會爲他工作嗎?即使它不安裝LGL包?
問候
將一些預包裝就像http://pinard.progiciels-bpi.ca/libR/library/graph/doc/index.html爲你工作? – 2011-02-07 01:19:49
您可能需要將五個標籤中的一個換成`r`。 – 2011-02-07 01:40:22