V. van Hees
V. van Hees

Reputation: 81

R package with Rcpp builds, but when calling function: Error in .Call

I am trying to add C++ code to my R package. My package code is on Github: https://github.com/wadpac/GGIR. I have been reading the Writting R Extensions documentation, but no succes so far:

install.packages('Rcpp')
Rcpp.package.skeleton("GGIR", cpp_files = c("src/numUnpack.cpp","src/resample.cpp")
package_native_routine_registration_skeleton(".")
R CMD build .
R CMD check --no-manual ../GGIR_1.5-4.tar.gz
Status: OK
install.packages("~/GGIR/GGIR_1.5-4.tar.gz",dependencies=TRUE
* DONE (GGIR)

Untill here, everything seems to have gone well because the package builds and I have a source file I can install. However, when I try to use the R function that relies on the c++ code I get:

P = g.cwaread("/media/windows-share/testdata/testfile.cwa",start=1,end=10) Error in .Call("GGIR_numUnpack", PACKAGE = "GGIR", pack) : "GGIR_numUnpack" not available for .Call() for package "GGIR"

The C++ code works on my machine when using for example Rcpp::sourceCpp('src/numUnpack.cpp') directly.

I elaborate on my investigations so far at: https://github.com/wadpac/GGIR/issues/6

Session info:

sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=nl_NL.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=nl_NL.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=nl_NL.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=nl_NL.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] GGIR_1.5-4   Rcpp_0.12.10

loaded via a namespace (and not attached):
[1] compiler_3.4.0    tools_3.4.0       data.table_1.10.4

Upvotes: 2

Views: 1593

Answers (1)

David J. Bosak
David J. Bosak

Reputation: 1624

I just ran into this, and the above comments didn't seem to help much. What worked was making sure the useDynLib(<package>) statement is added to the NAMESPACE file. (Note that <package> should be the name of your package.)

Of course, if you are using roxygen2, then you can't edit the NAMESPACE file by hand. So what you do is add the useDynLib statement to the usethis namespace file. Like this:

## usethis namespace: start
#' @importFrom Rcpp sourceCpp
#' @useDynLib <package>
## usethis namespace: end
NULL

Then regenerate your documentation and it will show up in the NAMESPACE like this:

import(readr)
import(readxl)
import(tibble)
import(tools)
importFrom(Rcpp,sourceCpp)
useDynLib(<package>)

If the useDynLib(<package>) statement is not added to the NAMESPACE, your code won't be able to find your Rcpp functions.

Upvotes: 2

Related Questions