First function - again
Recall the the_answer() function from Chapter 1, that always returns 42. Let's rewrite it, this time in a C++ file.
The file's components are:
- An "#include" directive to get access to
Rcppfunctionality. - A
using namespacedeclaration, for convenient typing. - The function definition.
- An Rcpp R comment block, to call the function once the C++ code is compiled.
Latihan ini adalah bagian dari kursus
Optimizing R Code with Rcpp
Petunjuk latihan
- Include the
Rcpp.hheader file. - Declare that you use the Rcpp namespace.
- Make the function return
42. - In the Rcpp R comment block, call the function.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
// Include the Rcpp.h header
#include <___>
// Use the Rcpp namespace
using ___ ___;
// [[Rcpp::export]]
int the_answer() {
// Return 42
return ___;
}
/*** R
# Call the_answer() to check you get the right result
___
*/