Namespaces in R Packages

Namespaces in R packages

I have a start on an answer on the devtools wiki: https://r-pkgs.org/Metadata.html

How to automatically load functions into namespace of an R package

You should never use library(package.name) inside your package functions. Instead use package.name::function.name().
You need to re-export the magrittr pipe operator:

1- put magrittr into the DESCRIPTION file (as you did)

2- make an __imports.R file to the packages R/ directory with the following lines:

#' re-export magrittr pipe operator
#'
#' @importFrom magrittr %>%
#' @name %>%
#' @rdname pipe
#' @export
NULL

Or, similarly do as Hadley Wickham says:

#' Pipe operator
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL

I think you can do the same for other imports when necessary. Use @importFrom as much as you can, otherwise re-export it.

Function listing packages' namespaces

Try this

env_list <- function(start_env) {
last <- function(s) { s[[length(s)]]}
result <- list(start_env)
nmspcs <- list()
while(!identical(emptyenv(), last(result))) {
env <- parent.env(last(result))
if(startsWith(environmentName(env) , "package")){
nm <- sub("package:" , "" , environmentName(env))
result <- c(result, list(env))
nmspcs <- c(nmspcs,list(loadNamespace(nm)))
}
else {
result <- c(result, list(env))
}

}
c(result , nmspcs)
}

env_list(globalenv())
#> [[1]]
#> <environment: R_GlobalEnv>
#>
#> [[2]]
#> <environment: package:stats>
#> attr(,"name")
#> [1] "package:stats"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/stats"
#>
#> [[3]]
#> <environment: package:graphics>
#> attr(,"name")
#> [1] "package:graphics"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/graphics"
#>
#> [[4]]
#> <environment: package:grDevices>
#> attr(,"name")
#> [1] "package:grDevices"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/grDevices"
#>
#> [[5]]
#> <environment: package:utils>
#> attr(,"name")
#> [1] "package:utils"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/utils"
#>
#> [[6]]
#> <environment: package:datasets>
#> attr(,"name")
#> [1] "package:datasets"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/datasets"
#>
#> [[7]]
#> <environment: package:methods>
#> attr(,"name")
#> [1] "package:methods"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/methods"
#>
#> [[8]]
#> <environment: 0x7fc8622bd600>
#> attr(,"name")
#> [1] "Autoloads"
#>
#> [[9]]
#> <environment: 0x7fc863090ba8>
#> attr(,"name")
#> [1] "tools:callr"
#>
#> [[10]]
#> <environment: base>
#>
#> [[11]]
#> <environment: R_EmptyEnv>
#>
#> [[12]]
#> <environment: namespace:stats>
#>
#> [[13]]
#> <environment: namespace:graphics>
#>
#> [[14]]
#> <environment: namespace:grDevices>
#>
#> [[15]]
#> <environment: namespace:utils>
#>
#> [[16]]
#> <environment: namespace:datasets>
#>
#> [[17]]
#> <environment: namespace:methods>

Created on 2022-06-04 by the reprex package (v2.0.1)

R package NAMESPACE

You used the tag @import instead of @importFrom. Thus, you asked for importing the package drminstead of the function drm from drc

Btw, you don't need any import tag for drm since you called the function by namespace drc::drm

import NAMESPACE and DESCRIPTION question?

I would suggest reading the Namespaces chapter of Hadley's R Packages book. But in short, the answer is No.

Are imported functions in the NAMESPACE file attached to the R session when the main package is attached?

No, they are not. Imported functions are available for use in the package internals, but not attached to the user's search tree.

Another source for info is, of course, Writing R Extensions. They describe IMPORTS as:

The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached.


As a demonstration, the current version of ggplot2, v 3.2.1, has import(scales) in its NAMESPACE file. In a fresh R session, we can load ggplot2 and observe that the scales package is not attached:

library(ggplot2)
percent(1)
# Error in percent(1) : could not find function "percent"
scales::percent(1)
# [1] "100%"

ggplot2 uses functions from scales internally, and can do so without using the package::function notation. This is what the import(scales) accomplishes. However, unlike with Depends, scales is not attached for the user.



Related Topics



Leave a reply



Submit