14.6 Write a package with C++ code - Video Tutorials & Practice Problems
Video duration:
6m
Play a video:
<v Voiceover>Now that</v> we've been refreshed in writing R packages, let's take our simple fastsum3 function and build it into this package. To do that we will create a new file, which we will do by using CTRL Shift N, and we will save this file, it's very important that it's saved in a new folder called SRC. Stands for source. And we will call this file simple functions.cpp. This file is now a C++ file. As before, this will be a very simple function, and it's important to remember to export it. So // in square brackets, Rcpp::export, double square brackets. We declare that it's a double, it's name is fastsum3, and it's argument is a numeric vector named x. And the body of the function is just simply return sum of x semicolon. To use this function properly, it is important that we document it. We will document it the same way we did the regular r function. Except that instead of hash single quote, it is slash slash single quote. And unfortunately before where we could autocomplete the tags, here we have to type them manually, because Rstudio just doesn't have that functionality yet. But it's not too hard, the title is fastsum3, the description is sums a vector. For details we'll say sums a vector using Rcpp sugar, because sum is a type of syntactic sugar. The aliases which is used to direct the critel file to the correct function will be fastsum3. And since we want this exposed to the end user, we will say export fastsum3. It is also very important to tell the namespace to use this package as a dynamic library. So we will say @useDynLib, the name of the package. That's basic cppp. The author, and now the parameter. We need to tell it what to expect as a return. And that's the sum of x. And we should put in some examples. Now we will save this file and document it. Thinking like a regular r programmer, you would expect this to run. But we need to think like a C++ programmer. When doing C++, you need to include certain libraries and declare namespaces. So we'll come up to the top and say #include, then in angle brackets, Rcpp.h. We also need to say that we are using namespace Rcpp with a semicolon. Now the compiler knows where to look for the proper libraries. So we save this, but before we can run it, we need to tell the r package that it's using Rcpp. So we come over here to the description file. And we need to add two lines. First we say that it imports Rcpp, and due to some major revisions, we will say that it requires greater than or equal to 0.11.2, because there's some massive changes in Rcpp. And to help with the linking to dynamic libraries, we say linking to Rcpp. This is because in order to use a functionality of Rcpp, we need to import it and link to it. We'll come back to our code, and we will document again. The compiler is running and doing a lot of optimizations and generating the machine code, then we can see what it's done, updated the namespace, and updated the help files. Let's take a look at the namespace. We are now exporting the base R function, and that C++ function we wrote, and we declared we're using the dynamic library basic Cpp. Let's build and reload using the Rstudio interface. It's restarting R which it occasionally has to do, then it loaded the package. We can test it by saying fastsum3, 1 through 100. And we get the number we wanted. With just that, and remembering to think like a C++ programmer, it's incredibly easy to integrate fast C++ code into your R packages.