[R] calling R functions from Fortran via RInside?

Erin Hodgess erinm.hodgess at gmail.com
Sat Feb 27 05:38:33 CET 2016


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

	[[alternative HTML version deleted]]



More information about the R-help mailing list