Check various common "gotchas" in .onLoad()
, .onAttach()
, .Last.lib()
, and .onDetach()
namespace hooks that will cause R CMD check
issues. See Writing R Extensions for details.
Details
.onLoad()
shouldn't callcat()
,message()
,print()
,writeLines()
,packageStartupMessage()
,require()
,library()
, orinstalled.packages()
..onAttach()
shouldn't callcat()
,message()
,print()
,writeLines()
,library.dynam()
,require()
,library()
, orinstalled.packages()
..Last.lib()
and.onDetach()
shouldn't calllibrary.dynam.unload()
..onLoad()
and.onAttach()
should take two arguments, with names matching^lib
and^pkg
;.Last.lib()
and.onDetach()
should take one argument with name matching^lib
.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = ".onLoad <- function(lib, ...) { }",
linters = package_hooks_linter()
)
#> ::warning file=<text>,line=1,col=12::file=<text>,line=1,col=12,[package_hooks_linter] .onLoad() should take two arguments, with the first starting with 'lib' and the second starting with 'pkg'.
lint(
text = ".onAttach <- function(lib, pkg) { require(foo) }",
linters = package_hooks_linter()
)
#> ::warning file=<text>,line=1,col=35::file=<text>,line=1,col=35,[package_hooks_linter] Don't alter the search() path in .onAttach() by calling require().
lint(
text = ".onDetach <- function(pkg) { }",
linters = package_hooks_linter()
)
#> ::warning file=<text>,line=1,col=14::file=<text>,line=1,col=14,[package_hooks_linter] .onDetach() should take one argument starting with 'lib'.
# okay
lint(
text = ".onLoad <- function(lib, pkg) { }",
linters = package_hooks_linter()
)
lint(
text = '.onAttach <- function(lib, pkg) { loadNamespace("foo") }',
linters = package_hooks_linter()
)
lint(
text = ".onDetach <- function(lib) { }",
linters = package_hooks_linter()
)