# R program to create html from .csv file and template. # This program is indended to be called from the shell file "cgihtml". # It creates cgihtmllog.tmp with comments. # Shell file "cgihtml" writes user's request to "cgihtmlInfile.tmp" # and "cgihtmlOutfile.tmp" and "cgireadDBfilename.tmp" # Get csv filename dataName=scan("cgireadDBfile.tmp", what="", quiet=TRUE) if (!file.exists(dataName)) { write(paste("Can't open",dataName), file="cgihtmllog.tmp") quit(save="no") } baseName=dataName i=nc=nchar(dataName) while (i>1) { if (substring(dataName,i,i)==".") { baseName=substring(dataName,1,i-1) break } i=i-1 } csvName=paste(baseName,".csv",sep="") if (!file.exists(csvName)) { write(paste("Can't open", csvName), file="cgihtmllog.tmp") quit(save="no") } data=read.table(csvName, header=TRUE, sep=",", as.is=TRUE) if (any(names(data)=="category")) data=data[order(data$category),] # Read filenames from these files and verify that they exist. if (!file.exists("cgihtmlInfile.tmp")) { write("Can't open cgihtmlInfile.tmp", file="cgihtmllog.tmp") quit(save="no") } templateName=scan("cgihtmlInfile.tmp", what="", quiet=TRUE) if (!file.exists(templateName)) { write(paste("Can't open",templateName), file="cgihtmllog.tmp") quit(save="no") } if (!file.exists("cgihtmlOutfile.tmp")) { write("Can't open cgihtmlOutfile.tmp", file="cgihtmllog.tmp") quit(save="no") } outName=scan("cgihtmlOutfile.tmp", what="", quiet=TRUE) # Read data from template file (one field per line) lines=scan(templateName, sep="\n", strip.white=TRUE, what="", quiet=TRUE) lines=lines[nchar(lines)>0] nl=length(lines) if (nl==0) { write(paste(templateName, "is empty."), file="cgihtmllog.tmp") quit(save="no") } # Record lines with equal signs (start of field) and positions of equal signs tableloc=as.numeric(regexpr("^TABLE",lines)) if (sum(tableloc==1)!=1) { write(paste(templateName,"must have exactly one TABLE line."), file="cgihtmllog.tmp") quit(save="no") } tableloc=seq(along=lines)[tableloc==1] # Create html file write(lines[1:(tableloc-1)], file=outName, ncolumns=1, append=FALSE) write("
| ") write(paste(" |
|---|
| ",tmp," | ") write(paste(" |
| ",tmp," | "), file=outName, ncolumns=1, append=TRUE) } write("