[R] calling R functions from Fortran via RInside?

Erin Hodgess erinm.hodgess at gmail.com
Sat Feb 27 05:59:35 CET 2016


Please ignore previous message.  I recompiled, and linked.  Then things
worked.  For what it's worth, here are the steps.



* g++ testC.cpp -c
-I/home/erin/R/x86_64-pc-linux-gnu-library/3.2/RInside/include
-I/home/erin/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include
-I/usr/share/R/include*g++ -o fcr testF.o testC.o -L/usr/lib/R/lib -lR -L
/home/erin/R/x86_64-pc-linux-gnu-library/3.2/RInside/lib -lRInside
-L/home/erin/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/libs/
-Wl,-rpath,/home/erin/R/x86_64-pc-linux-gnu-library/3.2/RInside/lib/
-lRInside
-Wl,-rpath,/home/erin/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/libs/
-lgfortran

 ./fcr
 Fortran Calling RInside
This is C++, Hello World
This is R,  Hello World


On Fri, Feb 26, 2016 at 10:38 PM, Erin Hodgess <erinm.hodgess at gmail.com>
wrote:

> Hello everyone.
>
> Hope you are having a nice weekend.
>
> Is it possible to call R functions from a Fortran program, possibly via
> RInside and Rcpp, please?
>
> I tried the following that I saw on stack overflow.  Here is the cpp:
>
> #include <iostream>
> #include <RInside.h>
>
> void helloR_(int argc, char *argv[], const char *msg);
>
> extern "C" void helloR(int argc, char *argv[], const char *msg) {
>
>     // create an embedded R instance
>     RInside R(argc, argv);
>
>     // convert to string for RInside assignment
>     std::string txt = std::string(msg);
>
>     // C++ Notice
>     std::cout << "This is C++, " << txt << std::endl;
>
>     // Assign string to R object
>     R.assign(txt, "txt");
>
>     // eval the string, give R notice
>     R.parseEvalQ("cat('This is R, ', txt, '\n')");
> }
>
> And here is the Fortran code:
>
>  PROGRAM MAIN
>     USE iso_c_binding
>     IMPLICIT NONE
>     INTEGER :: argc
>     CHARACTER(len=32) :: arg
>     CHARACTER(len=32) :: msg
>
>     INTERFACE
>       SUBROUTINE R_FUN(argc, arg, msg) bind(C, name="helloR")
>         USE iso_c_binding
>         INTEGER(kind=c_int), INTENT(IN) :: argc
>         CHARACTER(kind=c_char), INTENT(IN) :: arg(*)
>         CHARACTER(kind = C_CHAR), INTENT(IN) :: msg(*)
>       END SUBROUTINE R_FUN
>     END INTERFACE
>
>     print *, "Fortran Calling RInside"
>     CALL R_FUN (argc, arg, "Hello World"//C_NULL_CHAR)
>
>   END PROGRAM MAIN
>
> I compiled each, did the linking (according to the stack overflow), but
> the program locked up when I ran it.
>
> This is on an Ubuntu 15.10 laptop, R 3.2.3
>
> Thank you,
> Sincerely,
> Erin
>
>
>
> --
> Erin Hodgess
> Associate Professor
> Department of Mathematical and Statistics
> University of Houston - Downtown
> mailto: erinm.hodgess at gmail.com
>



-- 
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodgess at gmail.com

	[[alternative HTML version deleted]]



More information about the R-help mailing list