Create a package that depends on other packages

Asked

Viewed 48 times

1

Staff I am creating a package with some functions one of them depends on another package, as "gives the signal" for R understand and install this package? The package I depend on is raster.

a = matrix(c(0,0,2,0,2,2,0,2), byrow = T, ncol = 2)
b = list(matrix(c(0.5, 0 ,1 , 1, 1.5, 0), ncol = 2, byrow = T))

polygon.intersection <- function(polygon1,polygon2){
  polygon11 = as(polygon1, "gpc.poly")
  polygon22 = as(polygon2, "gpc.poly")
  res = intersect(polygon11, polygon22)
  if(is.na(get.bbox(res)$x[[1]])) res = matrix(c(0,0,0,0), ncol = 2)

  else res1 = as(res, "matrix")
  res1
}
polygon.intersection(a,b)

1 answer

2


In the directory where your package code is, there must be a metadata file called DESCRIPTION. In this file, one of the fields that can be included is the Imports, indicating that your package depends on another package(s) package(s) to work.

For example, your file could contain the following:

Imports:
    raster,
    sp

Note that your package does not install the other packages by itself, but these dependencies are read when someone runs install.packages() and all imported packages are also installed.

I recommend reading the hadley’s website for more details on creating packages.

  • My import it’s like this, but it’s not working.

  • 1

    Why doesn’t it work? The Imports does not load the package, only changes the installation. Ideally you always use pacote::função so you don’t have to use library and make the code clearer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.