File: rinside_sample11.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 (34 lines) | stat: -rw-r--r-- 1,055 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
// Simple example motivated by post from Paul Smith <phhs80@gmail.com>
// to r-help on 06 Mar 2011
//
// Copyright (C) 2011 - 2012  Dirk Eddelbuettel and Romain Francois

#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 = "tmpf <- tempfile('curve'); "  
    "png(tmpf); "
    "curve(x^2, -10, 10, 200); "
    "dev.off();"
    "tmpf";
  // by running parseEval, we get the last assignment back, here the filename
  std::string tmpfile = R.parseEval(cmd);

  std::cout << "Could now use plot in " << tmpfile << std::endl;
  unlink(tmpfile.c_str());		// cleaning up

  // alternatively, by forcing a display we can plot to screen
  cmd = "x11(); curve(x^2, -10, 10, 200); Sys.sleep(30);";
  // parseEvalQ evluates without assignment
  R.parseEvalQ(cmd);
    
  exit(0);
}