When we use R for paper simulation, there are often many loops involved. Generally, the easier way to speed up is tofor loop
Rewrite asapply family
The method performs vectorized calculations, but the speed of this method is limited. When comparing speed with other algorithms during real simulation, unless your own algorithm is excellent, it is still difficult to compete with some algorithms in mature packages.
At this time, there are many ways to speed up again. The common ones are to rewrite the code toFortran
Code, rewriteC++
The code or rewritten asC
Code. becauseRcpp
The existence of the package is rewritten asC++
The code is relatively simple, so we will introduce several commonly used methods to rewrite and call them in R later.
Create C++ Files in RStudio
Everyone has installed it by defaultRStudio
, We all create a C++ file from it. There is an advantage of creating it from here, that it will directly display a piece of sample code, and we just need to make a little change on it.
First we areRStudio
Selected in:document
——New file
——C++ Files
, after creating a new file, the following content is included (here you need to install it in RRcpp
If the package is not installed, click hereRStudio
It will automatically help with installation):
#include <> using namespace Rcpp; // This is a simple example of exporting a C++ function to R. You can // source this function into an R session using the Rcpp::sourceCpp // function (or via the Source button on the editor toolbar). Learn // more about Rcpp at: // // / // / // / // // [[Rcpp::export]] NumericVector timesTwo(NumericVector x) { return x * 2; } // You can include R code blocks in C++ files processed with sourceCpp // (useful for testing and development). The R code will be automatically // run after the compilation. // /*** R timesTwo(42) */
We will understand it one by one according to the English instructions above.
Detailed description
#include <> using namespace Rcpp;
This is the header file and usesRcpp
Namespace. The beginning of a normal C++ code is actually the same, which is actually very similar to the one in our Rlibrary
And in Pythonimport
, With this, we can name some object forms that are only available in R in the code, such as vectors, matrices, data frames, etc., so as to facilitate the mutual transmission of R and some content in C++.
The code in the example file actually names a function in which both the input and output objects are numeric vectors. This function is also very simple: an operation that multiplies the vector by 2.
If we want to use functions defined in C++ files in R, we need to add them above the functions in C++// [[Rcpp::export]]
. It should be noted that a cpp file can define multiple functions in it, but only one function can be passed out.
Then we click on the upper right side of the fileSource
, you can load our function into variable space, or run the following command directly in another R script file:
Rcpp::sourceCpp('Desktop/')
There is another trick in the sample file, which is directly in ourcpp
The following command is added to the file:
/*** R timesTwo(42) */
After adding this sentence, weSource
After this file, you can directly test the function you just defined and seetimesTwo(42)
The running results can be used more during testing.
More content
For some common data types and common functions in Rcpp, you can refer to the blog:R language learning Rcpp basic knowledge comprehensively organized, it is very well said. Here are some contents:
Data Type | describe |
---|---|
int | Integer type |
double | Numerical |
bool | Boolean type (TRUE, FALSE) |
String | Character type |
IntegerVector | Integer vector |
NumericVector | Numeric vector (element type is double) |
ComplexVector | Complex vectors |
LogicalVector | Logical vector; R's logical variable can take three values: TRUE, FALSE, NA; and C++ booleans have only two, true or false. If R's NA is converted to a boolean value in C++, true will be returned. |
CharacterVector | Character vector |
IntegerMatrix | Integer matrix |
NumericMatrix | Numeric matrix (element type is double) |
LogicalMatrix | Logical matrix |
CharacterMatrix | Character matrix |
List | lists; similar to lists in R, whose elements can make any data type |
DataFrame | Data frames; data frames; Inside Rcpp, data frames are actually implemented through lists |
Function | Functional |
Environment | Environment type; can be used to refer to functions in R environment, functions in other R packages, and operate variables in R environment |
RObject | Types that can be identified by R |
Regarding some basic operations and common functions on matrices and data frames:
operate | describe |
---|---|
[n] | For vector type or list, access the nth element. For matrix type, first connect the next column of the matrix to the previous column, forming a long column vector and accessing the nth element. Unlike R, n starts at 0. |
(i,j) | For matrix type, access the (i,j)th element. Unlike R, i and j start from 0. Unlike vectors, parentheses are used here. |
List[“name1”] | Access the element named name1 in the List. |
DataFrame[“name2”] | Access the column named name2 in the DataFrame. |
() | Returns the length of X; suitable for vectors or matrices. If it is a matrix, vectorize first. |
X.push_back(a) | Add a to the end of X; suitable for vectors |
X.push_front(b) | Add b to the beginning of X; suitable for vectors |
() | Returns the number of columns of X |
() | Returns the number of rows of X |
Summarize
At this point, some related content about the basic use of Rcpp is almost introduced.
There are two other websites that are highly recommended:
/
The former contains a lot of basic operation codes, including: vector->vector; vector->matrix; scalar->matrix, etc., and there are example functions and related codes in it, copied to your owncpp
It is easy to get started by running and understanding files.
The latter is equivalent to a search library. You need to use Rcpp to perform matrix operations, parallel calculations, common algorithms and other operations. If you search directly, you can see some corresponding codes written by the master, and at the same time know which functions in the library should be called.
In the following blog, we will introduce:RcppEigen performs matrix operation
The above is the detailed content of the Rcpp entry R code speedup process. For more information about Rcpp entry R code speedup, please pay attention to my other related articles!