Package "Imports" not loading in R development package - r

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

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 include a requried packages inside my own package in R?

I built my own package and everything is working well. However, I need to load another packages when I load my package. That is, I want R loads all requires packages automatically when I load my package like this:
library(mypackage)
loading requrie packages:
first package
second package
Is there any easy way?
Add them in your DESCRIPTION file, section Depends.
Depends:
first package,
second package
You should read the book R Packages.

Could not find function "year" after installation from zip file R

I'm using R and working on a server without internet connection. So I had to search how to install packages from a zip file. I wanted to use the lubridate package.
install.packages("V:/R/lubridate_1.3.3.zip", repos=NULL)
Then I tried to use
library(lubridate)
year(data$date)
but I received an error that package or namespace load failed for "lubridate". And the function year could not be found.
Did I forgot any step?
If you install a package with install.packages from a CRAN mirror, the imports and dependency tree is installed automatically. If you download the zip and do an offline installation that's obviously not possible. Thus, you have to download and install all these packages, too.
You can find the primary dependencies and imports from lubridate's CRAN page and then follow the links to get the whole trees. Or you can get it even more easily from MRAN.
If I didn't miss anything, you need plyr, stringr, memoise, Rcpp, stringi, magrittr, and digest.
This method is also probably not really feasible for packages with big dependency trees. You could use the function from this SO answer in such a case.

imported packages in devtools

I develop my R package in RStudio. I use load_all() from devtools (either typing in the console or from the Build menu), which seems to work fine. If I then work on an R function in my package and source it, when I try and run the function I need to load the libraries it depends on, despite these other packages being imported (through the DESCRIPTION file).
So for example, I will get the message:
could not find function "trellis.par.get"
and I need to load the lattice package etc.
Is this expected behaviour? I would have thought devtools would have imported these other package dependencies, or am I misunderstanding something here?
Thanks
David

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