R devtools:document Dependency package not available - r

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
.

Related

Why doesn't Devtools respect my dependency constraints in an R package

I am not an R programmer. I am a web developer that is used to composer dependency manager for PHP which has .json file that lets you specify package version constraints for your application. Then you run the manager and it resolves your dependencies to a set of versions, which it writes in the .lock file. From that lock file I have a very specific set of package version that does not change when I deploy to my production environment.
I am looking for similar mechanics with R as we are running R in the cloud and we are looking to automate the deployment and we want to be sure we have the exact same package version running in dev as in production.
So to get a similar behavior, I've decided to lock the dependencies in the DESCRIPTION file of my package by specifying an exact version of my package (which not recommended in the docs). So I've made an empty R package with a single dependency (by following this tutorial: https://tinyheero.github.io/jekyll/update/2015/07/26/making-your-first-R-package.html) with the following DESCRIPTION. ananas is just a random name for my random package.
Package: ananas
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors#R:
person(given = "First",
family = "Last",
role = c("aut", "cre"),
email = "first.last#example.com",
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: What license it uses
Encoding: UTF-8
LazyData: true
Imports:
anytime (== 0.3.0)
If you check the documentation at http://r-pkgs.had.co.nz/description.html#dependencies and you scroll to "versioning" title (not "version" which is further down). You can see an example clearly stating that it is possible to use exact dependencies version constraints.
Then I tried to use the following commands to install the dependencies and the package itself
Rscript -e 'devtools::install("ananas", lib="r-library", upgrade="never")'
Rscript -e 'devtools::install("ananas", lib="r-library")'
And I have an .Rprofile file located where I run the install command with the following content
.libPaths(c("./r-library", .libPaths()))
Yet, when I got to the r-library folder to check the package anytime. I opened the DESCRIPTION file for the anytime package and I see it is at the latest version of 0.3.7 at the time of writing this which does not correspond to the constraints of exactly 0.3.0 in the DESCRIPTION file of my package.
So my question is why devtools does not respect the version constraints? What am I missing?
Very similar to the following unanswered stackoverflow issue: R doesn't respect maximum version when installing package dependencies

Adding dependencies properly to an r package, so that they install automatically

I'm making my first R package, and I'm trying to include package dependencies. The package installs and works fine on my machine, but I already have all of the dependencies installed. When another user attempts to install and they do not have all the dependencies already installed, they get an error.
ERROR: dependency 'dplyr' is not available for package 'my_package'
I am documenting the package via roxygen2.
I know I'm supposed to include #'#import lines in my /R files, and they get added automatically to the DESCRIPTION and NAMESPACE files.
My DESCRIPTION file looks like this:
Package: my_package
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors#R: person("First", "Last", email = "first.last#example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.4.1)
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
Imports: dplyr,
descr
And my NAMESPACE looks like this:
export(my_function)
import(descr)
import(dplyr)
The user is installing the package locally with:
install.packages("C:/custom_packages/my_package_0.0.0.9000.tar.gz/", repos = NULL, type = "source")
Answers I've read on this topic say that having the proper import statements in the DESCRIPTION and NAMESPACE should be all you need to document dependencies, which I have here. The behavior for most CRAN packages I've installed is that if there is a dependency that is not installed, it installs along with the installation. What steps am I missing so that my package mimics this behavior?
A good strategy when it comes to developing your first packages is, in my experience, checking the work of others. The easiest way to do that is to check some of your favorite packages on Github. Here is, for example, a part of one of my DESCRIPTION files:
Depends:
R (>= 3.3.0)
License: GPL-3
Imports:
stringi (>= 1.1.7),
data.table (>= 1.10.4.3),
methods (>= 3.3.0),
quanteda (>= 1.1.0),
scales (>= 0.5.0),
stats (>= 3.3.0),
utils (>= 3.3.0)
As you can see, every package has a minimal version (most of them are simply the versions I use but for some, I tested if older versions work). And I use Imports to mark packages and Depends only to indicate the oldest R version which I successfully tested. You should almost always use Imports or Suggests instead of Depends for packages.
Once you have set this up, you can run:
# This should point to the folder of your DESCRIPTION file
setwd("/path/to/your/package/")
roxygen2::roxygenise(clean = TRUE)
Do not alter the NAMESPACE directly! This should be enough to install your package or put it on GitHub.
However, this is just the tip of the iceberg and it would probably be a good idea to check this post out and then read up on the details in this book.
Update: Given the comment from #Benjamin, I see that I have missed one part of your question. repos = NULL, type = "source" suppresses the installation of dependencies. A better way would be to use devtools. I'm not sure if this is the correct way, but when I already have a tarball and need to install it I use something like:
# In Windows use copy and paste directly on the file to get a link
devtools::install_url("C:/custom_packages/my_package_0.0.0.9000.tar.gz")

R - Error: Dependency 'leaps' (or dplyr) is not available for package xxx

I'm sorry for the simple question but I'm trying to build my first R package but run into trouble when trying to declare which external packages need to be installed before my R scripts should be run.
If I add a line
Imports: leaps
In my DESCRIPTION file
And add the line
library(leaps)
into my script it gives me the error:
Error: Dependency 'leaps' is not available for package xxx
when trying to build and reload my custom package.
Leaps is has not been installed previously but obviously the code runs through when using install.packages()
This is under R 3.4.2
Any suggestions what I am missing? Thank you very much in advance.
Sorry, to add to the before but I wanted to say, that the same error persists also for all other packages like dplyr and also if I use Depends: dplyr instead of Imports: dplyr.
A code example of both the Description and the Name file is below:
DESCRIPTION
Package: test123
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself#somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true
Imports: dplyr
Remotes: url::https://cran.rstudio.com
hello.R
hello <- function() {
print("Hello, world!")
}

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