[R] attempt at making a polygon class failed

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Aug 29 10:11:26 CEST 2007



Leeds, Mark (IED) wrote:
> I was reading a presentation of Professor Peng's and typed the
> presentation code into R but I changed it to make plot.polygon a 
> separate function instread of defining the function in SetMethod itself
> as he did. Is that the problem with the code below because
> plot(p) just gives me zero. Thanks.
> 
> 
> setClass("polygon", representation(x = "numeric",
> 				   y = "numeric"))
> 
> plot <- function(object)
> 	{
> 		return(0)
> 	}


If you redefine plot this way, you cannot use a call to the generic plot 
within the plot.polygon function any more.
Hence do *not* define the new plot() function above. The rest should work.

Uwe Ligges




> setGeneric("plot")
> 
> plot.polygon <- function(x, y, ...) {
> 	xlim <- range(x at x)
> 	ylim <- range(x at y)
> 	plot(0,0, type = "n", xlim = xlim, ylim = ylim, ...)
> 	xp <- c(x at x, x at x[1])
> 	yp <- c(x at y, x at y[1])
> 	lines(xp, yp)
> }
> 
> setMethod("plot","polygon",plot.polygon)
> 
> p <- new("polygon", x = c(1,2,3,4), y = c(1,2,3,1))
> 
> plot(p)
> --------------------------------------------------------
> 
> This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list