|
RInside Version 0.2.6
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*- 00002 // 00003 // Showing off some of the templated conversion due to Rcpp 00004 // 00005 // Copyright (C) 2009 Dirk Eddelbuettel 00006 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00007 00008 #include <RInside.h> // for the embedded R via RInside 00009 00010 int main(int argc, char *argv[]) { 00011 00012 try { 00013 00014 RInside R(argc, argv); // create an embedded R instance 00015 00016 double d1 = 1.234; // scalar double 00017 R["d1"] = d1; // or R.assign(d1, "d1") 00018 00019 std::vector<double> d2; // vector of doubles 00020 d2.push_back(1.23); 00021 d2.push_back(4.56); 00022 R["d2"] = d2; // or R.assign(d2, "d2"); 00023 00024 std::map< std::string, double > d3; // map of doubles 00025 d3["a"] = 7.89; 00026 d3["b"] = 7.07; 00027 R["d3"] = d3; // or R.assign(d3, "d3"); 00028 00029 std::list< double > d4; // list of doubles 00030 d4.push_back(1.11); 00031 d4.push_back(4.44); 00032 R["d4"] = d4; // or R.assign(d4, "d4"); 00033 00034 std::string txt = // now access in R 00035 "cat('\nd1=', d1, '\n'); print(class(d1));" 00036 "cat('\nd2=\n'); print(d2); print(class(d2));" 00037 "cat('\nd3=\n'); print(d3); print(class(d3));" 00038 "cat('\nd4=\n'); print(d4); print(class(d4));"; 00039 R.parseEvalQ(txt); 00040 00041 } catch(std::exception& ex) { 00042 std::cerr << "Exception caught: " << ex.what() << std::endl; 00043 } catch(...) { 00044 std::cerr << "Unknown exception caught" << std::endl; 00045 } 00046 00047 exit(0); 00048 } 00049