Rpackage: Rstudio possibly generating bad NAMESPACE file - r

I am making an R package in Rstudio and I selected the option Configure Build Tools > Configure and select Use roxygen to generate NAMESPACE. I wrote my functions in Rcpp and this is what the NAMESPACE looks like when I generate it with roxygen2:
# Generated by roxygen2 (4.1.1): do not edit by hand
export(function1)
export(function2)
export(function3)
export(function4)
Since my functions are written with Rcpp, which I then export, then they will used in R via .Call. However, from writing R extensions we should use useDynLib() in such a case. This is why I think I am getting an error when I try to call function1 and the error is:
Error in .Call("Mypackage_function1", PACKAGE = "Mypackage", var1, :
"Mypackage_function1" not available for .Call() for package "Mypackage"
When I use the default NAMESPACE when I start a project in Rstudio, I have the following in NAMESPACE:
useDynLib(packagename)
exportPattern("^[[:alpha:]]+")
importFrom(Rcpp, evalCpp)
When I use the default NAMESPACE I can call the functions using .Call however I get a warning when I check the package that I am not generating the NAMESPACE using roxygen.
Is there a fix for this? Any advice is appreciated.

This is unrelated to the usage of RStudio: For Roxygen to generate the relevant useDynLib specification, you need to use the #useDynLib tag in a Roxygen doc comment:
#' #useDynLib packagename
You can do this anywhere (where you can use normal Roxygen comments) but it makes sense to put this into the package documentation, rather than the documentation of a specific function. Usually this resides in a file called R/packagename-package.r:
#' My package documentation
#' … bla bla …
#' #name packagename
#' #docType package
#' #useDynLib packagename
NULL

Related

Controlling the order of useDynLib lines in NAMESPACE with roxygen2

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.

Extending a S4 class from another package: reconcilePropertiesAndPrototype error

I am trying to write a subclass for RJDBC::JDBCConnection as I need custom methods to connect the dbplyr package using the approach from dplyr#2941 (originally from here). However, I am not overwriting the *.JDBCConnection methods but want to write methods for a subclass of JDBCConnection.
Therefore, following the advise from this Stack Overflow question, I wrote my package which is essentially this:
### R/testclass.R ####################
#' Test class
#'
#' This extends JDBCConnection in package RJDBC
#'
#' #import RJDBC
#'
setClass("TestConnection", contains = "JDBCConnection")
### DESCRIPTION ######################
Package: test
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself#somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true
The class I want to extend exists, as can be checked with help("JDBCConnection-class", package = "RJDBC").
Calling devtools::document() within this packages returns the following error:
Updating test documentation
Loading test
Error in reconcilePropertiesAndPrototype(name, slots, prototype, superClasses, :
no definition was found for superclass "JDBCConnection" in the specification of class "TestConnection"
I also tried replacing #import with #importClassesFrom as per this SO question, but the result was the same.
How can I get document() to run?
You also need to add
Imports: RJDBC
to your DESCRIPTION file. See, for example, this guide:
If your package is using functions in other packages, you also need to
add some lines to your DESCRIPTION file.
...
Imports is used for packages that are needed by your package but that
don’t need to be loaded with library(). Packages referred to in
#import or #importFrom statements in your Roxygen2 comments, or whose
functions are accessed via the :: operator, should be here.
After that, your package document()'d fine for me.
I succeeded to document the package when I do not rely on roxygen2 to write my DESCRIPTION file but add the packages myself. NAMESPACE is managed by roxygen2.
If I add the Line
Imports: methods, RJDBC
or
Depends: RJDBC
to the DESCRIPTION file manually, devtools::document() runs without error.
[duckmayr found it out in the same time]

Error in .doLoadActions

I have an Rcpp based R package which when checked by devtool::check() produces following warning:
Error in .doLoadActions(where, attach) : error in load action .A.1 for package tarantoolr: (function (ns) : could not find function "loadModule"
What might be the cause of such behavior and what is the best way to fix this issue?
Full build and check log from travis-ci can be viewed here, warnings are located around lines 1212 and 1223.
Package itself is located at Github.
Try running your package using devtools::check(document = FALSE) as I think your NAMESPACE file is being overwritten and made "empty" as you do not use roxygen2 to create the necessary entries
e.g. You need to create a file called tarantoolr-package.R that contains:
#' #importFrom(Rcpp, evalCpp)
#' #useDynLib(tarantoolr)
#' #exportPattern("^[[:alpha:]]+")
#' #details
#' We all live in a yellow submarine..
"_PACKAGE"
Without this file, again, the NAMESPACE file is empty and, thus, the global export of all function via exportPattern("^[[:alpha:]]+") does not occur. Hence, there are no known functions within the environment.

NAMESPACE not generated by roxygen2. Skipped. - Confusion with Hadley book

I am trying to make a package but when I run document() it prints NAMESPACE not generated by roxygen2. Skipped. I am trying to use ggplot2,XML, R6 packages in my functions. I am importing them in the following way:
#' #rdname visualization
#' #param hist_data A table of weather variables with PWS created by hist_data function
#' #param variable A character string of variable name
#' #examples
#' table <- getWeather(city = "San Francisco", state="CA")
#' please <- getConditionsTable(table, "2015-03-09")
#' tab <- hist_data(table, please)
#' head(tab)
#' plot_variable_across_all_pws(hist_data=tab, variable="tempi")
#' #import ggplot2
#' #import XML
#' #import R6
I am wondering what might be causing this error and there is nothing in my Namespace except for exportPattern("^[^\\.]")
Also, I was going over R packages book by Hadley http://r-pkgs.had.co.nz/namespace.html
And confused by the line :
"Note that you can choose to use roxygen2 to generate just NAMESPACE, just man/*.Rd, or both. If you don’t use any namespace related tags, roxygen2 won’t touch NAMESPACE. If you don’t use any documentation related tags, roxygen2 won’t touch man/."
Is this what I'm doing wrong? or missing?
Backup NAMESPACE file, if you need it for future use
Delete the NAMESPACE file
Run devtools::document(), so that roxygen2 will generate new NAMESPACE file in the package source directory
*** make sure you have #export tag in the roxygen2 doc section of the R source file.
I think that devtools tries to avoid overwriting DESCRIPTION and NAMESPACE files that it didn't generate itself (to avoid angst if you have meticulously typed them in yourself, instead of using embedded roxygen comments in your r code). It isn't always possible but it tries.
The main mechanism, as I understand it, is to post a comment at the top of the file when it generates the file, and then later on, look for that comment (there are tricky bits round the edge, for example if you use #includes to create the Collate order in the DESCRIPTION file, but I don't think that is your problem here.)
An example of such a comment is
# Generated by roxygen2 (4.1.0.9001): do not edit by hand
The not generated by ... message is alerting you to this, and letting you know devtools is not going to use roxygen2 to make a NAMESPACE file for you. You possibly have the one you mention without the comment because you used RStudio to start your package, rather than devtools::create() ?
If you just delete the NAMESPACE file, I think devtools::document() would then work for you.
BTW You have a typo in the example code above (you have#' #import ggplo2 instead of #' #import ggplot2)
Thanks to #jsta's solution and I copied the following line # Generated by roxygen2: do not edit by hand at the top of the NAMESPACE file, and then with an empty line.
Then I ran devtools::document() in the console and it automatically replaced the existing NAMESPACE file.
I think that top line is just what roxygen will look for to see if that file is generated by roxygen.
None of the preceding examples worked for me. If I deleted the NAMESPACE file then roxygen complained there was no NAMESPACE. If I deleted and re-created a NAMESPACE file (with `touch, e.g. RStudio: Building package with roxygen2. Not producing NAMESPACE file) then roxygen complained that the file was not created with roxygen.
The solution was to copy a NAMESPACE file from another project that was created with roxygen.
Also one may simply delete everything from NAMESPACE and add leave one line: exportPattern("^[[:alpha:]]+")
If the file NAMESPACE is changed manually, devtools::document() fails to overwrite this file, that is why it leaves as before. When you delete the text from the NAMESPACE file and insert this line, devtools::document() thinks that the file is new and overwrites it.

Object not found after installing and loading package

I've thrown together a bunch of my utility functions into a package. However, I can't seem to access them after I've installed the package. I get errors of the form Error: object 'function_name' not found
Building the package, there are no error messages
Installing the package from source, there are no error messages
Loading the package, there are no error messages (library() nor require())
The package documentation is accessible once loaded
I'm using roxygen2 to generate documentation and the namespace
Any thoughts?
Do you use a NAMESPACE and forgot to add the object in question?
If you're using roxygen2, have you remembered to add #' #export function_name to the functions you want included in the namespace?
If the function name is not exported, you may need to use ":::"
pkgname:::function_name
I believe that CRAN now requires a NAMESPACE, and I think R 2.14.x may even require them.
The same problem for me, you need to change NAMESPACE file. sometime NAMESPACE's content looks like this:
# Generated by roxygen2: do not edit by hand
But you need to change it by hand, like this:
# Generated by roxygen2: do not edit by hand
export("function_name1", "function_name2")
OR use exportPattern("^[^\\.]") to export all function.

Resources