Simple C++ Expressions with evalCpp
Unlike R, you don't have access to a console in C++ to experiment iterations of your code.
To ease into C++ gradually, Rcpp provides the evalCpp() function
that takes a simple C++ expression as a string,
compiles it into a proper C++ function, and calls that function.
This is often used to check if a machine is properly configured to
work with Rcpp.
Este exercício faz parte do curso
Optimizing R Code with Rcpp
Instruções do exercício
- Load the
Rcpppackage. - Evaluate
2 + 2as a C++ expression withevalCpp()and also as an R expression. - What are the storage modes of
xandy? - Change the C++ expression
2 + 2so that it returns a double and assign it toz.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Load Rcpp
___
# Evaluate 2 + 2 in C++
x <- evalCpp(___)
# Evaluate 2 + 2 in R
y <- ___
# Storage modes of x and y
___
___
# Change the C++ expression so that it returns a double
z <- evalCpp("2 + 2")