Calling C functions from R

Asked

Viewed 678 times

13

I need to optimize some functions that are in the C language, but using the packages of genetic algorithms in R.

Is there any way to call C functions in R?

In Matlab I know that there is this possibility through mex. In the R there is something similar?

  • 1

    My Google returns it Using R - Calling C Code 'Hello World! ' which seems to explain well how to link the two languages (notaL I do not know R, nor have I tried any of this article).

  • 2

    Hello Bruno. I edited your question to change the title to make it more consistent with the question. I think so it should attract the right attention. If you don’t like the editing, you can change it yourself or even undo it.

  • Thanks @Luiz Vieira , you would know how to help me?

  • Not at all. I don’t know much about R, so I guess I can’t help. But I looked briefly at the article @pmg posted to you and there seems to be a lot of useful information. You can help yourself to someone who understands more by posting an answer here. If you even figure out how you do it based on this article, please post an answer yourself here (because the idea is to keep knowledge that helps not only you, but other future users who may have the same question). :)

  • 1

    Bruno, unless you have more specific questions about the interface between languages, the answer to your question is currently "Yes: .C, .Call and External", which can be seen on the link posted by pmg and also on book by Hadley Wickham, both in English.

  • If you want code example, see the data.table package

Show 1 more comment

1 answer

7


I think that currently the cleanest and easiest way to integrate C++ codes (or C in your case) is by using the Rcpp package (CRAN link) (package website here).

With Rcpp you can define a function in the section itself using the function cppFunction:

library(Rcpp)
cppFunction('int soma(int x, int y){ 
            int soma = x+y; 
            return soma;
            }')
soma(1,2)
[1] 3

Or you can also set your functions in a file .cpp and use the function sourceCpp. You will specify in the file header that you are using Rcpp #include <Rcpp.h>, using namespace Rcpp; and then define its functions. If you are using Rstudio, when you ask to create a new c++ file it already gives you the minimum file structure.

#include <Rcpp.h>
using namespace Rcpp;

// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp 
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
//   http://www.rcpp.org/
//   http://adv-r.had.co.nz/Rcpp.html
//   http://gallery.rcpp.org/
//

// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
  return x * 2;
}


// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically 
// run after the compilation.
//

/*** R
timesTwo(42)
*/

Well, with this you can already start trying to pass the code to the R. For more details I recommend the hadley’s book for a more general introduction and the vignettes of the package and the dirk’s book for more details about Rcpp.

  • 1

    Perfect @Carlos Cinelli , very good your post! Helped me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.