File: rinside_callbacks1.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 (51 lines) | stat: -rw-r--r-- 1,425 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
51
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
//
// Simple example showing how to capture R's console output using callbacks
//
// Copyright (C) 2014 Christian Authmann
//
// GPL'ed

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

#if !defined(RINSIDE_CALLBACKS)
int main(int argc, char *argv[]) {
    printf("This example requires RInside to be compiled and installed with RINSIDE_CALLBACKS defined\nSee inst/include/RInsideConfig.h\n");
    exit(0);
}
#else


class MyCallbacks : public Callbacks {
    public:
        // see inst/includes/Callbacks.h for a list of all overrideable methods
        virtual void WriteConsole( const std::string& line, int type ) {
            output_buffer << line << std::endl;
        };

        virtual bool has_WriteConsole() {
            return true;
        };

        std::string getConsoleOutput() {
            return output_buffer.str();
        }
    private:
        std::ostringstream output_buffer;
};

int main(int argc, char *argv[]) {
    MyCallbacks *callbacks = new MyCallbacks();

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

    R.parseEvalNT("print(\"Hello world\")");

    std::string result = callbacks->getConsoleOutput();
    printf("R said:\n%s\n", result.c_str());
    exit(0);
}


#endif