Externally installing libraries, not only referring - r

I want to build a package which refers to other packages. To create referent to another package I simply create new .R file with my package name (let's say it's megapackage). I use in that file code following.
#' megapackage
#'
#' #docType package
#' #name megapackage
#' #import ggplot2
#' #import dplyr
#' #import zoo
#' #import gridExtra
#' #import scales
#' #import cowplot
#' #import TTR
NULL
Now the problem occurs when I do not have previously installed package (I get error ERROR: lazy loading failed for package 'megapackage'). I mean, #import command works as a referent to library, but it will not install that package by default. How can I make it in such way that it will also install necessary packages, and not only refer to them in library ?

I highly suggest reading Hadley's book R Packages. It's available free online. Here's the relevant part:
Imports: packages listed here must be present for your package to
work. In fact, any time your package is installed, those packages
will, if not already present, be installed on your computer
(devtools::load_all() also checks that the packages are installed).
Try running load_all() or installing your package in a clean installation of R.

You have to actually install the package (not build it).
You need to have a #import dplyr in the .R file and Imports: dplyr in the DESCRIPTION file.
When you install the package with install.packages("filepath", dependencies = T) it should work then.
So you first have to have the external packages installed and build your package to get a .tar.gz file
You can use this .tar.gz package file now to install your package (with install.packages as outlined above). This should now also automatically install the external packages (when not already present).

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.

Package "Imports" not loading in R development package

I am building a package in R in a windows environment using Rstudio, devtools roxygen2 and Rtools.
The package is showing no problems in R CMD CHECK. However when I try to load the package using library("mypkg"), the packages specified under Imports in DESCRIPTION are not being loaded (Loading required package: message is not there). On using pkgDepends("mypkg"), the $Depends is shown as character(0).
I have to load the required packages using library() for mypkg to function.
I am using namespace imports instead of package::function() syntax. All the required packages are there in the NAMESPACE as imports().
Why is this happening? How to solve this?
That's the correct behaviour. Imports just means that code inside your package can see the functions that you import from other packages. The other packages aren't placed on the search path like with Depends.
Further reading:
Better explanation of when to use Imports/Depends

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

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