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

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.

Including libraries in a package or main Script

I am building a package for the first time.
The package needs couple of libraries to work. Should I include those libraries in the package for every function? or should I include them in my main Script?
In the DESCRIPTION file in your package you can list the packages your package depends on. This will allow you to use the code from those packages to be used anywhere in your package. So, there is no need for explicit use of library or require. When your package is loaded, the other packages will also be loaded. In addition, when setting dependencies = TRUE in install.packages the packages your package depends on will also be installed (if available on CRAN).

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

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

What is the difference between a library and a package in R?

In R what is the difference between a library and a package?
I have come across posts where people refer to packages within a library. Based on this idea I interpret it that a package lives in a library (i.e I store my packages with a designated library). However I get confused when I want to use package 'x'.
I am under the imperssion I need to call the library function to get package 'x' to be in use ?
And once I have have called upon package 'x' the functions of package 'x' then become available to me ?
In R, a package is a collection of R functions, data and compiled code. The location where the packages are stored is called the library. If there is a particular functionality that you require, you can download the package from the appropriate site and it will be stored in your library. To actually use the package use the command "library(package)" which makes that package available to you. Then just call the appropriate package functions etc.
1. Package
Package extends basic R functionality and standardizes the distribution of code. For example, a package can contain a set of functions relating to a specific topic or tasks.
Packages can be distributed as SOURCE (a directory with all package components), BINARIES (contains files in OS-specific format) or as a BUNDLE (compressed file containing package components, similar to source).
The most basic package, for example created with,
library(devtools)
create("C:/Users/Documents/R-dev/MyPackage")
contains:
R/ directory where all the R code goes to, and DESCRIPTION and NAMESPACE metadata files.
2. Library
Library is a directory where the packages are stored. You can have multiple libraries on your hard drive.
To see which libraries are available (which paths are searched for packages):
.libPaths()
And to see which packages are there:
lapply(.libPaths(), dir)
To use package ‘x’, it first has to be installed in a package library. This can be done for example, with:
install.packages(‘x’) # to install packages from CRAN
or
R CMD INSTALL Xpackagename.tar.gz #to install directly from source
Once installed it has to be loaded into memory with library(x) or require(x).

Resources