[R] would be nice ...

Eric Lecoutre lecoutre at stat.ucl.ac.be
Tue Feb 24 14:51:15 CET 2004


Hi,

What about using cat()?
Here is a piece of code that allows to bypass standard output to produce 
such a result.
Be carefull: it is dangerous: it replaces output by calling 'cat()', which 
is not allowed on all objects...

-----
EatOutput <- function(start=TRUE,stop=!start){
         if (start)
         {

                 ToCat <- function()
                 {
                         function(expr,value,ok,visible){
                         if (visible) {
                                 sink()
                                 cat(value,file="")
                                 cat("\n",file="")
                                 on.exit(sink("tmp"),add=TRUE)
                         }
                         invisible(return(TRUE))
                         }
                 }

                 on.exit(sink("tmp"),add=TRUE)   # To create the first sink
                 on.exit(.out<<-addTaskCallback(ToCat()),add=TRUE)
         }
         else
         {
                 test <- try(removeTaskCallback(.out))
                 if(!inherits(test,"try-error")) sink()

         }
}

-----

Now suppose this piece of code is put in file "eat.r", ending with a call 
to the function EatOutput()

Then:

 > echo "source('eat.r');sum(1:10)" | R --slave
55

If you explain more clearly your needs, maybe we we could propose something 
more accurate.

Eric


 > echo "cat(sum(c(1,2,3)));cat("\n");cat(3*2)" | R --slave
66

At 13:49 24/02/2004, Dirk Eddelbuettel wrote:
>On Tue, Feb 24, 2004 at 10:07:32AM +0100, Martin Maechler wrote:
> > >>>>> "Jan" == Jan de Leeuw <deleeuw at stat.ucla.edu>
> > >>>>>     on Tue, 24 Feb 2004 00:52:51 -0800 writes:
> >
> >     Jan> if R had something like
> >     >> python -c "print(sum([1,2,3]));print(3*2)"
> >     Jan> 6 6
> >
> >     Jan> but I guess the only way to do this is by writing the
> >     Jan> string to a tmp file and then doing something like "R
> >     Jan> CMD BATCH --quiet" on the tmp file
> >
> > Well, a bit better (with a shell prompt "%") is
> >
> >     % echo "print(sum(c(1,2,3)));print(3*2)" | R --quiet --vanilla
> >
> >     > print(sum(c(1,2,3)));print(3*2)
> >     [1] 6
> >     [1] 6
> >     >
> >
> > or (slightly nicer)
> >
> >     % echo "sum(c(1,2,3)); 3*2" | R --quiet --vanilla
> >     > sum(c(1,2,3)); 3*2
> >     [1] 6
> >     [1] 6
> >     >
> >
> > but it still echoes the input by default
>
>Not with --slave:
>
>edd at chibud:~> echo "sum(c(1,2,3)); 3*2" | R --slave
>[1] 6
>[1] 6
>
>I would be pretty trivial to filter the "^[1] " out.
>
>Dirk
>
>--
>The relationship between the computed price and reality is as yet unknown.
>                                              -- From the pac(8) manual page
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward 
Tufte




More information about the R-help mailing list