[R] Importing arguments for use by functions in a script
EvansA
annaevans66 at hotmail.com
Sun Aug 8 16:36:30 CEST 2010
QUESTION:
Is there a way of passing arguments from an external file to a script so
that they can be used directly by functions within the script?
I have a series of interdependent functions. I wish to test the time for
processing various datasets.
I was initially doing something along the lines of the following (yes, I am
new to R):
rm(list= ls())
systime1<-system.time(source("seq_imp_fct.R"))
systime2<-system.time(source("pattern_fct.R"))
systime3<-system.time(source("AAdistribution_fct.R"))
# run function
systime101<-system.time(seqres<-seq_imp_fct("testprot.txt"))
systime102<-system.time(patres<-pattern_fct(pattern="SS{1,2}",sequences=seqres))
systime103<-system.time(AAres<-AAdistribution_fct(sequences=patres))
As my list is of functions is growing, I was attempting to write a script
that would allow me to semi-automate this.
I have created a table of function file names, variables to hold return
values, arguments
Function Returns Arguments
"seq_imp_fct.R" seqres "testprot.txt"
"pattern_fct.R" patres pattern="SS{1,2}",sequences=seqres
"AAdistribution_fct.R" Aares sequences=patres
which I am reading in:
funcs<-read.table("functions.txt",sep="\t",header=TRUE,colClasses =
"character")
> funcs
Function Returns Arguments
1 seq_imp_fct.r seqres testprot.txt
2 pattern_fct.r patres pattern=SS{1,2},sequences=seqres
3 AAdistribution_fct.r Aares sequences=patres
#######################################################################
# source functions and get system times
#######################################################################
systimes<-data.frame()
nrows<-nrow(funcs)
i<-0
for(i in 1:nrows)
{
func<-funcs[i,1]
systime<-system.time(source(func))
systimes[i,1]<-func
systimes[i,2]<-"source"
systimes[i,3]<-systime[1]
systimes[i,4]<-systime[2]
systimes[i,5]<-systime[3]
}
#######################################################################
# run functions and get system times
#######################################################################
ii<-i+1
iiend<-i*2
i<-0
for(ii in ii:iiend)
{
i<-i+1
func<-funcs[i,1]
rets<-as.name(funcs[i,2])
args<-funcs[i,3]
# remove .R from function
sfunc<-substring(func,1,((nchar(func))-2))
# convert text string to an object
ssfunc<-get(sfunc)
# convert text string in rets to object pointer and run function
systemtime<-system.time(eval(call("<-",rets,call("<-",rets,(ssfunc(args))))))
print(seqres)
systimes[ii,1]<-func
systimes[ii,2]<-"process"
systimes[ii,3]<-systemtime[1]
systimes[ii,4]<-systemtime[2]
systimes[ii,5]<-systemtime[3]
}
This works fine when doing the equivalent of:
systime101<-system.time(seqres<-seq_imp_fct("testprot.txt"))
But falls over when doing the "equivalent" of:
systime102<-system.time(patres<-pattern_fct(pattern="SS{1,2}",sequences=seqres))
Generating error:
Error in gregexpr(pattern, as.character(sequences[, 3]), perl = T) :
argument "sequences" is missing, with no default
Timing stopped at: 0 0.01 0.02
I don't want to have to customise the handling of arguments for each
function.
Is there a way around this?
--
View this message in context: http://r.789695.n4.nabble.com/Importing-arguments-for-use-by-functions-in-a-script-tp2317758p2317758.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list