[R] Echoing commands (not using source())

Henrik Bengtsson henrikb at braju.com
Wed Apr 11 12:21:03 CEST 2001


...

> > myFunction <- function() {
> >   source("myFunction.R", echo=TRUE);
> > }
>
> Hrmm. You are still writing incorrect R!

But, this is why I asked in the first place. I want to be able to have a
function that works the same way as example("foo.R") but instead having the
example code living in a script file, it should be a function living in
memory.

In the long run, this will be used in my object oriented add on I am writing
for [R] (which works and it provides references!). One could then define a
static method called example(). With static classes, as in Java, the user
can write:

  MyClass$example()

which should run and show an example code. This should behave similar to
example("MyClass"), but as I started out, it would be nice if the code could
actually live inside same scope/environment and not have to be searched
which is the case when using example(). I'll stick with source()/example()
for now.

Henrik Bengtsson
MSc Computer Science

> -----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
> was thinking
> > about the "reflection" methods build in into the language, e.g.
> > Internal(body(fun)). But I guess that it requires the source to
> be loaded
> > 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
> and evaluate
> > it. That works, but the code won't be parsed until run-time. I
> think I will
> > use source() for now. It solves my problem and is also more
> memory efficient
> > 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
> have some kind
> > 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
> Prof Brian
> > > 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")
> > > commands that are
> > > > being executed? I know how to do this for script files by using
> > > source(...,
> > > > 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
> not a macro
> > > language like Matlab.
> > >
> > > > am also not looking for the debug() function. Here is what I
> > > would like to
> > > > 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.)
> > >
> > > > Output:
> > > > >myFunction()
> > > > 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
> evaluated it
> > > 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
> http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > 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
>

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list