How do I execute a R command in C, without using R extensions, something like:
int main() {
system("R g<- graph(c(0,1,0,4,0,9,1,7,1,9,2,9,2,3,2,5,3,6,3,9,4,5,4,8,5,8,6,7,6,8,7,8),n=10,dir=FALSE)
plot(g)")
return(0)
}
How do I execute a R command in C, without using R extensions, something like:
int main() {
system("R g<- graph(c(0,1,0,4,0,9,1,7,1,9,2,9,2,3,2,5,3,6,3,9,4,5,4,8,5,8,6,7,6,8,7,8),n=10,dir=FALSE)
plot(g)")
return(0)
}
You can run R using the system() call as you have, but you can't stick R functions on the command line like that.
What you can do is to write the R code to a file and call it with system("R CMD BATCH foo.R")
- minimally:
main(){system("R CMD BATCH test.R");}
Now, by default the output graphics from R CMD BATCH go to a PDF file, so you need to open a graphics window and make the script pause if you want to see it.
Of course your test.R file could be written by your C code before it runs it, and you could keep it in a temporary directory or something.
An alternative for R CMD BATCH
is Rscript
. I think for problems where C++ and R are not tightly integrated, i.e. there are distinct phases where R and C++ are used, nor large volumes or frequency of data exchange, using system calls can be very simple and robust.
An alternative is to run R and C++ alongside each other and exchange info between them. This can be done using the RInside pacakge, which is available on CRAN.