[R] Am having trouble calling a function

James Rome jamesrome at gmail.com
Sat Aug 27 22:34:16 CEST 2011


In my main R program, I have

source("retaanalysis/Functions/doAirport.R")
.... stuff to read data and calculate ads
sapply(ads,    function(x) {doAirport(x, base)} )

And doAirport has

# analyze the flights for a given airport
doAirport = function(df, base) {
    # Get rid of unused runway factor levels (from other airports)
    df$lrw <- drop.levels(df$lrw) # In gdata package
    # Drop messages from after the landing time
    df = df[df$PredTime >= 0.0,]
    airport = as.character(df[1,"Dest"])
    #print it out
    airport
    date = strptime(df[1,"on"], format="%Y-%m-%d")
    rwys = factor(unique(df$lrw), ordered=TRUE)    # Get the names of
the runways
    rwys = as.vector(rwys)
    nrwys = length(rwys)
    # Make a data frame with the correct index for the runway
    rdf = data.frame(lrw=rwys, rwyidx=seq(1:nrwys))
    df = merge(df, rdf, all.x=TRUE)

    #colours <- c(RF = "brown", AF =383, PH="red",PF = "black",
    #BA = "green", FI = "blue", FF = 56, PS = "magenta", TC=94)
    colours <- c("0"="red","1"="black",
    "2"="green", "3"="blue","4"="magenta","5"="orange")
    shapes <- c(RF = "R", AF = "f", PH="H", PF = "P",
    BA = "B", FI = "I", FF = "F", PS = "S", TC="T")
   
    #Plot individual flight data
    dfm = df
    dfm = dfm[!df$MsgType=="AS",]   # Eliminate AS messages
    dfm$MsgType=drop.levels(dfm$MsgType)
    dfm = dfm[as.numeric(dfm$PredTime) < 60.0,]
    dfm$tc= as.factor(floor(dfm$PredTime/10.0))  # get 10-minute bin
    dfm$flightfact = drop.levels(dfm$flightfact)
    row.names(dfm) = seq(1:dim(dfm)[1])
   
    #Find max and min error for each flight
    library(zoo)
    maxes = tapply(dfm$dt,dfm$flightfact,FUN=max)  # Returns a list
    mins = tapply(dfm$dt,dfm$flightfact,FUN=min)
    mdf = data.frame(flight=index(maxes), maxes=as.numeric(maxes),
        mins=as.numeric(mins))
    # Add a column for colors
    mdf$clr = as.factor((mdf$flight - 1) %% 5)
    mc = c("red","cyan","green","blue","magenta","black")
    # Plot these
    outfile = paste(base, airport, "/", airport, "ErrorRange", date,
".pdf", sep="")
    pdf(file=outfile, width=14, height=7, par(lwd=.5))
    # Get only ones that span 0
    lblx = dim(mdf)[1]/2
....
}

And if I manually set
df = ads[[j]]
doAirport runs OK.

But when called from the main program, I always get
> sapply(ads,    function(x) {doAirport(df, base)} )
Error in eval(expr, envir, enclos) : object 'lblx' not found
In addition: Warning message:
'mode(onefile)' differs between new and previous
     ==> NOT changing 'onefile'
> sapply(ads,    function(x) {doAirport(x, base)} )
Error in eval(expr, envir, enclos) : object 'lblx' not found
In addition: Warning message:
'mode(onefile)' differs between new and previous
     ==> NOT changing 'onefile'

What am I doing wrong?

Thanks,
Jim

E-mail: jamesrome at gmail.com
URL: http://jamesrome.net



More information about the R-help mailing list