-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
Sent: Wednesday, April 11, 2001 2:55 AM
To: Henrik Bengtsson
Cc: r-help at stat.math.ethz.ch
Subject: RE: [R] Echoing commands (not using source())
On Wed, 11 Apr 2001, Henrik Bengtsson wrote:
No, I am actually not thinking about [R] as a macro language. I
about the "reflection" methods build in into the language, e.g.
Internal(body(fun)). But I guess that it requires the source to
into the memory, which is not necessary. Is that correct?
There's a trick. The code is kept attached to the function for
user-written code. Try
attr(myFunction, "source")
[1] "function() {"
[2] "source(\"myFunction.R\", echo=TRUE);"
[3] "}"
Writing the code "twice" using cat(...) makes it easier to do misstakes.
I did say `pre-processing': of course R can do it for you, rather easily.
Another idea is to but the whole code into a character string
it. That works, but the code won't be parsed until run-time. I
use source() for now. It solves my problem and is also more
since the code is only loaded iff the function is called, e.g.
myFunction <- function() {
source("myFunction.R", echo=TRUE);
}
Hrmm. You are still writing incorrect R!
The problem with this is that it will search for the file in the current
directory, i.e. it won't work within libraries if you don't
of lookup method such as found in demo() and example().
Thanks
Henrik Bengtsson
-----Original Message-----
From: ripley at auk.stats [mailto:ripley at auk.stats]On Behalf Of
D Ripley
Sent: Tuesday, April 10, 2001 11:42 PM
To: Henrik Bengtsson
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Echoing commands (not using source())
On Tue, 10 Apr 2001, Henrik Bengtsson wrote:
Is there a way of echoing (similar to Matlab "echo on")
being executed? I know how to do this for script files by using
echo=TRUE), but I would like to be able to use this within
*any* function. I
One problem is that is not that what source(..., echo = TRUE
does). It echo
expressions at the prompt level, but not all commands. R is
am also not looking for the debug() function. Here is what I
do:
myFunction <- function() {
echo(TRUE);
x <- 3;
cat("x = ", x, "\n");
echo(FALSE);
}
(That's not really correct R. Line feeds are command separators as
well as ;, so you have a lot of empty commands in there.)
x <- 3;
cat("x = ", x, "\n");
x = 3
You can only do this by modifying the function to print the commands,
as source() does. Note that by time the function is being
is already parsed, so in a sense there is no `command' to be echoed.
You can get a rough re-construction via deparse on expressions
(and a command line can include multiple expressions).
Why do you want to do this? A simple pre-processing of the function
to be
myFunction <- function() {
cat("x <- 3", "\n")
x <- 3
cat("cat(\"x = \", x, \"\n\"), "\n")
cat("x = ", x, "\n")
}
might be simplest, but I get the impression that you are thinking
of R as a
macro language and there may be a more powerful approach to your
real goal.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-
r-help mailing list -- Read
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._._._._
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595