Transform class within package

Asked

Viewed 38 times

1

Guys I have a matrix and I want to turn it into another specific class of a package, rgeos, place the @import in the package, give library in it too, but when I check it gives error that mistake.

a = matrix(1, 2,2)
as(a, "gpc.poly")
Error in as(x, "gpc.poly"): no method or default for coercing "matrix" to "gpc.poly"

How to solve?

P.S.: I’ve read Hadley and the book R Packages, but I got it.

1 answer

1


The following roxygen2 header correctly imports the method you need:

#' Hello
#' @export
#' @importFrom rgeos coerce
hello <- function() {
  a = matrix(1, 2,2)
  as(a, "gpc.poly")
}

This is a little strange: for the function as function properly it precise function coerce package rgeos. Remember that the package needs to be properly installed.

The package build returned this:

==> devtools::document(roclets=c('rd', 'collate', 'namespace'))

Updating test documentation
Loading test
Writing NAMESPACE
Documentation completed

==> R CMD INSTALL --no-multiarch --with-keep.source test

* installing to library ‘/home/dfalbel/R/x86_64-pc-linux-gnu-library/3.2’
* installing *source* package ‘test’ ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (test)

And the function worked correctly:

Restarting R session...

> library(test)
> hello()
GPC Polygon
   Num. Contours:  1 
   Num. Vertices:  2 
   BBox (X):  1 --> 1 
   BBox (Y):  1 --> 1 

Browser other questions tagged

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