File: rinside_sample4.cpp

package info (click to toggle)
r-cran-rinside 0.2.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: cpp: 3,310; ansic: 117; xml: 57; ruby: 34; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,847 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// Another simple example inspired by an r-devel mail by Abhijit Bera
//
// Copyright (C) 2009 Dirk Eddelbuettel 
// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois

#include <RInside.h>                    // for the embedded R via RInside
#include <iomanip>

int main(int argc, char *argv[]) {
    
    try {
        RInside R(argc, argv);          // create an embedded R instance 

        std::string txt = 
            "suppressMessages(library(fPortfolio)); "
            "lppData <- 100 * LPP2005.RET[, 1:6]; "
            "ewSpec <- portfolioSpec(); " 
            "nAssets <- ncol(lppData); ";
        R.parseEvalQ(txt);              // prepare problem
        
        const double dvec[6] = { 0.1, 0.1, 0.1, 0.1, 0.3, 0.3 }; // choose any weights
        const std::vector<double> w(dvec, &dvec[6]);
        R["weightsvec"] = w;            // assign weights
        txt = "setWeights(ewSpec) <- weightsvec";
        R.parseEvalQ(txt);              // evaluate assignment

        txt = 
            "ewPf <- feasiblePortfolio(data=lppData, spec=ewSpec, constraints=\"LongOnly\");"
            "print(ewPf); "
            "vec <- getCovRiskBudgets(ewPf@portfolio)";
        Rcpp::NumericVector   V(     (SEXP) R.parseEval(txt) ); 
        Rcpp::CharacterVector names( (SEXP) R.parseEval("names(vec)"));   

        std::cout << "\n\nAnd now from C++\n\n";
        for (int i=0; i<names.size(); i++) {
            std::cout << std::setw(16) << names[i] << "\t"
                      << std::setw(11) << V[i] << "\n";
        }
        
    } catch(std::exception& ex) {
        std::cerr << "Exception caught: " << ex.what() << std::endl;
    } catch(...) {
        std::cerr << "Unknown exception caught" << std::endl;
    }

    exit(0);
}