# Copyright 2006 Université de Liège, Nicolas Mazziotta, 
# sous les termes de la licence GPL2

require("Hmisc", quiet=T)


toTxt <- function(x) UseMethod("toTxt")

toTxt.tricroise<- function(tri, caption=NULL, outtri=NULL) {
	outtri$tab<-tri$tab
	outtri$caption<-caption
	if (!is.null(tri$pem)) {
		tri$pem<-ifelse(is.na(tri$chi2signif), paste(tri$pem," ooo", sep=""),
			ifelse(tri$chi2signif>=0.01, paste(tri$pem," oo*", sep=""), 
				ifelse(tri$chi2signif>=0.001, paste(tri$pem," o**",sep=""), paste(tri$pem," ***",sep=""))
			))
		if (!is.null(tri$pem)) {
			outtri$pem<-format(tri$pem, justify="right")
		}
	}
	p.value<-tri$chi2$p.value
	if (!is.null(tri$fisher)) {
		p.value<-tri$fisher$p.value
		outtri$stat<-paste("Test exact de Fisher, p = ", format(p.value, dig=3), sep="")
	} else {
		outtri$stat<-paste("χ² = ", format(tri$chi2$stat, dig=2),	", ddl = ", tri$chi2$parameter, ", p = ", round(tri$chi2$p.value, dig=3), sep="")
		outtri$stat<-paste(outtri$stat, ifelse(p.value >.05,  "ooo", ifelse(p.value >.01,  "oo*", ifelse(p.value >.001, "o**", "***"))))
	}
		#outtri$stat<-paste("\nTest exact de fisher, p = ", round(tri$fisher$p.value, dig=3), sep="")
		#outtri$stat<-paste(outtri$stat, ifelse(tri$fisher$p.value >.05,  "ooo", ifelse(tri$fisher$p.value >.01,  "oo*", ifelse(tri$fisher$p.value >.001, "o**", "***"))))

		outtri$stat<-paste(outtri$stat, ", min. = ", round(tri$chi2minexp), sep="")
	return(outtri)
}

toTxt.tricroisecoll <- function(tricoll, outcoll=NULL) {
	x<-names(tricoll)
	for (i in 1:length(tricoll)) {
		y<-names(tricoll[[i]])
		for (j in  1:length(tricoll[[i]])) {
			format<-toTxt(tricoll[[i]][[j]])
			outcoll[paste(x[i], "x", y[j])]<-list(format)
		}
	}
	return(outcoll)
}


#vim: encoding=utf-8 fileencoding=utf-8
