I am working on a R package that uses an external 3rd party dll to load data. I have written wrapper functions to that external dll that I can call with .C()
Assume that my package is called mypackage
and the external is called xternal.dll
. It seems that to load the mypackage.dll
that is generated during compilation it is necessary that external.dll
is loaded first. I am using roxygen2
to manage the NAMESPACE
file, and I have used the #' @useDynLib
tags. Unfortunately when roxygen2
writes the NAMESPACE
file it adds the useDynLib
calls in the lexical order of the shared object being called like A-Z, a-z.
Is there a way to control the order of the useDynLib
in the namespace by roxygen2
?
So far I have found the follwing solutions and neither of them seems to be particularly compelling:
Renaming my package to be lexically ordered after the external dll.
Managing the NAMESPACE file manually
.
Example: The function foo.R:
#' @export
#' @useDynLib xternal
#' @useDynLib mypackage
foo <- function(){
return(FALSE)
}
results in the NAMESPACE
after calling devtools::document()
:
# Generated by roxygen2: do not edit by hand
export(foo)
useDynLib(mypackage)
useDynLib(xternal)
The package would fail to load, however if I manually swap the two useDynLib
lines the package installs and works fine.
After a very helpful hint received on GitHub:
The solution is to use the @rawNamespace
tag, that writes a verbatim line into the NAMESPACE
file:
foo.R:
#' @export
#' @rawNamespace useDynLib(xternal); useDynLib(mypackage)
foo <- function(){
return(FALSE)
}
results in a NAMESPACE file:
# Generated by roxygen2: do not edit by hand
export(foo)
useDynLib(xternal); useDynLib(mypackage)
and the shared objects are loaded in the correct order.