Banging my head on the keyboard: Starting C++ in R studio

If you read my last post, you know that I’ve begrudgingly decided to use C++ to perform MCMC which is supposed to be much faster than R, particularly with regards to using for loops. Sadly, the resources to this end are rather limited. There are resources related to introductory programming in C++ and resources for making packages in R (which can source c files) but the intro programming tutorials tend to use the windows command prompt to execute c files. Instead we want to be able to create functions in c++ and source them into R for usage, especially because later we will want to create a package with this code that we can publish on CRAN. We will do this in this first post while setting up our folders for making a package later on.

Getting Started: First install the package Rcpp and load it into the R session with library(Rcpp). I am assuming here that you are using R studio (please do)

Create a package that contains c++ file we can play with with the command

> Rcpp.package.skeleton(“pleasework”)

Which produces the following in your home window

pic1

Now we have our nice pleasework file!!! (It took me a while getting this working, so pleasework was a necessary file name). Click inside of it and we can see our package contents:

pic2

I’ll discuss these folders later on when I talk about building a package, but in brief-R contains R files for your package, man contains the manual for your package that goes online and src contains all your c++ files that are called in the program. Let’s click into it so we can use our c++ program.

pic3

The RcppExports.cpp file is necessary for using c++ in R packages so DO NOT EDIT IT. The rcpp_hello_world.cpp file contains an example of using c++. Click into it, the file should look like this:

pic4

Brief C++ coding lesson:

I’ll briefly discuss what the code above is doing as an intro to some c++ for R users.

#include <Rcpp.h> works like the library() function in R, it calls the c++ library Rcpp.h to use, which is needed to use c++ in R.

using namespace Rcpp; is needed to use c++ in R, simple examples from the command prompt will typically use

using namespace std; which is the standard namespace.

// [[Rcpp::export]] is used before defining any function that we want to use in R from c++.

We will have to use this line before ALL desired functions or else we can’t use them in R.

pic4

List rcpp_hello_world(){   } creates a function called rcpp_hello_world that returns a list. The function arguments are passed within {   } just like in R/Rstudio. Inside the function, we define two vectors x and y. x is specified as a character vector with entries foo and bar with the command:

CharacterVector x = CharacterVector::create( “foo”, “bar” ) ;

Likewise, y is specified as a vector with entries 0.0 and 1.0 with this command.

NumericVector y = NumericVector::create( 0.0, 1.0 ) ;

Next we create a list z that contains the two vectors x and y. These lists work similarly to R.
List z = List::create( x, y ) ;

and finally, we tell c++ that we want to return the list z.

return z ;

Save the file (if you made any edits) and now we are going to source the function rcpp_hello_world into R for usage using the function sourceCpp.

Here is what the output of this looks like when we source it into R (Note: The path of your file will ABSOLUTELY BE DIFFERENT FROM MINE! Right click on the program rcpp_hello_world.cpp within the src folder and click properties. Next copy and paste your path into R and change the \ to /.)

sourceCpp(“D:/Users/Andrew/Documents/pleasework/src/rcpp_hello_world.cpp”)

rcpp_hello_world()

 

[[1]]

[1] “foo” “bar”

 

[[2]]

[1] 0 1

So it returned the list above of x,y!!

Ok now let’s make one our own. Let’s make a function that adds 100 to whatever number the user specifies in the SAME file as the hello world function with the following code:

pic5

Notes on code

So we used the line

// [[Rcpp::export]] 

again above our code to tell R that we want to export the function main.

int main(int number2) {} tells c++ that we are making a function called main with an integer input called number2 and it returns an integer value. int number2 tells c++ that number2 is an integer and int main tells c++ to return a integer value.

Now the line

int number2 = 100; defines a variable called number2 that is set to an integer value of 100.

int sum;  creates an integer variable called sum, but doesn’t set it to a value.

sum = number1 + number2; adds the two variables number1 and number2.

Finally

return sum; returns the value of 100 + the user specified value.

This is the output and code in R

sourceCpp(“D:/Users/Andrew/Documents/pleasework/src/rcpp_hello_world.cpp”)

main(2)

[1] 102

main(2.2)

[1] 102

Notice that we return 102 for both the inputs 2 and 2.2. This is because the int argument before the function main in the c++ code indicates that the function should return a integer value (which takes less memory to allocate than real numbers with up to 15 decimal places). For some reason, we can only define the function main() to return integer values so instead, we will rename the function adding() which we will specify as a non-integer with the double indication and the following code.

pic6

 

Notice that we specify that sum and number2 are both decimal valued numbers with the commands

double number2

double sum;

and we specify that the function called adding() will return a real number (up to 15 digits) with the command

double adding(){}

c++ is very particular about memory allocation (which is one reason it’s faster) so we have to tell c++ whether it’s a real or integer valued number. Likewise we do this which character variables and lists.

number1 is still specified as an integer and set equal to 100. We could specify number1 as a decimal valued number using double but this will cause the computation to be slower since we are allocating more memory to a number than we need.

This produces the following output after we save this file and resource it:

>sourceCpp(“D:/Users/Andrew/Documents/pleasework/src/rcpp_hello_world.cpp”)>

> adding(2)

 

[1] 102

> adding(2.277777)

 

[1] 102.2778

 

It works with negative numbers too!!

> adding(-99.99)

[1] 0.01

> adding(-1000.111)

[1] -900.111

 

Final Tips:

One thing I noticed was that each time I made changes in my c++ code and saved it, the first time that I used the sourceCpp command, it failed. But after I ran it a second time it worked fine. This could be that it takes a second for R to recognize the changes in the c++ coding, if it worked correctly you should see the name of your functions here!

functions

Happy coding!

-Andrew G. Chapple

 

 

 

Advertisements

3 thoughts on “Banging my head on the keyboard: Starting C++ in R studio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s