14.3 Use Rcpp syntactic sugar - Video Tutorials & Practice Problems
Video duration:
5m
Play a video:
<v Voiceover>Rcpp</v> actually makes it easier to write C plus plus code than doing straight up C plus plus code. They have introduced types such as numeric vectors, character vectors, and matrices that are tightly associated with our objects that way as a seamless integration of C plus plus and R. To illustrate this, let's build a function that takes in two vectors, X and Y and adds them together and returns the added vector. So starting off, if we want this function to be usable in R, we need to export it. So we say slash, slash, and then square brackets Rcpp, colon, colon export. Now we declare the function. The function is going to take in two vectors and return one vector. We know its returning a vector the question is what type. In R, a vector of numbers in generically a numeric vector so we declare we're going to return a numeric vector. And we'll call the function add. It takes two inputs. X and Y, both of which are numeric vectors. We say numeric vector X comma numeric vector Y. We put our body inside the curly braces. In standard C plus plus, if you want to add together two vectors, or what they would probably call an array, you have to loop through into it element by element. So let's recreate that. The first thing we need to know is how long are these arrays. In this example we expect X and Y to be the same. We will declare a variable N that keeps track of the size. So int N, because it's an integer gets X dot size. X is the numeric vector and it has a method size that returns how long it is. And remember to end the statement in a semi colon. Now we will create a result vector to hold all the additions. So we say numeric vector result, and we will say it has length of N. And end in a semi colon. Now we're ready to loop through and add the elements together. So we need to build a fortloop. In C plus plus you say four int I equals zero. Starting at a zero. And remember in C plus plus indeces start at zero, not at one. So we start at zero and we keep going as long as I is less than N because the last element is N minus one. And each iteration add one to I. Inside here we could say the I element of result gets the I element of X plus the I element of Y. After done with the loop you can save return, result. Now that we have this whole function you can source the code using control shift S. It goes ahead and compiles the code. Now we can test it out. We could say add of one through ten comma one through ten. And we get the results we expect. Of course for such a trivial operation you're not going to see much speed improvement using C plus plus but this is just to illustrate how easy it is to do. But things can get even easier. We built this in a very C plus plus like fashion and built a four loop to iterate over the arrays. However Rcpp has syntactic sugar which makes this much easier. Let's see this in action. We come back up here to our file. And we're going to create another new function. Again we need to make sure we export it. We're building a function that will return in numeric vectors so we need to declare a numeric vector. And we will call the function add 2. It takes in two numeric vectors. Now the beautiful thing about Rcpp is the syntactic sugar lets us write code that is a lot like R code. So we could just write return X plus Y. And it will automatically iterate over the vectors and add them together element by element. Let's source this and see what happens. And we see we have an error. That's because when we declared the argument Y we just said it was a numeric. When in fact it has to be a numeric vector. C plus plus is very particular about specifying the correct types of the arguments. And notice how our studio very nicely told us exactly where the error was. That is incredibly useful to have. So let's go fix our error and source it again. So now what we could do add two, one through ten, comma one through ten. And we get the results we expect. Nice, simple, easy.