PageRenderTime 17ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/working/auteur/R/table.print.R

http://github.com/eastman/auteur
R | 18 lines | 12 code | 2 blank | 4 comment | 4 complexity | ec91cb8c1f7d3911ead8212644413ada MD5 | raw file
 1#general printing utility for ensuring equal numbers of characters within columns and defining spacing between columns
 2#author: JM EASTMAN 2010
 3#note: works only for numeric dataframes
 4
 5table.print=function(df,digits=4,buffer=5){
 6	if(length(buffer) != ncol(df) | length(buffer)==1) buffer=rep(buffer[1],ncol(df))
 7	if(length(digits) != ncol(df) | length(digits)==1) digits=rep(digits[1],ncol(df))
 8	ss=sapply(round(df),nchar)
 9	lar=df>1
10	nn=sapply(names(df),nchar)
11	
12	# find longest string
13	strw=sapply(1:ncol(df), function(x) max(nn, max(1,(ss[lar])+digits[x],na.rm=TRUE),na.rm=TRUE))  
14	pr.df=data.frame(sapply(1:ncol(df), function(x) sprintf(paste("%",(strw[x]+buffer[x]),".",digits[x],"f",sep=""),df[,x])))   
15	names(pr.df)=names(df)
16	rownames(pr.df)=rownames(df)
17	print(pr.df)
18}