1

is it possible to plot functions from a C program using R? Is there a R C API available for plotting? Of course, it is possible to generate a R script from the C code and then call execute this generated script using the system command but I want to call R functions for plotting directly from the C code?

Thanks in advance, Jonas

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jonas
  • 2,974
  • 4
  • 24
  • 23
  • I think you might have more luck doing to the other way around and export your C function calculations into R so that R is the driver and your C code provides the values to be plotted. – David Heffernan Mar 23 '11 at 19:08
  • Not worth it, IMHO. Export your data from C into a CSV file or similar and import it into R. Or alternatively, just use gnuplot from C. – Chinmay Kanchi Mar 23 '11 at 19:40

3 Answers3

2

You might like Rcpp

http://dirk.eddelbuettel.com/code/rcpp.html

EDIT: This might be useful as well: Call R plots from c++ using RInside/ Rcpp

Community
  • 1
  • 1
  • Hmmm, and R has no C API? AFAIK R is developed in C and Fortran. Any links and code samples are welcome ;) – Jonas Mar 23 '11 at 19:23
  • @Jonas Why do you want to get R to do your plotting? Surely there's easier ways of plotting things. And what's stopping you doing it all in R? – David Heffernan Mar 23 '11 at 19:29
  • Jonas, R *does* have a C API which is clearly documented in the *Writing R Extensions* manual. But what may be a three-liner with Rcpp or RInside can become a ten-or-more-liner with the C API. Some of us like the abstractions that Rcpp offers---but you are free to ignore them. – Dirk Eddelbuettel Mar 23 '11 at 19:55
  • Hi everyone, first of all: thanks for your replies! What I am looking for is a small C example using R directly without generating a R script and executing it via the system command. In pseudo code I want something like this: void plotSinAsPNG() { /* init R */ /* create plot */ /* store plot as PNG image */ return; } I don't want R to be visible / noticeable to the user. The user should just run the C program and have the plot as output in the form of an PNG image. I also don't want to use Any code snippets – Jonas Mar 25 '11 at 05:23
1

As OneWhoisUnnamed already said, Rcpp and RInside may be of help if you want to take advantage of C++ rather than C.

Another nice example, completed after the post mentioned in the other answer, is this simple example of embedding R inside a simple Qt application: Qt does the widgets, R does the plotting---see this question for more, including a screenshot. This is now in the RInside SVN sources but not yet on CRAN.

Community
  • 1
  • 1
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
-1

You can interface with functions written in C using the .C function. One example of this is http://madison.byu.edu/bayes/gsnow.txt

The inline package may also be of help.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • Yes, `.C()` is the oldest useable interface, and `.Call()` improves upon it and allows whole R objects to be passed. That is where Rcpp may be of help. But plain C is also an option, of course. – Dirk Eddelbuettel Mar 23 '11 at 20:36
  • He asks for the opposite: Call R from C. Specifically, to be able to use R as a plotting engine from C. – csl May 16 '13 at 12:41