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")> funcsFunction 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.
Joshua Wiley
2010-Aug-10 00:52 UTC
[R] Importing arguments for use by functions in a script
Hi, Since no one has answered this, I will take a shot. I doubt it would really take more work to just put the functions in a script. Sure you would have to 'customize' each one, but since you are entering their names, name of the desired output object, and arguments, simply directly entering that into a script and then running the whole thing with source() seems pretty comparable to me. I guess my point with all that is that you might reconsider problem and what your goal is. Nevertheless, if you are determined that storing all of that in a text file is what you want to do, perhaps this will help you get things to work. I imagine you could even wrap up all the steps below into a function and then just apply() that function to your text file and skip all the for loops. Note the single quotes inside the character strings. resname <- "whatever" funname <- "grep" argname <- "pattern = 'h', x = c('hi','bye')" myfun <- paste(funname, "(", argname, ")", sep = "") system.time(assign(x = resname, value = eval(parse(text = myfun)))) Josh On Sun, Aug 8, 2010 at 7:36 AM, EvansA <annaevans66 at hotmail.com> wrote:> > 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. > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Thank you for your response. Re: "I guess my point with all that is that you might reconsider problem and what your goal is" You are quite right, I just got caught up with the not being able to do something - and now see the error of my ways. But I am glad to see your solution. -- View this message in context: http://r.789695.n4.nabble.com/Importing-arguments-for-use-by-functions-in-a-script-tp2317758p2320383.html Sent from the R help mailing list archive at Nabble.com.
Apparently Analagous Threads
- [PATCH v2] xfstests: btrfs/316: cross-subvolume sparse copy
- [PATCH 1/2] xfstests: add generic/321 to test fsync() on directories V2
- [PATCH v3] xfstests: btrfs/316: cross-subvolume sparse copy
- [PATCH] xfstests: add generic/320 to test fsync() on directories V2
- [PATCH] ocfs2: fix missing reset j_num_trans for sync