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

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

Related

R package: how to make my package loading other package when I library my package

I have made my own package called "test" and I install it. However, there exist some problem.
For example, in my .R file, I use function "rowQuantiles" from other package called "matrixStats".
And I already add
import(matrixStats)
to namespace file
and add
Imports:matrixStats (>= 0.57.0)
to description file.
However, whenever I library my own pakcage
library(test)
The following error always comes out
could not find function "rowQuantiles"
How can I make that whenever I library my own package it will load other required packages. Just like the following
> library(ggpubr)
Loading required package: ggplot2
Key to understand this is to understand the difference between loading a package and attaching a package.
Packages listed under Imports: are only loaded when your package attached, i.e. when you do library(mypkg). In contrast, packages listed under Depends: are loaded and attached. So, if you use:
Depends: matrixStats
then all of matrixStats functions will be on the search() path when you package is attached.

How to make my own package install external packages by default

I have a question about installing external packages within my own package. Let's say my package requires two packages : ggplot2 and TTR. I create a new .R file which was saved in package R folder with code following :
#' mypackage
#'
#' #docType package
#' #name mypackage
#' #import ggplot2
#' #import TTR
NULL
Now #import command by default does not install lacking packages. During my package installation I get information that there is no package TTR. This problem does not occur when I manually install TTR package. Then #import command works properly and recognize TTR package. My question is : how can I tell #import command to also install lacking packages ? I tried load_all but it's not working.
The problem you are probably encountering is that your package needs to be on CRAN so that install.packages also installs the external packages according to the value of the dependencies argument.
The reason for this is that install.packages assumes that the dependencies are located at the same place as your package.
If your package is a local .tar.gz or .zip file, you can use :
devtools::install_local('mypackage.tar.gz')
This will automatically get the CRAN dependencies defined in #import
When you want to add external packages to your package, have have to do the following:
add package in DESCRIPTION file
#import in .R file for roxygen2
In the description file you have to add a line:
Imports:
stats,
grDevices,
ggplot2
In your .R file, when using roxygen2 you have to add a line:
#' #import ggplot2
roxygen2 then automatically adds ggplot2 to the NAMESPACE file for you (where it needs to be).
When somebody installs your package, ggplot2 will now be automatically be installed.
Just speculation, but maybe the problem was, that you or the user did not actually install the package, but instead has opened the .Rproj file and tried to build the package. Then I think you have to have the installed the package before manually.
Try to build your package. You get a tar.gz / .tgz and install the package in R Studio under Tools -> Install packages or with the install.packages command.

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
.

R package: description, selective import and namespace

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).

Resources