# Functions for lab 4 # by Tom Minka # R 1.7.1 ############################################################################# # Standard startup if(!exists("zip.unpack")) { zip.unpack <- function(path,destdir) { cmd = paste("unzip -q",path,"-d",destdir) status = system(cmd) if(status > 0) stop(paste("unzip failed. do you have",path,"?")) status } } install.local <- function(package,path="",destdir=NULL,chdir=T) { if(is.null(destdir)) destdir = tempdir() if(path == "") { if(exists("choose.files")) path = choose.files(caption=paste("Select",package,"zip file"),multi=F) else path = "mining.zip" } zip.unpack(path,destdir) #package = basename(path) #package = strsplit(package,"_")[[1]][1] library(package,lib.loc=destdir,character.only=T,warn=F) if(chdir) { olddir = getwd() newdir = dirname(path) setwd(newdir) # get the canonical version of newdir newdir = getwd() if(newdir != olddir) { cat("Working directory is now",newdir,"\n") } } } my.require <- function(package) { package = deparse(substitute(package)) if(length(.find.package(package,quiet=T))>0) library(package,char=T) else install.local(package) } my.require(mining) ############################################################################# rgb2indexed <- function(img) { col <- rgb(img[,1],img[,2],img[,3]) unique.col <- unique(col) ind <- match(col,unique.col) attr(ind,"col") <- unique.col ind } plot.image <- function(img,col) { if(missing(col)) { col <- attr(img,"col") if(is.null(col)) { if(any(floor(img) != img)) stop("image is not integer valued") else if(any(img < 1)) stop("image is not positive") else col <- 1:max(img) } } width <- nrow(img) height <- ncol(img) opar <- par(mar=c(0,0,0,0)) on.exit(par(opar)) image(1:width,1:height,img[,height:1],col=col,axes=F,asp=1,xlab="",ylab="") } ############################################################################# load("lab2_imgs.rda") load("lab4_img.rda")