How to properly include dependencies in R-package?
You need to declare imports in two places:
The DESCRIPTION file. You should have a line similar to:
Imports: ggplot2, pkg1, pkg2
The NAMESPACE file. Here you declare the packages you need
import(ggplot2)
or to avoid namespace clashes
importFrom(ggplot2, geom_point)
You can get roxygen2 to maintain the NAMESPACE file using the
@import
and@importFrom
tags.
In your example your DESCRIPTION file looks OK, but you haven't added the necessary functions to the NAMESPACE.
Adding dependencies properly to an r package, so that they install automatically
A good strategy when it comes to developing your first packages is, in my experience, checking the work of others. The easiest way to do that is to check some of your favorite packages on Github. Here is, for example, a part of one of my DESCRIPTION files:
Depends:
R (>= 3.3.0)
License: GPL-3
Imports:
stringi (>= 1.1.7),
data.table (>= 1.10.4.3),
methods (>= 3.3.0),
quanteda (>= 1.1.0),
scales (>= 0.5.0),
stats (>= 3.3.0),
utils (>= 3.3.0)
As you can see, every package has a minimal version (most of them are simply the versions I use but for some, I tested if older versions work). And I use Imports to mark packages and Depends only to indicate the oldest R version which I successfully tested. You should almost always use Imports or Suggests instead of Depends for packages.
Once you have set this up, you can run:
# This should point to the folder of your DESCRIPTION file
setwd("/path/to/your/package/")
roxygen2::roxygenise(clean = TRUE)
Do not alter the NAMESPACE directly! This should be enough to install your package or put it on GitHub.
However, this is just the tip of the iceberg and it would probably be a good idea to check this post out and then read up on the details in this book.
Update: Given the comment from @Benjamin, I see that I have missed one part of your question. repos = NULL, type = "source"
suppresses the installation of dependencies. A better way would be to use devtools
. I'm not sure if this is the correct way, but when I already have a tarball and need to install it I use something like:
# In Windows use copy and paste directly on the file to get a link
devtools::install_url("C:/custom_packages/my_package_0.0.0.9000.tar.gz")
How to properly include dependencies in the DESCRIPTION file of R package?
To answer your questions:
(1) It is fine to update the DESCRIPTION
file manually.
(2) There is no "proper way" to do this, but I find that the best way to add an R-package as an import is to use usethis::use_package("package")
. This will add it to the correct place in the DESCRIPTION
file, and will remind you to reference the packages you use as package::function()
(which is needed, as the Imports
field only load, and don't attach, the packages).
Regarding putting a package in Depends
vs. Imports
: you should almost always put packages your code relies on as Imports
. You can read more about it here.
I hope this was useful.
Load dependencies only for one function
Use Suggests for this.
You can read about it in Writing R Extensions or in Hadley's Package Basics: Decription. In both cases, the recommendation is
- The optional dependency goes in the
Suggests
field of your packageDESCRIPTION
. - Use
if (requireNamespace)
in the function to test it the package is there.
Something like this:
if (requireNamespace("plotly", quietly = TRUE)) {
# do your plotly stuff
} else {
# do non-plotly stuff
# maybe as simple as stop("Please install plotly to use this function")
}
As for whether you can use require
after requireNamespace
- that seems pointless. Hadley's recommendations seem to be pretty clear to use requireNamespace("plotly")
to load the package and subsequently use plotly::
to call the needed functions.
If you'd prefer to ignore this advice, then just do require
the first time. Using requireNamespace
followed by require
is redundant. As explained in your link, requireNamespace
loads a package without attaching it. require
both loads and attaches.
Use dependencies in R packages through library() / Description file
No, the user does not have to load the packages that are used by functions in my_package
.
The fact that you have listed a package under Imports:
in the DESCRIPTION
file means that during the installation of my_package
, R will check that this package is available on your system. This means then that functions in my_package
can use functions from these packages using the ::
notation, as you suggested.
Using the ::
notation is the recommended way to refer to functions from other packages, but there are also other options:
In order to make all the functions from, say,
dplyr
accessable without::
inmy_package
, you could addimport(dplyr)
to theNAMESPACE
file. This is convenient, if you use many functions from a package.If you intend to use only, say, the function
select
fromdplyr
, you could addimportFrom(select, dplyr)
to theNAMESPACE
file.You could also add the package to the
DESCRIPTION
file underDepends:
. This would mean that the package is loaded to the global environment when you uselibrary(my_package)
. This is almost never a good solution.
The general idea of dependencies is R is that my_package
will have "it's own version" of the packages it depends on loaded. Therefore, you can always be sure that you will, e.g., use the function select()
from the dplyr
package, as you intended to do. The exception is the use of Depends:
which bypasses this system. In this case, my_package
will look for functions in the global environment and if somebody should have defined some function called select()
in the global environment, my_package
will use this function and you will get unexpected results.
Example 1:
DESCRIPTION
file:
Imports:
dpylr
some function from my_package
:
my_fun <- function(...) {
dplyr::mutate(...) %>%
dplyr::select(1:3)
}
Example 2:
DESCRIPTION
file:
Imports:
dpylr
NAMESPACE
file:
import(dplyr)
some function from my_package
:
my_fun <- function(...) {
mutate(...) %>%
select(1:3)
}
Example 3:
DESCRIPTION
file:
Imports:
dpylr
NAMESPACE
file:
importFrom(dplyr,select)
some function from my_package
:
my_fun <- function(...) {
dpylr::mutate(...) %>%
select(1:3)
}
You find more detailed explanations of how to handle dependencies in R packages on the web. For instance the following are useful:
- On the DESCRIPTION file
- On the NAMESPACE file
Also, it is not necessary to write the NAMESPACE
file by hand. You can let roxygen2
do that for you. Read the documentation for more information.
Installing missing dependencies in an R package
When you install the package from a local file, install is going to search for dependencies on the same local path... and won't find them.
To get the CRAN dependecies automatically installed, you can use :
install.packages("devtools")
devtools::install_local("MypackageName.zip")
Related Topics
Plot a Jpg Image Using Base Graphics in R
Why Does Is.Vector() Return True for List
How to Change the Default Directory in Rstudio (Or R)
Error in Na.Fail.Default: Missing Values in Object - But No Missing Values
Dplyr - Mutate Dynamically Named Variables Using Other Dynamically Named Variables
Find Matches of a Vector of Strings in Another Vector of Strings
Using Variable Value as Column Name in Data.Frame or Cbind
Replace Na with Previous and Next Rows Mean in R
How to Use Tidyr to Fill in Completed Rows Within Each Value of a Grouping Variable
Vary the Color Gradient on a Scatter Plot Created with Ggplot2
Download Plotly Using Downloadhandler
Count Total Missing Values by Group