Dimitri Liakhovitski
2012-Feb-03 21:23 UTC
[R] incomplete final line found on <name of my sourced function file>
Dear R-ers, I hope there is a really simple solution to my problem. I've written a function that I saved in an .r file. I source this file in my code. For a while it worked fine. But then when I run the line: source("F mylineplot.r") I started getting a warning: In readLines(file) : incomplete final line found on 'F mylineplot.r' I have no idea why - I tried to check and to recheck what's going on, but am not finding anything. The code works both when I try to run it NOT as a function and when I run it AS a function. So why the warning message? Just in case - the text of my function inside my file that I source. I really don't expect anyone to dig into it - but maybe something will jump at you? Thanks a lot! Dimitri ### Creating a plot with (aggregated) several lines: # indata - my data frame # datesvar - name of the variable that contains dates # inars - names of the variables to be graphed # myfunction - function to be used (mean or sum) # my metric - string for the metric # mytitle - title of the graph # fixedy - if 1, range on y axis starts with zero # indata=en;datesvar="Week";invars=seas[5];myfunction=mean # mymetric="TEST";fixedy=0;title="BLA" mylines = function(indata,datesvar,invars,myfunction,mymetric,mytitle,fixedy=0) { all.colors<-c("#E0EEEE","#0000CD","#D4D4D4","#FFC1C1","#FFDEAD","#9ACD32", "#99CCFF","#6495ED","#66CDAA","#EEC900","#BC8F8F", "#C00000","#696969","#473C8B","#8B4500", "#FF7F00","#9370DB", "#800000","#104E8B","#228B22")[20:1] myagg<-aggregate(indata[invars],by=indata[datesvar],FUN=myfunction) yrange=range(pretty(as.matrix(myagg[2:length(myagg)]))) if(fixedy==0){ ymin<-yrange[1] ymax<-yrange[2]} else { ymin<-0 ymax<-yrange[2]} ydistance<-ymax-ymin if(ydistance>0.1 & ydistance<=1){mystep<-0.1} else { if(ydistance>1 & ydistance<=10){mystep<-1/2} else { if(ydistance>10 & ydistance<=100) {mystep<-10/5} else { if(ydistance>100 & ydistance<=1000) {mystep<-100/5} else { if(ydistance>1000 & ydistance<=10000) {mystep<-1000/2} else { if(ydistance>10000 & ydistance<=100000) {mystep<-10000/5} else { mystep<-100000/5 } } } } } } nr.of.dates<-length(myagg[[datesvar]]); index<-seq(1,nr.of.dates,2) par(bg = "white") plot(x=myagg[[datesvar]],y=myagg[,2],ylim=c(ymin,ymax),col=all.colors[1],type='l', ylab=mymetric,xlab="",lwd=2,xaxt='n',yaxt='n',main=mytitle) mycolors<-1 for(i in 2:length(invars)){ mycolors<-c(mycolors,(i)) } axis(1, labels =format(as.Date(myagg[[datesvar]][index], origin="1970-01-01"), "%Y-%m-%d"), at=myagg[[datesvar]][index], las=2,cex.axis=0.8) axis(2, labels=seq(ymin,ymax,by=mystep),at=seq(ymin,ymax,by=mystep),las=1,cex.axis=0.9) abline(v=myagg[[datesvar]][index],lty="dotted",col = "lightgray") # abline(h=seq(ymin,ymax,by=mystep), lty="dotted",col = "lightgray") legend("topleft",inset=0,legend=invars,fill=all.colors[mycolors],horiz=T,bg="white",cex=1) # ?plot points(myagg[[datesvar]],myagg[[invars[1]]],type="l",lwd=3,lty=i,col=all.colors[1]) for(i in 2:length(invars)){ points(myagg[[datesvar]],myagg[[invars[i]]],type="l",lwd=2,lty=1,col=all.colors[i]) # or lty=i mycolors<-c(mycolors,(i)) } return(myagg) } -- Dimitri Liakhovitski marketfusionanalytics.com
R. Michael Weylandt
2012-Feb-03 21:28 UTC
[R] incomplete final line found on <name of my sourced function file>
Try opening the file up in a text editor and inserting a blank line or two on the end. (There's either an EOL or EOF character missing and this trick usually works for me -- never sure why/when it happens though) Michael On Fri, Feb 3, 2012 at 4:23 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:> Dear R-ers, > > I hope there is a really simple solution to my problem. > I've written a function that I saved in an .r file. I source this file > in my code. For a while it worked fine. But then when I run the line: > > source("F mylineplot.r") > > I started getting a warning: > In readLines(file) : incomplete final line found on 'F mylineplot.r' > > I have no idea why - I tried to check and to recheck what's going on, > but am not finding anything. > The code works both when I try to run it NOT as a function and when I > run it AS a function. So why the warning message? > > Just in case - the text of my function inside my file that I source. I > really don't expect anyone to dig into it - but maybe something will > jump at you? > Thanks a lot! > Dimitri > > > ### Creating a plot with (aggregated) several lines: > # indata - my data frame > # datesvar - name of the variable that contains dates > # inars - names of the variables to be graphed > # myfunction - function to be used (mean or sum) > # my metric - string for the metric > # mytitle - title of the graph > # fixedy - if 1, range on y axis starts with zero > # indata=en;datesvar="Week";invars=seas[5];myfunction=mean > # mymetric="TEST";fixedy=0;title="BLA" > > mylines = function(indata,datesvar,invars,myfunction,mymetric,mytitle,fixedy=0) > { > > ?all.colors<-c("#E0EEEE","#0000CD","#D4D4D4","#FFC1C1","#FFDEAD","#9ACD32", > ? ? ? ?"#99CCFF","#6495ED","#66CDAA","#EEC900","#BC8F8F", > ? "#C00000","#696969","#473C8B","#8B4500", ? ? "#FF7F00","#9370DB", > ? ? ? ?"#800000","#104E8B","#228B22")[20:1] > > > ?myagg<-aggregate(indata[invars],by=indata[datesvar],FUN=myfunction) > ?yrange=range(pretty(as.matrix(myagg[2:length(myagg)]))) > ?if(fixedy==0){ > ? ?ymin<-yrange[1] > ? ?ymax<-yrange[2]} else { > ? ?ymin<-0 > ? ?ymax<-yrange[2]} > ?ydistance<-ymax-ymin > ?if(ydistance>0.1 & ydistance<=1){mystep<-0.1} else { > ? ?if(ydistance>1 & ydistance<=10){mystep<-1/2} else { > ? ? ? ?if(ydistance>10 & ydistance<=100) {mystep<-10/5} else { > ? ? ? ?if(ydistance>100 & ydistance<=1000) {mystep<-100/5} else { > ? ? ? ? ?if(ydistance>1000 & ydistance<=10000) {mystep<-1000/2} else { > ? ? ? ? ? ?if(ydistance>10000 & ydistance<=100000) {mystep<-10000/5} else { > ? ? ? ? ? ? ?mystep<-100000/5 > ? ? ? ? ? ?} > ? ? ? ? ?} > ? ? ? ?} > ? ? ?} > ? ?} > ?} > ?nr.of.dates<-length(myagg[[datesvar]]); index<-seq(1,nr.of.dates,2) > ?par(bg = "white") > ?plot(x=myagg[[datesvar]],y=myagg[,2],ylim=c(ymin,ymax),col=all.colors[1],type='l', > ? ? ? ? ?ylab=mymetric,xlab="",lwd=2,xaxt='n',yaxt='n',main=mytitle) > ?mycolors<-1 > ?for(i in 2:length(invars)){ > ? ?mycolors<-c(mycolors,(i)) > ?} > > ?axis(1, labels =format(as.Date(myagg[[datesvar]][index], > origin="1970-01-01"), "%Y-%m-%d"), > ? ? ? ? ? ? ? ?at=myagg[[datesvar]][index], las=2,cex.axis=0.8) > ?axis(2, labels=seq(ymin,ymax,by=mystep),at=seq(ymin,ymax,by=mystep),las=1,cex.axis=0.9) > ?abline(v=myagg[[datesvar]][index],lty="dotted",col = "lightgray") > # ?abline(h=seq(ymin,ymax,by=mystep), lty="dotted",col = "lightgray") > ?legend("topleft",inset=0,legend=invars,fill=all.colors[mycolors],horiz=T,bg="white",cex=1) > # ?plot > > ?points(myagg[[datesvar]],myagg[[invars[1]]],type="l",lwd=3,lty=i,col=all.colors[1]) > ?for(i in 2:length(invars)){ > ? ?points(myagg[[datesvar]],myagg[[invars[i]]],type="l",lwd=2,lty=1,col=all.colors[i]) > # or lty=i > ? ?mycolors<-c(mycolors,(i)) > ?} > ?return(myagg) > } > > > -- > Dimitri Liakhovitski > marketfusionanalytics.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.