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
While I don't use such a workflow, one option would be to place the
series of commands in a function, and then run that as appropriate.
do.initialisation <- function() {
...
}
do.cleanup <- function() {
}
Best wishes,
Mark
On 03/08/07, Dennis Fisher <fisher at plessthan.com>
wrote:> 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.
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
--
Dr. Mark Wardle
Clinical research fellow and specialist registrar, Neurology
Cardiff, UK
Gabor Grothendieck
2007-Aug-03 17:34 UTC
[R] Sourcing commands but delaying their execution
Merged file can become:
... current file1 contents ...
f <- function() {
... current file 2 contents ...
}
and then source that followed by running your middle code
followed by running f().
Of course, you may need to make some changes in the body
of f due to the changed scoping that this introduces.
On 8/3/07, Dennis Fisher <fisher at plessthan.com>
wrote:> 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.
>
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) 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}}