[R] Sourcing commands but delaying their execution

Duncan Murdoch murdoch at stats.uwo.ca
Fri Aug 3 21:33:51 CEST 2007


On 8/3/2007 3:01 PM, Liaw, Andy wrote:
> Here's one possibility:
> 
> The file "garbage.R" has
> 
>   x <- rnorm(100)
>   print(summary(x))
> 
> You can do:
> 
>   cmds <- parse(file="garbage.R", n=NA)
> 
> and when you want to execute those commands, do
> 
>   eval(cmds)

And since it's Friday afternoon, I'll suggest something that allows the 
commands to be broken into two parts and executed separately (but which 
is ridiculous overkill for this problem):

Put

   x <- rnorm(100)
   divider
   print(summary(x))

into your file, then

   cmds <- parse(file="garbage.R", n=NA)

as in Andy's suggestion.  Then find the dividing line using

   divider <- which(unlist(lapply(cmds, deparse)) == "divider")

and then

   part_one <- 1:(divider-1)
   part_two <- (divider+1):length(cmds)

Finally, execute as

   eval(cmds[part_one])
   MORE_COMMANDS
   eval(cmds[part_two])

Duncan Murdoch
> 
> Andy 
> 
> From: Dennis Fisher
>> 
>> Colleagues:
>> 
>> I have encountered the following situation:
>> 	SERIES OF COMMANDS
>> 	source("File1")
>> 	MORE COMMANDS
>> 	source("File2")
>> 
>> Optimally, I would like File1 and File2 to be merged into a single  
>> file (FileMerged).  However, if I wrote the following:
>> 	SERIES OF COMMANDS
>> 	source("FileMerged")
>> 	MORE COMMANDS
>> 
>> I encounter an error: the File2 portion of FileMerged contains  
>> commands that cannot be executed properly until "MORE COMMANDS" are  
>> executed.  Similarly, sourcing FileMerged after MORE COMMANDS does  
>> not work because MORE COMMANDS requires the information from the  
>> File1 portion of FileMerged.
>> 
>> I am looking for a means to source FileMerged but not execute 
>> some of  
>> the commands immediately.  Functionally this would look like:
>> 	SERIES OF COMMANDS
>> 	source("FileMerged")	# but withhold execution of 
>> some of the commands
>> 	MORE COMMANDS
>> 	COMMAND TO EXECUTE THE WITHHELD COMMANDS
>> 
>> Does R offer some option to accomplish this?
>> 
>> Dennis
>> 
>> Dennis Fisher MD
>> P < (The "P Less Than" Company)
>> Phone: 1-866-PLessThan (1-866-753-7784)
>> Fax: 1-415-564-2220
>> www.PLessThan.com
>> 
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>> 
>> 
>> 
> 
> 
> ------------------------------------------------------------------------------
> Notice:  This e-mail message, together with any attachments,...{{dropped}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list