Skip to content

unix

16 messages · Meriema Belaidouni, Anders Nielsen, Peter Dalgaard +8 more

#
hello
Is it possible to execute Unix command from R prompt?
for example
ls
cd ..

In matlab this possible by adding "!"
!ls
!cd ..

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Try something like:

R>system("ls")


Anders
On Mon, 6 Nov 2000, Meriema Belaidouni wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Meriema Belaidouni <meriema at info.univ-angers.fr> writes:
Use system("ls"). We've had the "!" convention at some point, but as I
remember it, people got tired of having it kick in unexpectedly where
logical "not" was intended, and we decided that it was probably a bad
idea to have a syntactically correct R expression do something
completely different when it happened to sit at the beginning of a line...
#
Meriema Belaidouni <meriema at info.univ-angers.fr> writes:
system("ls")

[...]

Cheers
Ross Darnell
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Yes, you can use this:

system("date > date.txt")

for example
Meriema Belaidouni wrote:

  
    
#
I'd like to point out a potential confusion. Although the advice about
system() is technically correct, for the particular case of changing
directories you probably want to use getwd/setwd [I think system() spawns
its own shell, so changing directories with system() isn't persistent].  
For example:
[1] "/home/ben"
[1] "/home/ben"    ## I'm still in the same working directory
NULL
[1] "/home"

system("pwd") would do the same thing as getwd(), but is specific to Unix
(whereas getwd() should also work on Windows etc.)

  For my two cents, the "shorthand" command that I'd like to have in R
would be a shortcut for referring to .Last.value like %, %% in Maple ...
might be hard to find a special character that's not already used for
something, though.
On 6 Nov 2000, Peter Dalgaard BSA wrote:

            

  
    
#
On Mon, 6 Nov 2000, Houix Olivier wrote:

            
Both yes and no - I don't think you can change the working directory,
because system() runs a shell process to termination:
/home/rsb
/home/rsb
/home/rsb/tmp
/home/rsb

Is there a way to do this too?

Roger
#
RB> Both yes and no - I don't think you can change the working directory,
RB> because system() runs a shell process to termination:
RB> /home/rsb
RB> /home/rsb
RB> /home/rsb/tmp
RB> /home/rsb

RB> Is there a way to do this too?


Sure, using setwd():
/home/Leisch/bin
NULL
/tmp

best,
.f
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 6 Nov 2000 ben at zoo.ufl.edu wrote:

            
I agree with this suggestion, I was going to suggest it myself at one
point. (I think it is Mathematica that uses %, %% and Maple uses ",
"",...)

Bill

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
... and Matlab uses "ans" and Mathematica effectively uses %, %%

Would be nice to have something simpler than .Last.value

YG

----- Original Message -----
From: "Bill Simpson" <wsi at gcal.ac.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, November 07, 2000 4:11 AM
Subject: Re: [R] unix
-.-.-
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Yves Gauvreau <cyg at sympatico.ca> writes:
Perhaps I am stating the obvious or being extremely stupid but you can
create a function:

        lv <- function()
         {
         .Last.value
         }

And then use lv() whenever you need .Last.value.

Mark

--
Mark Myatt


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Good suggestion and simple enough.

I had in mind something more like "version" which doesn't require the "()",
but your idea is excellent.

Regards

YG

----- Original Message -----
From: "Mark Myatt" <mark at myatt.demon.co.uk>
To: "Yves Gauvreau" <cyg at sympatico.ca>
Cc: "Bill Simpson" <wsi at gcal.ac.uk>; <r-help at stat.math.ethz.ch>
Sent: Tuesday, November 07, 2000 8:43 AM
Subject: [R] .Last.value or % or ANS or ... (was unix)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Tue, 7 Nov 2000, Mark Myatt wrote:

            
How do you say "give me the output I saw three steps ago?" In Mathematica
you would use %%% or Out[-3].

Bill

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Bill Simpson <wsi at gcal.ac.uk> writes:
Why not implement a LIFO stack? Here is a first attempt:

        #
        # Flush the stack 
        # (call this to create the stack)
        #
        flush <- function()
         {
         stack <<- list()
         }
 
        #
        # Push a value onto the stack
        #
        push <- function()
         {
         stack <<- c(stack, .Last.value)
         }

        #
        # Pull a value from the stack 
        # (default is last pushed value)
        #
        pull <- function(x = 0)
         {
         stack[[length(stack) - x]]
         }

        #
        # Pull a value from the stack and remove it
        # (default is last pushed value)
        #
        pop <- function(x = 0) 
         {
         pop <- stack[[length(stack) - x]]
         stack <<- stack[-length(stack) + x]
         pop
         }

You still need to push() the values you want to keep onto the stack.
Another limitations of the above code is that objects are stored as
'atoms':

        > flush()
        > a <- c(1,2,3,4,5)
        > mean(a)
        [1] 3
        > push()
        > stack
        [[1]]
        [1] 3

        > a
        [1] 1 2 3 4 5
        > push()
        > stack
        [[1]]
        [1] 3

        [[2]]
        [1] 1

        [[3]]
        [1] 2

        [[4]]
        [1] 3

        [[5]]
        [1] 4

        [[6]]
        [1] 5

        > pull(1)
        [1] 4
        > pop(1)
        [1] 4
        > stack
        [[1]]
        [1] 3

        [[2]]
        [1] 1

        [[3]]
        [1] 2 

        [[4]]
        [1] 3

        [[5]]
        [1] 5

Which makes it pretty useless for vector and matrix results but there
should be a way round that.

Mark


--
Mark Myatt


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
[stack implementation]

  But if I could remember to push() every time I could remember to assign
everything to temporary variables anyway ...
#
ben at zoo.ufl.edu writes:
I suppose so. It might be possible to do an automatic push() on any
assignment but that would create an enormous stack just retrieving a
dataset or fitting a model.

Mark

--
Mark Myatt


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._