RInside Version 0.2.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rinside_callbacks1.cpp
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
2 //
3 // Simple example showing how to capture R's console output using callbacks
4 //
5 // Copyright (C) 2014 Christian Authmann
6 //
7 // GPL'ed
8 
9 #include <RInside.h> // for the embedded R via RInside
10 
11 #if !defined(RINSIDE_CALLBACKS)
12 int main(int argc, char *argv[]) {
13  printf("This example requires RInside to be compiled and installed with RINSIDE_CALLBACKS defined\nSee inst/include/RInsideConfig.h\n");
14  exit(0);
15 }
16 #else
17 
18 
19 class MyCallbacks : public Callbacks {
20  public:
21  // see inst/includes/Callbacks.h for a list of all overrideable methods
22  virtual void WriteConsole( const std::string& line, int type ) {
23  output_buffer << line << std::endl;
24  };
25 
26  virtual bool has_WriteConsole() {
27  return true;
28  };
29 
30  std::string getConsoleOutput() {
31  return output_buffer.str();
32  }
33  private:
34  std::ostringstream output_buffer;
35 };
36 
37 int main(int argc, char *argv[]) {
38  MyCallbacks *callbacks = new MyCallbacks();
39 
40  RInside R(argc, argv); // create an embedded R instance
41  R.set_callbacks( callbacks );
42 
43  R.parseEvalNT("print(\"Hello world\")");
44 
45  std::string result = callbacks->getConsoleOutput();
46  printf("R said:\n%s\n", result.c_str());
47  exit(0);
48 }
49 
50 
51 #endif
int main(int argc, char *argv[])