Description Usage Arguments Details Value Note See Also Examples
sourceCpp
parses the specified C++ file or source code and looks for functions marked with the Rcpp::export
attribute. A shared library is then built and its exported functions are made available as R functions in the specified environment.
1 2 3 |
file |
A character string giving the path name of a file |
code |
A character string with source code. If supplied, the code is taken from this string instead of a file. |
env |
Environment where the R functions should be made available. |
rebuild |
Force a rebuild of the shared library. |
showOutput |
|
verbose |
|
If the code
parameter is provided then the file
parameter is ignored.
Functions exported using sourceCpp
must meet several conditions, including being defined in the global namespace and having return types that are compatible with Rcpp::wrap
and parameter types that are compatible with Rcpp::as
. See the Rcpp::export
documentation for more details.
If the source file has compilation dependencies on other packages (e.g. Matrix, RcppArmadillo) then an Rcpp::depends
attribute should be provided naming these dependencies.
It's possible to embed chunks of R code within a C++ source file by including the R code within a block comment with the prefix of /*** R
. For example:
1 2 3 4 5 6 | /*** R
# Call the fibonacci function defined in C++
fibonacci(10)
*/
|
Multiple R code chunks can be included in a C++ file. All R code is sourced after the C++ functions within the file have been defined.
Returns (invisibly) a character vector with the names of the R functions that were sourced into the specified environment.
The sourceCpp
function will not rebuild the shared library if the source file has not changed since the last compilation.
The sourceCpp
function is designed for compiling a standalone source file whose only dependencies are R packages. If you are compiling more than one source file or have external dependencies then you should create an R package rather than using sourceCpp
. Note that the Rcpp::export
attribute can also be used within packages via the compileAttributes
function.
If you are sourcing a C++ file from within the src
directory of a package then the package's LinkingTo
dependencies, inst/include
, and src
directories are automatically included in the compilation.
If no Rcpp::export
attributes are found within the source file then a warning is printed to the console. You can disable this warning by setting the rcpp.warnNoExports
option to FALSE
.
Rcpp::export
, Rcpp::depends
, cppFunction
, evalCpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.