Sunteți pe pagina 1din 5

html.table<function(x, file = "", append = F, main = "", center = F, html.tag = F, table.at tributes = "BORDER", column.attributes = c(row.header.column.attributes, rep(data.column.

attributes, length = ncol(x))), cell.attributes = "", row.header.attributes = "ALIGN=RIGHT", row.header.column.attributes = "" , data.column.attributes = "", ...) { # formats data.frames, matrices, and vectors as html tables # also accepts a list of data.frames, matrices, and/or vectors # does not handle more sophisticated structures # Add a space for those with identation: if(dim(x)[1]>0){ indent.ind<-substring(dimnames(x)[[1]],1,1)==" " if(sum(indent.ind,na.rm=T)>0){ dimnames(x)[[1]][which(indent.ind)]<-paste("&#160 ",dimnames(x)[[1]][which(inden t.ind)]) } } out <- c() # add <HTML> tag if(html.tag) out <- c(out, "<HTML>") # add <CENTER> tag if(center) out <- c(out, "<CENTER>") # if x is a list, iterate over elements # print list component names for each element # pad between elements with empty paragraph if(is.list(x) && !inherits(x, "data.frame")) { if(!is.null(main) && main != "") out <- c(out, paste("<H2>", main, "</H2>")) n <- length(x) for(i in seq(length = n)) { out <- c(out, html.table(x[[i]], main = names(x)[i])) if(i < n) out <- c(out, "<P> </P>") } } else { # if it's not a list, format it as a character matrix if(inherits(x, "data.frame")) { dimnames.x <- dimnames(x) dim.x <- dim(x) x <- sapply(x, format, ...) if(dim.x[1] <= 1) { x <- matrix(x, nrow = dim.x[1], ncol = dim.x[2], byrow = T) dimnames(x) <- dimnames.x } } else if(is.matrix(x)) { dimnames.x <- dimnames(x)

dim.x <- dim(x) x <- sapply(split(x, col(x)), format, ...) if(dim.x[1] <= 1) { x <- matrix(x, nrow = dim.x[1], ncol = dim.x[2], byrow = T) dimnames(x) <- dimnames.x } } else { dimnames.x <- list(names(x), NULL) x <- as.matrix(format(x, ...)) } out <- c(out, paste(collapse = " ", "<TABLE", paste(collapse = " ", table.attributes), ">")) if(!is.null(main) && main != "") out <- c(out, paste("<CAPTION> <H3>", main, "</H3> </CAP TION>", collapse = " ")) out <- c(out, paste("<TR>", paste(paste("<TH", column.attributes , ">"), c(" ", dimnames.x[[2]]), "</TH>", collapse = " "), "</TR>", collapse = " ")) # data rows row.header.attributes <- rep(row.header.attributes, len = nrow(x )) if(is.matrix(cell.attributes)) { if(!identical(dim(cell.attributes), dim(x))) stop("If cell.attributes is a matrix it must hav e same dimensions as x") for(i in seq(length = nrow(x))) { out <- c(out, paste(paste("\n <TR", row.header .attributes[i], ">"), "<TH>", dimnames.x[[1]][i], "</TH>", paste(sep = "", paste("\n <TD", cell.attribute s[i, ], ">", sep = " "), x[i, ], "</TD>", colla pse = " "), "</TR>", collapse = " ")) } } else { for(i in seq(length = nrow(x))) { out <- c(out, paste(paste("\n <TR", row.header .attributes[i], ">"), "<TH>", dimnames.x[[1]][i], "</TH>", paste(sep = "", paste("\n <TD", cell.attribute s, ">", sep = " "), x[i, ], "</TD>", collapse = " "), "</TR>", collapse = " ")) } } out <- c(out, "</TABLE>") } if(center) out <- c(out, "</CENTER>") if(html.tag) out <- c(out, "</HTML>") # return vector of strings or write to file and return file name if(file == "") return(out) else { write(out, file = file, append = append) invisible(file) } }

fun.check.1<-function(x,y){ # x is median percentage change # y is median change if(x<100 & y<10){ return(TRUE) } if(x<50 & y>=10 & y < 100){ return(TRUE) } if(x<20 & y>=100 & y < 1000){ return(TRUE) } if(x<50 & y>=1000){ return(TRUE) } else return(FALSE) } msd<-function(x){ x<-na.omit(x) len<-length(x) return(sqrt(sum((x-median(x))^2/(len)))) } chem.sig<-function(x){ if(any(is.na(x))==FALSE){ x[abs(x)>=1]<-signif(x[abs(x)>=1],3) x[abs(x)<1]<-round(x[abs(x)<1],3) return(x)} if(any(is.na(x))==TRUE){ not.a.id<-which(!is.na(x)) NA.val<-rep(NA,length(x)) xx<-na.omit(x) if(length(xx)!=0){ xx[abs(xx)>=1]<-signif(xx[abs(xx)>=1],3) xx[abs(xx)<1]<-round(x[abs(xx)<1],3) NA.val[not.a.id]<-xx return(NA.val) } else return(rep(NA,length(x))) } } format.col.column<-function(x,id){ x.format<-array(x,dim(x)) if(sum(as.numeric(as.character(x[id, -1])))==(ncol(x)-1)){

x.format<-paste("bgcolor=white ","align='right'") return(x.format) } if(sum(as.numeric(as.character(x[id, -1])))==0){ x.format<-paste("bgcolor=lightblue ","align='right'") return(x.format) } x.format[,-1][,x[id,-1]==0]<-paste("bgcolor=lightblue ","align='right'") x.format[,-1][,x[id,-1]!=0]<-paste("bgcolor=white ","align='right'") x.format[,1]<-paste("bgcolor=white ","align='right'") return(x.format) } round.excel<-function(x,ndec){ if(ndec<0 | round(ndec,0)!=ndec) {return(rep(NA,length(x)))} s<-sign(x) x<-abs(x*10^(ndec-1)) aux<-round(x,1) aux<-ifelse(aux - trunc(aux)==0.5,ceiling(aux),round(aux, 0)) return(s*aux/10^(ndec-1)) }

ms.cross.over.function<-function(val,ratio){ if(any(is.na(val)) | any(is.na(ratio))){ return(NA) } if(val<=1 | val>5000){return(TRUE)} if(val>1 & val<=10 & ratio <= 3 & ratio >=.3){ return(TRUE) } if(val>10 & val<=500 & ratio <= 2 & ratio >=.5){ return(TRUE) } if(val>500 & val<=1000 & ratio <= 3 & ratio >=.3){ return(TRUE) } if(val>1000 & val<=5000 & ratio <= 4 & ratio >=.25){ return(TRUE) } else {return(FALSE)} } format.col.column.single<-function(x) {

x.format <- rep(paste("bgcolor=white ", "align='right'"),length(x)) if(any(is.na(x) | x==FALSE)){ x.format[which(is.na(x) | x==FALSE)] <- paste("bgcolor=lightblue ", "align='right'") } return(x.format) } format.col.column.m<function (x, id) { x.format <- array(x, dim(x)) if (sum(as.numeric(as.character(x[id, -1]))) == (ncol(x) 1)) { x.format <- paste("bgcolor=white ", "align='right'") return(x.format) } if (sum(as.numeric(as.character(x[id, -1]))) == 0) { x.format <- paste("bgcolor=lightblue ", "align='right'") return(x.format) } x.format[, x[id, ] == 0] <- paste("bgcolor=lightblue ", "align='right'") x.format[, x[id, ] != 0] <- paste("bgcolor=white ", "align='right'") return(x.format) }

S-ar putea să vă placă și