26

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 <bar@mail.com>
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)
}
Andarin
  • 799
  • 1
  • 8
  • 17

2 Answers2

26

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.

csgillespie
  • 59,189
  • 14
  • 150
  • 185
  • At the top of my NAMESPACE file I import several packages, and I have them all listed under Imports in my DESCRIPTION file, but they are still not being installed when I install my package. Am I missing something? Also, @csgillespie, what do you mean when you say you can get roxygen2 to maintain the NAMESPACE file. I have been re-adding these imports after I `roxygenise` instead – mikey Dec 13 '21 at 20:36
  • roxygen2 can generate the namespace file. – csgillespie Dec 21 '21 at 16:53
  • Yes, but when I use roxygen2 to generate the namespace file it does not include the imports, it only includes the exports, so I have to add them manually – mikey Dec 21 '21 at 17:22
  • 2
    Try https://stackoverflow.com/questions/29135971/namespace-not-generated-by-roxygen2-skipped-confusion-with-hadley-book – csgillespie Dec 22 '21 at 19:24
  • Thank you @csgillespie ! Now the NAMESPACE file is properly created, but the dependencies still don't install when I install the package from source. But this is a separate issue. – mikey Dec 23 '21 at 13:15
4

A standard workflow, as described by Hadley, is:

library(devtools)

# Add a dependency
use_package('tibble')

# (Re-)build NAMESPACE
document()

# Reload the package: CTRL-L or
load_all()
James Hirschorn
  • 7,032
  • 5
  • 45
  • 53