In R, what does loaded via a namespace (and not attached) mean?
It means the package (In this case R) can access the package functions/objects, but the user can not without explicitly loading the tools package where as stats, graphics, etc. are loaded and ready to go for the user.
Here's an example:
sessionInfo()
file_ext("file.com")
tools::file_ext("file.com")
sessionInfo()
How to avoid class name conflict with a package loaded via a namespace (and not attached) (qdap & openssl)
For a dirty fix run
`[[.qdap_hash` <- `[[.data.frame`
Checking...
> qdap::polarity("test")
all total.sentences total.words ave.polarity sd.polarity stan.mean.polarity
1 all 1 1 0 NA NA
> library(openssl)
Warning message:
package ‘openssl’ was built under R version 3.3.3
> qdap::polarity("test")
Error in derive_pubkey(key) :
RAW() can only be applied to a 'raw', not a 'list'
> `[[.qdap_hash` <- `[[.data.frame`
> qdap::polarity("test")
all total.sentences total.words ave.polarity sd.polarity stan.mean.polarity
1 all 1 1 0 NA NA
>
The offending line in polarity
is words <- c(posneg, alter[[1]])
The object alter
gets created with alter_env
which creates an object which has classes "qdap_hash", "key", ...
qdap_hash doesn't have it's own '[[' method so it checks to see if key has a '[[' method which it typically doesn't. Once openssl gets loaded there is a [[ method for key so it uses that and gives the error since it isn't in the form expected. If we define our own method for qdap_hash that gets called before even attempting to use [[.key
so we bypass the issue. The author of qdap has been informed of the issue and the possible fix.
Namespace dependencies not required
If you use import
or importFrom
in your NAMESPACE file, you should have an entry for that package in the Imports section of your DESCRIPTION file (unless there is a reason that you need to use Depends in which case the package should have an entry in Depends, and not Imports)
Here is a relevant section of Writing R Extensions
The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached. Namespaces accessed by the ‘::’ and ‘:::’ operators must be listed here, or in ‘Suggests’ or ‘Enhances’ (see below). Ideally this field will include all the standard packages that are used, and it is important to include S4-using packages (as their class definitions can change and the DESCRIPTION file is used to decide which packages to re-install when this happens). Packages declared in the ‘Depends’ field should not also be in the ‘Imports’ field.
I made a package with a single function f
. I made a NAMESPACE file with the same importFrom
line that you say you have in yours.
NAMESPACE file
export("f")
importFrom("ggplot2","ggplot","geom_histogram")
At this point, if I run R CMD check
, as expected, I get an error:
Namespace dependency not required: ‘ggplot2’
But, if I add Imports: ggplot2
to the DESCRIPTION such that my DESCRIPTION file is as follows, it passes R CMD check
with no problems.
DESCRIPTION file
Package: anRpackage
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2012-11-07
Author: Me
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: GPL
Imports: ggplot2
Can R differentiate between a manually loaded library and a dependency
R has a subtle difference between a loaded package and an attached package.
A package is attached when you use the library
function,
and it makes its exported functions "visible" to the user's global environment.
If a package is attached,
its namespace has been loaded,
but the opposite is not necessarily true.
Each package can define two main types of dependencies: Depends
and Imports
.
The packages in the former get attached as soon as the dependent package is attached,
but the packages in the latter only get loaded.
This means you can't completely differentiate,
because you may call library
for a specific package,
but any packages it Depends
on will also be attached.
Nevertheless, you can differentiate between loaded and attached packages with loadedNamespaces()
and search()
.
EDIT: It just occurred to me that if you want to track usage of library
(ignoring require
),
you could write a custom tracker:
library_tracker <- with(new.env(), {
packages <- character()
function(flag) {
if (missing(flag)) {
packages <<- union(packages, as.character(substitute(package, parent.frame())))
}
packages
}
})
trace("library", library_tracker, print = FALSE)
library("dplyr")
library(data.table)
# retrieve packages loaded so far
library_tracker(TRUE)
[1] "dplyr" "data.table"
The flag
parameter is just used to distinguish between calls made by trace
,
which call the function without parameters,
and those made outside of it,
in order to easily retrieve packages loaded so far.
You could also use environment(library_tracker)$packages
.
Error: could not find function ... in R
There are a few things you should check :
- Did you write the name of your function correctly? Names are case sensitive.
- Did you install the package that contains the function?
install.packages("thePackage")
(this only needs to be done once) - Did you attach that package to the workspace ?
require(thePackage)
(and check its return value) orlibrary(thePackage)
(this should be done every time you start a new R session) - Are you using an older R version where this function didn't exist yet?
- Are you using a different version of the specific package? This could be in either direction: functions are added and removed over time, and it's possible the code you're referencing is expecting a newer or older version of the package than what you have installed.
If you're not sure in which package that function is situated, you can do a few things.
- If you're sure you installed and attached/loaded the right package, type
help.search("some.function")
or??some.function
to get an information box that can tell you in which package it is contained. find
andgetAnywhere
can also be used to locate functions.- If you have no clue about the package, you can use
findFn
in thesos
package as explained in this answer. RSiteSearch("some.function")
or searching with rdocumentation or rseek are alternative ways to find the function.
Sometimes you need to use an older version of R, but run code created for a newer version. Newly added functions (eg hasName in R 3.4.0) won't be found then. If you use an older R version and want to use a newer function, you can use the package backports to make such functions available. You also find a list of functions that need to be backported on the git repo of backports. Keep in mind that R versions older than R3.0.0 are incompatible with packages built for R3.0.0 and later versions.
Related Topics
What Is the Correct Way to Ask for User Input in an R Program
Regression Tables in Markdown Format (For Flexible Use in R Markdown V2)
How to Include Rmarkdown File in R Package
Replace Accented Characters in R with Non-Accented Counterpart (Utf-8 Encoding)
Is There a Technical Difference Between "=" and "<-"
Dynamic Height and Width for Knitr Plots
Using Geo-Coordinates as Vertex Coordinates in the Igraph R-Package
Summing Across Rows of a Data.Table for Specific Columns
Arrange Plots in a Layout Which Cannot Be Achieved by 'Par(Mfrow ='
How to Fix Outofmemoryerror (Java): Gc Overhead Limit Exceeded in R
How to Draw Two Half Circles in Ggplot in R
How to Separately Control the X and Y Axes Using Ggplot
Subscripts and Superscripts "-" or "+" with Ggplot2 Axis Labels? (Ionic Chemical Notation)
Filter Each Column of a Data.Frame Based on a Specific Value
Add Textbox to Facet Wrapped Layout in Ggplot2