File: rinside_sample15.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 (31 lines) | stat: -rw-r--r-- 1,047 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
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// Creating a lattice plot from RInside
// cf http://stackoverflow.com/questions/24378223/saving-lattice-plots-with-rinside-and-rcpp/
//
// Copyright (C) 2014  Dirk Eddelbuettel and GPL'ed 

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

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

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

    // evaluate an R expression with curve() 
    // because RInside defaults to interactive=false we use a file
    std::string cmd = "library(lattice); "
        "tmpf <- tempfile('xyplot', fileext='.png'); "  
        "png(tmpf); "
        "print(xyplot(Girth ~ Height | equal.count(Volume), data=trees)); "
        "dev.off();"
        "tmpf";

    // by running parseEval, we get the last assignment back, here the filename
    std::string tmpfile = R.parseEval(cmd);
    std::cout << "Can now use plot in " << tmpfile << std::endl;
    
    exit(0);
}