[Rcpp-devel] Could Rcpp class(like NumericVector) be implemented in c++?

Dirk Eddelbuettel edd at debian.org
Sun May 26 21:02:39 CEST 2013


On 26 May 2013 at 11:28, Kevin Ushey wrote:
| If you're trying to compile this as a standalone application, I guess you have
| to link against both libRcpp and (a generated?) libR. Try R-exts;
| specifically, http://cran.r-project.org/doc/manuals/r-devel/R-exts.html#
| Compiling-against-the-R-library .

Well even without a standalone, ie just to load into R, you need to know what
you are doing.  R CMD INSTALL, R CMD SHLIB etc pp all know that, but
translating that to Eclipse settings is far from trivial.

To minimize the pain, I suggest that Qiang sticks with either R CMD ..., or
switches to RStudio which does this for you (and does some extra checking).

Dirk
 
| -Kevin
| 
| 
| On Sun, May 26, 2013 at 10:52 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
| 
| 
|     On 26 May 2013 at 10:23, Qing Xia wrote:
|     | Dear All,
|     |
|     | I tried to use NumericVector in class declaration, Eclipse could compile
|     the
|     | code, but could not run it (something to do with the R.dll). I tried
|     replacing
|     | NumericVector with vector<double> and it worked with no problem. I want
|     to use
|     | NumericVector so that I could use Rcpp sugar to compute the mean and
|     variance.
|     | Could it possibly I get it wrong that sugar only take NumericVector? 
|     |
|     | I have configured Eclipse and I could ran the codes in the example folder
|     | perfectly. I've attached simplified version of my code. Any hints please?
|     I've
|     | been stuck on this.
| 
|     Well -- if you cannot get Eclipse to build your code, stop using Eclipse.
| 
|     Many of use just the command-line, or command-line in conjunction with an
|     editor like Emacs, others really like RStudio --- but you need to figure
|     this
|     out, or else you won't have a chance to set Eclipse up right.
| 
|     Dirk
| 
| 
|     | Thanks!
|     |
|     | Qing
|     |
|     | #include<Rcpp.h>
|     | using namespace Rcpp;
|     |
|     | class Stock{
|     | private:
|     | NumericVector prices;
|     | NumericVector returns;
|     | void calcReturns();
|     | public:
|     | void loadPricesFromFile(const char *symbol);
|     | NumericVector getReturns(unsigned);
|     | NumericVector getPrices(unsigned);
|     | double getMeanReturn(unsigned);
|     | double getReturnStdDev(unsigned);
|     | void plotHistory(unsigned);
|     | };
|     |
|     | void Stock::calcReturns(){
|     | for (int i=0;i<prices.size()-1;i++){
|     | returns.push_back(log(prices[i]/prices[i+1]));
|     | }
|     | };
|     |
|     | NumericVector Stock::getPrices(unsigned n){
|     | NumericVector vec(n);
|     | for (int i=0;i<vec.size();i++){
|     | vec.push_back(prices[i]);
|     | }
|     | return vec;
|     | }
|     |
|     | NumericVector Stock::getReturns(unsigned n){
|     | NumericVector vec(n);
|     | for (int i=0;i<vec.size();i++){
|     | vec.push_back(returns[i]);
|     | }
|     | return vec;
|     | }
|     |
|     | double Stock::getMeanReturn(unsigned n){
|     | NumericVector vec(n);
|     | vec=getReturns(n);
|     | double m=mean(vec);
|     | return m;
|     | }
|     |
|     | double Stock::getReturnStdDev(unsigned n){
|     | NumericVector vec(n);
|     | vec=getReturns(n);
|     | double m=var(vec);
|     | return m;
|     | }
|     |
|     | void Stock::plotHistory(unsigned n){
|     |
|     | }
|     |
|     | void Stock::loadPricesFromFile(const char *symbol){
|     | for (int i=0;i<1000;i++){
|     | prices.push_back(1000-i);
|     | }
|     | calcReturns();
|     | }
|     |
|     | int main(int argc,char *argv[]){
|     | Stock stock;
|     | stock.loadPricesFromFile("MSFT");
|     |     double m,sd;
|     |     unsigned num;
|     |     std::cout<<"Please enter the number for computation:";
|     |     std::cin>>num;
|     |     m=stock.getMeanReturn(num);
|     |     sd=stock.getReturnStdDev(num);
|     |     std::cout<<m<<"\t"<<sd<<std::endl;
|     |     return 0;
|     | }
|     |
|     | ----------------------------------------------------------------------
|     | _______________________________________________
|     | Rcpp-devel mailing list
|     | Rcpp-devel at lists.r-forge.r-project.org
|     | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
|     --
|     Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
|     _______________________________________________
|     Rcpp-devel mailing list
|     Rcpp-devel at lists.r-forge.r-project.org
|     https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| 
| 
| 
| 
| --
| -Kevin

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list