I have some functions in R and I re-coded them by Rcpp. Each of those functions has a stand-along .cpp file. One function called
add_inflow(). Previously, I put all cpp functions on my desktop and used
Rcpp::sourceCpp("add_inflow.cpp"). Then, this c++ function could give me an output value by plugging parameters.
Then I want to embed them into my R package called
stormwindmodel, by following Compiled Code, R Packages, Hadley
First, I run
devtools::use_rcpp(), then moved all cpp functions under
src file. Then, I clicked build&reload button and it was succesfully done. At this point, I found the original R functions were in the Environmental Panel but didn't see my cpp functinos. Then I run
load_all, and this time cpp functinos showed up. However, when I run
add_inflow_Cpp() function, Rstudio gave me this output:
Error in .Call("stormwindmodel_add_forward_speed_Cpp", PACKAGE = "stormwindmodel", : "stormwindmodel_add_forward_speed_Cpp" not available for .Call() for package "stormwindmodel"
Did I miss any steps? Any suggestion to me?
If the question was not of good enough quality then please give me feedback, I will edit it as soon as possible.
You are likely missing the
useDynLib(<pkg>) entry in your
NAMESPACE file. If you're using Roxygen and following the examples in the book, you need to include the following content in an R file (the best guess at this point is you missed this step):
#' @useDynLib your-package-name #' @importFrom Rcpp sourceCpp NULL
@useDynLib <pkg> Roxygen directive instructs the
roxygen2 package to include a
useDynLib(<pkg>) in the
NAMESPACE file whenever you re-document the package.
Did you remember to add the associated lines above to an R file in your package in the R folder (e.g. at
R/package-init.R), and re-document the package after adding that? If you've done everything correctly, you should see
useDynLib(<pkg>) added to the
NAMESPACE file, with
<pkg> replaced by the actual name of your package.
It should be noted that
devtools::use_rcpp() does not do this automatically for you -- after running the function, it instructs you that you need to do this step manually:
> devtools::use_rcpp() Adding Rcpp to LinkingTo and Imports * Creating `src/`. * Ignoring generated binary files. Next, include the following roxygen tags somewhere in your package: #' @useDynLib sparklyr #' @importFrom Rcpp sourceCpp NULL Then run document()
Answer author Kevin-ushey
Tickanswer.com is providing the only single recommended solution of the question Rcpp: C++ functions don't work in R package under the categories i.e r , rstudio , rcpp , r-package , . Our team of experts filter the best solution for you.