The exercises below have been written assuming that you have never programmed in C/C++ before. If you have previous experience, feel free to alter the questions to suit your background.
Rcpp
package
r
library("Rcpp")
and run the following code
r
cppFunction("
double add_c(double x, double y) {
double value = x + y;
return value;
}
")
add_c
and make sure it works. What happens if you pass it a vector
or a character to the function?src
- this is a standard directory name for computer code.
It's short for source
. This is where you should keep your C++ code.add.cpp
and move the add_c
function (with necessary headers)
to the file.add_c
function.subtract_c
.sum
, mean
, sd
or var
.microbenchmark
, compare your C version to your R function.sd
as a baseline.Hint: There's a chapter on RCpp Efficient R programming
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.