14.2 Write a C++ function for R - Video Tutorials & Practice Problems
Video duration:
4m
Play a video:
Video transcript
<v Voiceover>Thanks to the efforts of</v> Dirk Eddelbuettel and Romain Francois, it has become incredibly easy to integrate C++ code into R. The founder of R studio, J.J. Allaire, has also contributed significantly to the Rcpp package, and really made it easier to use. In fact, in our studio, you could come to the new File drop-down, and choose a C++ file. It has a file already setup, if you have a basic function. We're not gonna do that now because we want to do it from scratch, that's just how easy it is. So for us, we will start a new R Script because that gives us a blank page. Very first thing we will do is save this file, and I will save it in here, and I will call it SimpleFunctions.cpp. The .cpp extension is for C++. R Studio's gonna ask, am I sure I wanna change from an R file to a C++ file? And yes, I do. And now, the R Studio interface changed a little bit because it's now for working with C++ code. Learning C++ is an entire LiveLesson on its own, so we're not gonna get into that, we're just gonna go over the basics so that you can use it with R. To get started using Rcpp and R, you first need to include the Rcpp Library. In C++, that is done with hash include, then in angle brackets, Rcpp.h, and close off the angle brackets. Notice R Studio is color coding it for us. By loading in Rcpp.h, we now have access to all the functionality built into Rcpp. To access functions and individual C++ libraries, you typically prepend the function name, with the name of the library. That can get laborious though, so we are going to take a shortcut, and say, using namespace Rcpp, that will prevent us from having to type in Rcpp:: the name of the function, every time. And it is important in C++, you end your lines with a semicolon. To start things off simply, let's build a function and all it does is, takes in a number, and returns two times that number. Declaring a function in C++ is different than doing it in R. First thing you do is you declare the type of variable that will be returned. In our case, that is an integer. Then you give the name of the function, which we will call Times Two. Then in parentheses, you specify the arguments, including their type. The only input to this function will be an integer that we call X. You then include, in the body of this function, which will be enclosed in curly braces, what you want to do. And for us, we could simply just say, return X times two, and finish it with a semicolon. Now, with the magic of Rcpp, we want to expose this to R, so we're gonna go up here, right above the function, and put in a slash slash, which is normally a comment in C++, then in double square brackets, put Rcpp::export, and close off the square brackets. This tells the parser to make this function available to the R session. So let's go ahead and save this file. If you wanted to do this from the Command Line, you could run a function called Source CPP and plug in this file name, but it is much easier if you just use your keyboard to hit Control Shift S to source it, or on a Mac, Command Shift S. So let's go ahead and source this file and see what happens. You can see, it runs the function source CPP for you, automatically, and it is all done. Now, if we type in LS in the Command Line, we should see we have a function called Times Two, and we can call it by running Times Two and put in any number, say five, and we get back 10, because five times two results in 10. This was an example of a simple C++ function that didn't take a lot of effort to write.