piechart <- function (x, labels=names(x), edges=200,
	radius=0.8, col=NULL, main=NULL, ...)
{
	if (any(is.na(x) | x <= 0))
		stop("invalid values in pie")
	if (is.null(labels))
		labels <- as.character(1:length(x))
	x <- c(0, cumsum(x)/sum(x))
	dx <- diff(x)
	twopi <- 8 * atan(1)
	pin <- par("pin")
	xlim <- c(-1, 1)
	ylim <- c(-1, 1)
	if (pin[1] > pin[2]) xlim <- (pin[1]/pin[2]) * xlim
	else ylim <- (pin[2]/pin[1]) * ylim
	plot.new()
	plot.window(xlim, ylim, "")
	for (i in 1:length(dx)) {
		n <- floor(edges * dx[i])
		t <- seq(x[i], x[i + 1], length = n)
		xc <- c(cos(twopi * t), 0) * radius
		yc <- c(sin(twopi * t), 0) * radius
		polygon(xc, yc, col=col[(i-1)%%length(col)+1])
		t <- mean(x[i + 0:1])
		xc <- cos(twopi * t) * radius
		yc <- sin(twopi * t) * radius
		lines(c(1,1.05)*xc, c(1,1.05)*yc)
		text(1.1*xc, 1.1*yc, labels[i], xpd = TRUE, adj = ifelse(xc < 0, 1, 0))
	}
	title(main = main, ...)
	invisible(NULL)
}
