R package: description, selective import and namespace - r

Although there are quite a few postings on similar topics, none of them helped me understanding how to setup the DESCRIPTION file an R package.
My questions are:
1.) Is my description file correct now? Did I use "depends" and "imports" correctly? (maybe duplicate question...)
2.) Are required packages (dependencies?) automatically installed along with my package when needed, or "loaded" when one of my package function needs to refer to a function of an imported package? (didn't find anything on this issue yet...)
I tried to submit a package to CRAN and got following feedback:
checking package dependencies ... NOTE
Depends: includes the non-default packages:
‘MASS’ ‘car’ ‘foreign’ ‘ggplot2’ ‘lmtest’ ‘plyr’ ‘reshape2’ ‘scales’
Adding so many packages to the search path is excessive and importing selectively is preferable.
I originally had listed all above mentioned packages in the depends section of the DESCRIPTION file. In the NAMESPACE file, I used import(pkgName) for all packages listed above.
After that, I updated my files using importFrom(pkgName, function) in the NAMESPACE file and moved most of the packages to the imports section of my DESCRIPTION file. The package check with the current R-devel-version no longer gives this note. Here's an extract of my DESCRIPTION file:
License: GPL-3
Depends:
ggplot2
Imports:
MASS,
car,
foreign,
lmtest,
plyr,
reshape2,
scales
Collate:
'sjImportSPSS.R'
and the NAMESPACE file:
import(ggplot2)
importFrom(MASS,lda)
importFrom(MASS,loglm)
importFrom(car,crPlots)
importFrom(car,durbinWatsonTest)
importFrom(car,influencePlot)
importFrom(car,leveragePlots)
importFrom(car,ncvTest)
importFrom(car,outlierTest)
importFrom(car,spreadLevelPlot)
importFrom(car,vif)
importFrom(foreign,read.spss)
importFrom(lmtest,bptest)
importFrom(plyr,adply)
importFrom(plyr,ddply)
importFrom(reshape2,melt)
importFrom(scales,brewer_pal)
importFrom(scales,percent)
I'm unsure whether this approach addresses the issue given in the check note above. Furthermore, when I load my package with library(sjPlot), ggplot2 is also attached, but none of the other packages. Does my package still work for other users? What if they don't have all needed packages installed?

From ?install.packages the default behavior is that Depends: and Imports: packages are installed if not already installed. Check out sessionInfo() and you'll see your Imports: are loaded (resident in memory) but not attached (available on disk). If your importFrom statements cover the symbols used in your package code, then your code will work for others (if there were missing imports, you would be warned about undefined global variables).

Related

How to pre-install r packages which are required to run the package you create? [duplicate]

This question already has answers here:
How can I load dependencies in an R package?
(2 answers)
Closed 3 years ago.
I created a new package in R and the functions in my package require pre-installed packages like igraph, dplyr to run properly. In the DESCRIPTION file, I did add these packages in the Imports field. But when I run my package, I am getting an error which indicates the packages required have not been installed.
To check what the problem is, I installed the pre-required packages separately and ran my package, and it seems to be working fine then.
This is how my DESCRIPTION file looks like
Package: xxx
Type: Package
Title: xxx
Version: 0.1.0
Author: xxx
Maintainer: xxx
Description: xxx
License: GPL-2
Encoding: UTF-8
LazyData: FALSE
Imports:
igraph,dplyr,network,gridExtra,centiserve
RoxygenNote: 6.1.1
Is there anyway I could load just my package which then loads the pre-required packages automatically, without having to load pre-required packages like igraph and dplyr manually.
This question raises the same issue as this Stack Overflow question. I offer this answer to provide a little more detail and official sources than what is at the other question.
The issue is that you've listed the packages in Imports rather than Depends. Normally this is preferable, but if you do it this way, you have to take care to import the functions you need in the NAMESPACE file. From Writing R Extensions, Section 1.1.3:
The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached.
Therefore, packages in Imports aren't loaded when your package is loaded, as would be the case if they are specified in Depends.
My advice would be to keep these packages in Imports, then specify the necessary imported functions in NAMESPACE, either manually adding the statements in that file using syntax like
importFrom(foo, f, g)
(See Writing R Extensions, Section 1.5.1), or via roxygen2 tags in your R scripts like
#' #importFrom foo f

Adding dependencies properly to an r package, so that they install automatically

I'm making my first R package, and I'm trying to include package dependencies. The package installs and works fine on my machine, but I already have all of the dependencies installed. When another user attempts to install and they do not have all the dependencies already installed, they get an error.
ERROR: dependency 'dplyr' is not available for package 'my_package'
I am documenting the package via roxygen2.
I know I'm supposed to include #'#import lines in my /R files, and they get added automatically to the DESCRIPTION and NAMESPACE files.
My DESCRIPTION file looks like this:
Package: my_package
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors#R: person("First", "Last", email = "first.last#example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.4.1)
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
Imports: dplyr,
descr
And my NAMESPACE looks like this:
export(my_function)
import(descr)
import(dplyr)
The user is installing the package locally with:
install.packages("C:/custom_packages/my_package_0.0.0.9000.tar.gz/", repos = NULL, type = "source")
Answers I've read on this topic say that having the proper import statements in the DESCRIPTION and NAMESPACE should be all you need to document dependencies, which I have here. The behavior for most CRAN packages I've installed is that if there is a dependency that is not installed, it installs along with the installation. What steps am I missing so that my package mimics this behavior?
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")

Add dependencies for my Rcpp package

Rcpp beginner's question:
I want to improve my execution efficiency in R. So I write some code in cpp and use Rcpp to help me compile them.
Question is that I use some other R packages in my .cpp files and I want those packages to be installed and imported automatically when a user installs my package.
e.g. If I use the R package 'gtools' in my files, I don't want the error:
* installing to library 'C:/Program Files/R/R-3.4.1/library'
* installing *source* package 'pkgname' ...
make: Nothing to be done for `all`.
** libs
installing to C:/Program Files/R/R-3.4.1/library/pkgname/libs/i386
** R
** preparing package for lazy loading
Error in library(gtools) : there is no package called 'gtools'
Error : unable to load R code in package 'pkgname'
ERROR: lazy loading failed for package 'pkgname'
* removing 'C:/Program Files/R/R-3.4.1/library/pkgname'
Exited with status 1.
I tried to add depended package name to the DESCRIPTION file. i.e.
Imports: Rcpp (>= 0.12.12),gtools
LinkingTo: Rcpp, gtools
But it gives me following error:
ERROR: dependency 'gtools' is not available for package 'pkgname'
I don't find any similar questions and please tell me if there are.
First, you should probably make sure gtools is installed on your system. I say this because of the following error:
Error in library(gtools) : there is no package called 'gtools'
With this being said, the main issue you are running into is uncertainty between the LinkingTo: and Imports: fields in the DESCRIPTION file. This is covered in Section 1.1.3: Package Dependencies of Writing R Extensions.
Specifically, we have:
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. Version requirements can be specified and are checked when the namespace is loaded (since R >= 3.0.0).
And the LinkingTo field:
A package that wishes to make use of header files in other packages needs to
declare them as a comma-separated list in the field ‘LinkingTo’ in the
DESCRIPTION file. For example
LinkingTo: link1, link2
The ‘LinkingTo’ field can have a version requirement which is checked at installation.
Specifying a package in ‘LinkingTo’ suffices if these are C++ headers containing source code or static linking is done at installation: the packages do not need to be (and usually should not be) listed in the ‘Depends’ or ‘Imports’ fields. This includes CRAN package BH and almost all users of RcppArmadillo and RcppEigen.
For another use of ‘LinkingTo’ see Linking to native routines in other packages.
So, the Imports: is meant to specify packages that contain R functions that you wish to import. In particular, the function from a given package or the entire package itself must be specified in the NAMESPACE file. For packages that use Rcpp, you can typically expect R functions to be available if the author has exported the routine from C++.
Now, regarding the LinkingTo:, this is a bit more specific. If an author wishes to make available a C++ API via header files they must explicitly declare the statements as is given in native methods of Writing R Extensions. Generally, packages that proceed in this manner are "header-only". These packages place the header definitions under inst/include, e.g.
|- pkgname
|- inst/
|- include/
|- pkgname.h
|- R/
|- man/
|- DESCRIPTION
|- NAMESPACE
However, another trend is to allow for "non-header" packages. This leads to a bit more complicated of topic as you have to understand shared objects and dynamic libraries. CRAN presents an overview of how to "Link" packages in Section 5.8: Linking to other packages of Writing R Extensions
If the author does not make available a C++ API, then there are four options:
Ask the author nicely to support calling the C++ API or submit a patch that enables access to the C++ API.
Call an R function from C++. (This negates any performance gain from writing your code in C++ though.)
Copy the implementation from the author's package while respecting their intellectual property.
Implement the desired functionality from scratch to avoid licensing issues.
Unfortunately, this is the case for gtools. As the author(s) do not provide a means to "link" to the C++ version of package's code.

R devtools:document Dependency package not available

Hi I am following the tutorial here from Hilary and here from Hadley Wickham trying to create a dummy package.
However, my package need some external dependencies XML and RCurl in this case, when I run the command document, it will complain that:
> setwd('/home/datafireball/projects/Rprojects/rgetout/rgetout')
> document()
Error: could not find function "document"
> library(devtools)
> document()
Updating rgetout documentation
Loading rgetout
Loading required namespace: XML
Error in (function (dep_name, dep_ver = NA, dep_compare = NA) :
Dependency package XML not available.
>
Here is my DESCRIPTION file.
Package: rgetout
Title: A R package to get all the outlinks for a given URL
Version: 0.1
Authors#R: "Eric Cartman <Eric.Cartman#gmail.com> [aut, cre]"
Description: This package is intended to include as much web extraction functionality as much as possible. It starts with one function. getout will extract
all the outlinks for a given URL with a user-agent that you can customize.
Depends: R (>= 3.0.2)
Imports:
XML,
RCurl
License: MIT
LazyData: true
Here is the source code github repo if you want to get more info.
If you are having problems with this, even when you have the packages installed and loaded, I suggest you to do the following.
Delete the Imports: and Suggests: entries of your DESCRIPTION file.
Make sure you have usethis working by doing library(usethis)
Now start adding the libraries to your DESCRIPTION file, by running the following command on your console: usethis::use_package("dplyr") for any Imports: you need. Repeat this step for every library that is required.
In my case, dplyr was the one refusing to load. You can decide where the package will be located by doing: usethis::use_package("dplyr", "Suggests").
It is assumed that you will have the required tools / dependencies for developing a package when you are
doing so.
utils::install.packages has a dependencies argument that will attempt to install uninstalled packages on which a package depends / (in whichever way they are dependent (suggests/ depends/linkingTo).
devtools::install_github will perform similarly.
Installing a package and documenting it as a component of development are quiet different activities
.

These packages need to be imported from (in the NAMESPACE file)

In trying to create a local R package, I listed some dependent packages as Depends:
...
Description: NA
License: GPL-2
Depends:R (>= 2.15.0),
survival,
PropCIs,
boot,
msm,
reshape2
LazyData: true
But I got these message by run R CMD check:
*checking dependencies in R code ... NOTE
Packages in Depends field not imported from:
‘PropCIs’ ‘boot’ ‘msm’ ‘reshape2’ ‘survival’
These packages need to be imported from (in the NAMESPACE file)
for when this namespace is loaded but not attached.
Then I use manually added these packages to NAMESPACE file, but it does work and the import lines were deleted automatically after checking.
Another weird thing is the checking process showed:
R CMD check succeeded
But the files then disappeared or deleted systematically/automatically.
May somebody know the reasons?
Just add the following lines to your roxygen code:
#import PropCIs boot msm reshape2 survival

Resources