Including libraries in a package or main Script - r

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

Related

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.

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

R package installation when another package needed is imported but not depended

When I am writing an R package, there are several packages that I listed as Import instead of Depend.
My question is that, when people are installing my package from source, i.e. install.packages('pkgNmae', repo=NULL, type="source"), and their computer does not have the necessary packages I imported pre-installed, will the command install.packages recognize this? Will the user be able to use my package after installation?
I am not sure if I list these packages in Import they'll be automatically installed if needed. Thanks!
Yes, they will be automatically installed if needed. Imports and depends are identical for the purpose of installation - they only differ in their package-load-time behaviour.

How force R to load package, `library()` fails due to dependency?

How force R to load package when some (not crucial) packages it depends on aren't installed ?
Motivation : sometimes I have to use R in places, where I can't automatically install required packages. Doing so manually is very time consuming and in most cases I need only a vary small part of functions contained in installed package.
Typical message error in this case is :
> library(packageX)
Loading required package: packageY
Error: package ‘packageY’ could not be loaded
In addition: Warning messages:
In library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc = lib.loc) : there is no package called ‘packageY’
Mayby 'devtools' package will be helpful. But I haven't investigate it.
If you cannot install the dependencies, I think your only option is to remove those packages from the Depends field of DESCRIPTION file of packageX and try to rebuild packageX
You can look at the source of the package to figure out which functions you need and what dependencies they need. You may want to collaborate with the author/maintainer of the package on this part. You could create a private version of the package that does not have the dependencies and the other functionality that you don't need.
If this is only for your personal use and the license for the package allows it (gpl and similar) then you don't need the authors permission to extract those pieces you want. If you want to link your package to this for distribution then you should work with the original author. I know a couple of package authors wanted just a couple of functions from one of my packages and I agreed that loading my entire package (and dependencies) was overkill for what they wanted to do, so I worked with them and they have copies of the functions in their package without needing to depend on mine. When I update one of the functions I send a copy to them to update their copy as well.

Resources