In the case that R code just isn’t fast enough, one available option is to convert your R code into C++. This does take some extra effort the first time around, and you have to know a bit about C++, but it can be a very powerful approach. Converting code to C++ is especially useful if you have to run a large number of loops in R. The more appropriate choice would be to vectorize the R code, but sometimes you have no other option.
The link between R and C++ is a package appropriately named RCpp. It makes your life easy by not having to worry about garbage collection, and there is even code completion for the C++ code!
Note that often only key time intensive functions need to be converted to C++.
Here are the steps to set up Rcpp, and write a basic function in C++ that can be used in R.
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
void sortPrinter(NumericVector x) {
Rcout << "[C++] Before: " << x << '\n';
sort(x.begin(),x.end());
Rcout << "[C++] After: " << x << '\n';
}