How to call C function from R?

后端 未结 3 2019
梦毁少年i
梦毁少年i 2020-12-09 06:45

How can you use some function written in C from R level using R data. eg. to use function like:

double* addOneToVector(int n, const double* vector) {
    dou         


        
3条回答
  •  一个人的身影
    2020-12-09 07:30

    First off, I wanted to thank both @m0nhawk and @Jan for their immensely useful contributions to this problem.

    I tried both methods on my MacBook: first the one showed m0nhawk which requires creating a function in C (without the main method) and then compiling using R CMD SHLIB and then invoking the function from R using the .C command

    Here's a small C code I wrote (not a pro in C - just learning in bits and pieces)

    Step 1: Write the C Program

    #include 
    int func_test() {
        
        for(int i = 0; i < 5; i++) {
            printf("The value of i is: %d\n", i);
        }
        return 0;
    
    }
    

    Step 2: Compile the program using

    R CMD SHLIB func_test.c
    

    This will produce a func_test.so file

    Step 3: Now write the R Code that invokes this C function from within R Studio

    dyn.load("/users/my_home_dir/xxx/ccode/ac.so")
    .C("func_test")
    

    Step 4: Output:

    .C("func_test") The value of i is: 0 The value of i is: 1 The value of i is: 2 The value of i is: 3 The value of i is: 4 list()

    Then I tried the direct method suggested by Jan - using the RCpp package

    library("Rcpp")
    cppFunction("
    NumericVector addOneToVector(NumericVector vector) {
      int n = vector.size();
    
      for (int i = 0; i < n; ++i)
        vector[i] = vector[i] + 1.0;
    
      return vector;
    }")
    
    # Test code to test the function
    addOneToVector(c(1,2,3))
    

    Both methods worked superbly. I can now start writing functions in C or C++ and use them in R

    Thank you once again!

提交回复
热议问题