Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly include dependencies in R-package?

I was trying now for several hours to build a package in R and getting a bit desperate about how slowly I progress. I managed quite fast to build a package with no dependencies, everything works fine. Due to recommendations in several posts, I'm using R Studio, devtools and Roxygen2 (being on Windows). With dependencies, I get problems when I CHECK (e.g. with devtools::check() ):

checking dependencies in R code ... NOTE Namespace in Imports field not imported from: 'ggplot2' All declared Imports should be used. See the information on DESCRIPTION files in the chapter 'Creating R packages' of the 'Writing R Extensions' manual.

Furthermore, check() deletes the import(ggplot2) line in the NAMESPACE. If I do check(document=F), it gives an cryptic error about a digest package which is not loaded. I read 'Writing R Extensions' - 1.1.3 Package Dependencies and Hadley's Wiki concerning how to write packages, but couldn't solve my problem. DESCRIPTION and NAMESPACE files of other R packages from CRAN don't look different to mine (for my eyes)?

Question: What am I doing wrong? Sorry for such a basic question, but I am at a loss and most step-by-step tutorials I've seen so far stop before explaining dependencies.

So far, I have 3 files:
A DESCRIPTION:

Package: test
Type: Package
Title: Foo
Version: 1.0
Date: 2014-03-21
Author: Bar
Maintainer: Foo <[email protected]>
Description: Blubb
Imports:
    ggplot2
License: GPL-3

A NAMESPACE:

export(is.equal.null)
import(ggplot2)

A R-File:

#' Extension of compare to include NULLs
#'
#' Works as an extension to usual compare
#' Compares two basic objects which in addition to usual compare can be NULL
#' Intuitive output: TRUE if both are equal or NULL resp., FALSE if both are unequal or only one is NULL
#'
#' @param obj1 Basic object like \code{numeric, char, boolean, NULL}
#' @param obj2 Basic object like \code{numeric, char, boolean, NULL}
#' @keywords compare
#' @export
#' @examples
#' is.equal.null(5,5)  # TRUE
#' is.equal.null(5,NULL)  # FALSE
#' is.equal.null(NULL,NULL)  # TRUE
is.equal.null <- function(obj1, obj2) {
  # Small helper function to generalize comparison to comparison of NULL
  # returns TRUE if both are NULL, and FALSE if only one of the objects is NULL
  bool <- obj1==obj2 
  #qplot(obj1)
  if (length(bool)) return(bool)
  if (is.null(obj1) & is.null(obj2)) return(TRUE)
  return(FALSE)
}
like image 943
Andarin Avatar asked Mar 21 '14 17:03

Andarin


People also ask

How can we install packages in R with all dependencies?

Remember in R, Boolean (TRUE and FALSE) must be all capital letters or R will not recognize them as Boolean. At the top, got to Tools and select Install Packages from the drop down. Finally, make sure install dependencies and checked and click install.

What does imports mean in R package?

Imports is used for packages that are needed by your package but that don't need to be loaded with library() . Packages referred to in @import or @importFrom statements in your Roxygen2 comments, or whose functions are accessed via the :: operator, should be here.

What are dependencies in R?

A dependency is a code that your package needs to run. Dependencies are managed by two files. The DESCRIPTION manages dependencies at the package level; i.e. what packages needs to be installed for your package to work. R has a rich set of ways to describe different types of dependencies.

How do I add packages to R programs?

Part 1-Getting the Package onto Your Computer Open R via your preferred method (icon on desktop, Start Menu, dock, etc.) Click “Packages” in the top menu then click “Install package(s)”. Choose a mirror that is closest to your geographical location. Now you get to choose which packages you want to install.


1 Answers

You need to declare imports in two places:

  1. The DESCRIPTION file. You should have a line similar to:

    Imports: ggplot2, pkg1, pkg2
    
  2. The NAMESPACE file. Here you declare the packages you need

    import(ggplot2)
    

    or to avoid namespace clashes

    importFrom(ggplot2, geom_point)
    

    You can get roxygen2 to maintain the NAMESPACE file using the @import and @importFrom tags.

In your example your DESCRIPTION file looks OK, but you haven't added the necessary functions to the NAMESPACE.

like image 104
csgillespie Avatar answered Oct 23 '22 12:10

csgillespie