ImportFrom function in NAMESPACE is removed upon document() - r

I am receiving an error [could not find function "str_trim"] when I run check() on an R package I am developing. I have since added two things:
1) In DESCRIPTION
Imports: stringr
2) In NAMESPACE
importFrom(stringr,str_trim)
However, when I then run install() and document(), then the line in NAMESPACE is removed. Then, when I again run check(), I receive the original error.
Why is this line being removed? Should I try a different approach, and if so, what kind of approach? Thank you!

It seems you are doing package development "the Hadley way".
Hadley wants you to use roxygen (i.e. the roxygen2 package but that should be automatic if you use his 'devtools').
Then, the roxygen "magic" is used to auto-write your NAMESPACE file... and so also destroys things you've put there.
You must add #importFrom .... statements to your R/*.R files if you want to use roxygen.
I agree with many things Hadley advocates; the wholesale use of 'roxygen' is not among them however.
I want good, carefully maintained help files with \link{}s, \eqn{}, etc etc
-> I edit my man/*.Rd files and I manually build NAMESPACE (so it ends up looking well organized, I can also add comments there, and I can even use
if(getRVersion() >= "3.2.0") { ...... } in the NAMESPACE file, something that's not easily possible with roxygen.

Related

How to declare a dependency on an R package from which you only use S3/S4 methods, but no exports?

Currently I have in my package DESCRIPTION, a dependency on dbplyr:
Imports:
dbplyr,
dplyr
dbplyr is useful almost solely because of the S3 methods it defines: https://github.com/tidyverse/dbplyr/blob/main/NAMESPACE. The actual functions you call to use dbplyr are almost entirely from dplyr.
By putting dbplyr in my Imports, it should automatically get loaded, but not attached, which should be enough to register its S3 methods: https://r-pkgs.org/dependencies-mindset-background.html#sec-dependencies-attach-vs-load.
This seems to work fine, but whenever I R CMD check, it tells me:
N checking dependencies in R code (10.8s)
Namespace in Imports field not imported from: ‘dbplyr’
All declared Imports should be used.
Firstly, why does R CMD check even check this, considering that it often makes sense to load packages without importing them. Secondly, how am I supposed to satisfy R CMD check without loading things into my namespace that I don't want or need?
I am pretty sure two of your assumptions are false.
First, putting Imports: dbplyr into your DESCRIPTION file won't load it, so its methods won't be loaded from that alone. Basically the Imports field in the DESCRIPTION file just guarantees that dbplyr is available to be loaded when requested. If you import something via the NAMESPACE file, that will cause it to be loaded. If you evaluate dbplyr::something that will cause it to be loaded. Executing loadNamespace("dbplyr") is another way, and there are a few others. You may also load some other package that loads it.
Second, I think you have misinterpreted the error message. It isn't saying that you loaded it without importing it (though it would complain about that too), it is saying that it can't detect any use of it in your package, so maybe it shouldn't be a requirement for installing your package.
Unfortunately, the code to detect uses is fallible, so it sometimes misses uses. Examples I've heard about are:
if the package is only used in the default value for a function argument. This has been fixed in R-devel.
if the package is only used during the build to construct some object, e.g. code like someclass <- R6::R6Class( ... ) needs R6, but the check code won't see it because it looks at someclass, not at the source code that created it.
if the use of the package is hidden by specifying the name of the package in a character variable.
if the need for the package is indirect, e.g. you need to use ggplot2::geom_hex. That needs the hexbin package, but ggplot2 only declares it as "Suggested".
These examples come from this discussion: https://github.com/hadley/r-pkgs/issues/828#issuecomment-1421353457 .
The recommended workaround there is to create an object that refers to the imported package explicitly, e.g. putting the line
dummy_r6 <- function() R6::R6Class
into your package is enough to suppress the note without actually loading R6. (It will be loaded if you ever call this function.)
However, your requirement is stronger: you do need to make sure dbplyr is loaded if you want its methods to be used. I'd put something in your .onLoad() function that triggers the load. For example,
.onLoad <- function(lib, pkg) {
# Make sure the dbplyr methods are loaded
loadNamespace("dbplyr")
}
EDITED TO ADD: As pointed out in the comments, there's a bug in the check code that means it won't detect this as being a use of dbplyr. You really need to do both things, e.g.
.onLoad <- function(lib, pkg) {
# Make sure the dbplyr methods are loaded
loadNamespace("dbplyr")
# Work around bug in code checking in R 4.2.2 for use of packages
dummy <- function() dbplyr::across_apply_fns
}
The function used in the dummy construction is arbitrary; it probably doesn't even need to exist, but I chose one that does.

Undo `usethis::use_XYZ()`

I'm developing a package in RStudio with usethis, trying to make use of best practices. Previously, I had run usethis::use_tidy_eval(). Now, I'm using data.table, and set this up by running usethis::use_data_table(). I get a warning,
Warning message:
replacing previous import ‘data.table:::=’ by ‘rlang:::=’ when loading ‘breakdown’
because the NAMESPACE contains the the two lines:
importFrom(rlang,":=")
importFrom(data.table,":=")
It turns out I no longer need usethis::use_tidy_eval(), so I'd like to revert it and in doing so get rid of the warning.
How can I undo whatever usethis helper functions do? Must I edit the NAMESPACE myself? How do I know what else was modified by usethis::use_tidy_eval()? What about undoing usethis::use_pipe()?
Unless you made a Git commit before and after running that code, there's probably not an extremely easy way. The two options I'd consider would be:
Read the source code of the function. This can require some hopping around to find definitions of helper functions, but use_tidy_eval looks like it:
adds roxygen to Suggests in DESCRIPTION
adds rlang to Imports in DESCRIPTION
adds the template R file tidy-eval.R
asks you to run document() which is what actually updates the NAMESPACE. You can find the lines added by looking for the importFrom roxygen tags in the template file.
To undo this, you should just be able to delete all of the above. However, you need to be a bit careful - e.g. if you import functions from rlang outside of tidy-eval.R, removing it from DESCRIPTION might prevent installation. Hopefully any such issues would be revealed by devtools::check() if they do happen.
The other option would be to get an older version of your package, run use_tidy_eval() and document() and then compare the changes. That will be more comprehensive and might catch things I missed above, but the same caveats about not being able to necessarily just reverse everything still apply.
Same strategy for use_pipe().
Sidenote: there are probably ways to adequately qualify different uses of := so that both can coexist in your package, in case that would be preferable.

Read a package NAMESPACE file

I am looking for a fast R solution to read a package NAMESPACE file. The solution should contain already preprocessed (and aggregated) records and separated imports and exports.
Unfortunately I can’t use getNamespaceExports("dplyr")/getNamespaceImports("dplyr") as they need the package to be loaded to the R session, which is too slow.
I need a solution which simply process a text from the NAMESPACE file. Any solutions using external packages as well as partial solutions would still be welcome.
The raw data we could grabbed with a call like readLines("https://raw.githubusercontent.com/cran/dplyr/master/NAMESPACE"). roxygen2 generated files are formatted properly, but this will be not true for all manually generated files.
EDIT:
Thanks to Konrad R. answer I could develop such functionality in my new CRAN package - pacs. I recommended to check pacs::pac_namespace function. There is even one function which goes one step further, comparing NAMESPACE files between different package versions pacs::pac_comapre_namespace.
The function is included in R as base::parseNamespaceFile. Unfortunately the function does not directly take a path as an argument. Instead it constructs the path from a package name and the library location. However, armed with this knowledge you should be able to call it; e.g.:
parseNamespaceFile('dplyr', .libPaths()[1L])
EDIT
Somebody has to remember that the whole packages imports (like import(rlang)) have to be still invoked with the same function and the exports for them extracted. Two core elements are using parse on NAMESPACE code and then using the recursive extract function parseDirective.

Documenting Rcpp module exposed methods with roxygen2

I have an Rcpp module in my package which, as well as exposing the class, exposes a number of methods. Is it possible to document the methods (on the C++ side) using roxygen2? My module looks like this:
RCPP_MODULE(BayesFst) {
using namespace Rcpp;
class_<BayesFst>( "BayesFst")
.default_constructor("Standard constructor")
.method("printData", &BayesFst::printData)
.method("printCounts", &BayesFst::printCounts)
.method("printInitialPvals", &BayesFst::printInitialPvals)
.method("printFstSummary", &BayesFst::printFstSummary)
.method("run", &BayesFst::run)
.method("setData", &BayesFst::setData)
.method("setPriorParameters", &BayesFst::setPriorParameters)
.method("setRunParameters", &BayesFst::setRunParameters)
.method("ldiriTest", &BayesFst::ldiriTest)
.property("interaction", &BayesFst::getInteraction, &BayesFst::setInteraction)
;
}
I would like to document all of these methods ideally. A simple idea is to hide the class behind wrapper functions and then call the methods from within R wrapper functions, but this feels kind of inelegant to me.
I have tried adding the roxygen comment lines to the functions, but because they don't get exported in the same way the documentation doesn't seem to get picked up.
You will not be able to rely on Rcpp to carry the roxygen documentation over the way compileAttributes() would ... as you are not invoking compileAttributes().
Feel free to construct (and test and then contribute :) a new helper function; otherwise I would just use roxygen in an R file and proceed from there.
Edit: I also filed an issue so that we don't forget about 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