Using Rcpp in Xcode

Introduction In this tutorial, we’ll look at how you can configure Xcode to work with and debug Rcpp objects and methods. Why? I’m interested in building fast machine learning models with C++ and exposing those models to R, much like ranger and xgboost. Rcpp makes it easy to pass objects between R and C++. However, the only way to debug Rcpp code within RStudio that I’m aware of is to litter the code with print statements.

Sorting A Vector In C++

Here we’ll look at one of the most common data manipulation tasks - sorting. Specifically, we’ll be using the STL’s sort method. Basics To get the ball rolling, let’s see how to sort a vector of integers. #include <iostream> // std::cout#include <algorithm> // std::sort#include <vector> // std::vector int main() { std::vector<int> foo = {5, 3, 10, 1, 7}; // Make values std::sort(foo.begin(), foo.end()); // Sort for(int &x : foo) std::cout << x << " "; // Print } 1 3 5 7 10

Making A Binary Search Tree in C++

This article is about implementing a Binary Search Tree (BST) in C++. I’ll skip the part about defining what a BST is since that’s a horse that’s been beaten many times. I am new to C++, so my implementation may have flaws. I welcome and encourage critique from other programmers :) Draft 1 We start by implementing a TreeNode struct. struct TreeNode { // member vars int data; TreeNode* left; TreeNode* right; // constructor TreeNode(int data): data(data), left(nullptr), right(nullptr) {} }; Notes:

Random Numbers In C++

Want to make some random numbers in C++? Buckle up. default_random_engine We start by creating a default_random_engine from C++’s <random> library. #include <random> int main() { std::default_random_engine myRandomEngine; } Now we can use myRandomEngine to generate some random numbers. #include <iostream>#include <random> int main() { std::default_random_engine myRandomEngine; std::cout << myRandomEngine() << " " << std::endl; std::cout << myRandomEngine() << " " << std::endl; std::cout << myRandomEngine() << " " << std::endl; } For me, this produces the output

Reading And Writing CSV Files With C++

As a data scientist, reading and writing data from/to CSV is one of the most common tasks I do on the daily. R, my language of choice, makes this easy with read.csv() and write.csv() (although I tend to use fread() and fwrite() from the data.table package). Hot Take. C++ is not R. As far as I know, there is no CSV reader/writer built into the C++ STL. That’s not a knock against C++; it’s just a lower level language.

Exposing a C++ Student Class With Rcpp

Intro The purpose of this tutorial is to document my notes as I experiment with exposing a simple C++ Student class to R using Rcpp. It’s more or less part two of a series of articles on Rcpp. (See part one here). Setup In part one of my tutorial on using Rcpp, we discussed how to use Rcpp’s sourceCpp() function to compile a C++ file with simple C++ functions. My next question was “How can I use Rcpp to expose more complicated C++ code that uses multiple files including header files?