File: rinside_sample6.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 (49 lines) | stat: -rw-r--r-- 1,421 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
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
//
// Showing off some of the templated conversion due to Rcpp
//
// Copyright (C) 2009        Dirk Eddelbuettel 
// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois

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

int main(int argc, char *argv[]) {

    try {

        RInside R(argc, argv);          // create an embedded R instance 

	double d1 = 1.234;		// scalar double
	R["d1"] = d1;			// or R.assign(d1, "d1")

	std::vector<double> d2;		// vector of doubles
	d2.push_back(1.23);
	d2.push_back(4.56);
	R["d2"] = d2;			// or R.assign(d2, "d2");

	std::map< std::string, double > d3; // map of doubles
	d3["a"] = 7.89;
	d3["b"] = 7.07;
	R["d3"] = d3;			// or R.assign(d3, "d3");

	std::list< double > d4; 	// list of doubles
	d4.push_back(1.11);
	d4.push_back(4.44);
	R["d4"] = d4;			// or R.assign(d4, "d4");

	std::string txt = 		// now access in R
	    "cat('\nd1=', d1, '\n'); print(class(d1));"
	    "cat('\nd2=\n'); print(d2); print(class(d2));"
	    "cat('\nd3=\n'); print(d3); print(class(d3));"
	    "cat('\nd4=\n'); print(d4); print(class(d4));";
        R.parseEvalQ(txt);
        
    } catch(std::exception& ex) {
        std::cerr << "Exception caught: " << ex.what() << std::endl;
    } catch(...) {
        std::cerr << "Unknown exception caught" << std::endl;
    }

    exit(0);
}