forestgeo / AGBfluxes

Compute flux of biomass at ForestGEO sites.
https://forestgeo.github.io/AGBfluxes/
GNU General Public License v3.0
2 stars 1 forks source link

Import specific funcitons instead of entire pakcages #7

Closed maurolepore closed 5 years ago

maurolepore commented 5 years ago

Unless you are using a lot of funcitons from a package, it's best to import specific functions instead of all of them.

GOOD

#' @importFrom BIOMASS function1 function2 ...

BAD

#' @import BIOMASS
maurolepore commented 5 years ago

@ervanSTRI, RE:

#' @imports BIOMASS  # how do I force the usage of this package (mandatory for this funtion to work)

@imports is not a valid tag. It causes devtools::document() to fail:

image

The valid tag is @import (without the trailing s). Your package right now imports all functions from data.table and all functions from BIOMASS. For clarity, I moved the calls to @import to R/imports.R. This is common practice.

But, it is best to not use @import but use @importFrom instead. See my comment above. Best of all is to avoid these tags completele and instead use the package::funciton() syntax. See http://r-pkgs.had.co.nz/namespace.html.

maurolepore commented 5 years ago

Also, just to clarify, your examples don't need to run devtools::use_package(). That function is not for user but for developers. We already run it. The effect is to write the name of a package in DESCRIPTION, under Imports:.

image

The packages listed under Imports:, will be installed when the user installs __AGBfluxes. Hopefully the chapter on NAMESPACE (link above) will clarify this.

maurolepore commented 5 years ago

The commit 97b421131bfad8dfa179710eaeca8cab312db443 shows how to refer to functions from other packages.

This example uses the package stats. First, I run use_package("stats") to add stats to DESCRIPTION, under Imports. Then I refer to functions as stats::funciton() , for example, stats::approx(). You can avoid using the syntax package::function() if you add the tag @import stats, but that is bad practice because it will contaminate the namespace of your package with all the functions from stats, when you indeed need only a few.

image

maurolepore commented 5 years ago

We are on top of this now.