Skip to content

would be nice ...

5 messages · Jan de Leeuw, Martin Maechler, Dirk Eddelbuettel +2 more

#
if R had something like

 > python -c "print(sum([1,2,3]));print(3*2)"
6
6

but I guess the only way to do this is by writing the string to
a tmp file and then doing something like "R CMD BATCH --quiet"
on the tmp file

I would like to use this for an R service, which allows you to
select any string in any application and replace it by its
R evaluation

===
Jan de Leeuw; Professor and Chair, UCLA Department of Statistics;
Editor: Journal of Multivariate Analysis, Journal of Statistical  
Software
US mail: 8130 Math Sciences Bldg, Box 951554, Los Angeles, CA 90095-1554
phone (310)-825-9550;  fax (310)-206-5658;  email: deleeuw at stat.ucla.edu
homepage: http://gifi.stat.ucla.edu
   
------------------------------------------------------------------------ 
-------------------------
           No matter where you go, there you are. --- Buckaroo Banzai
                    http://gifi.stat.ucla.edu/sounds/nomatter.au
#
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

    Jan> I would like to use this for an R service, which allows
    Jan> you to select any string in any application and replace
    Jan> it by its R evaluation
#
On Tue, Feb 24, 2004 at 10:07:32AM +0100, Martin Maechler wrote:
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
#
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:
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
#
On Tue, 24 Feb 2004, Martin Maechler wrote:

            
You can use --slave to suppress the input

[al:~] thomas% echo "print(sum(1:3))" | R --slave
[1] 6



	-thomas