tags:

views:

116

answers:

6

If i want to look through a long output or variable in R, is there an equivalent to the bash command more?

A: 

I don't believe so, but it should be easy to create. Just look for user input with readline("\nType <Return> to go to the next page : ") and recursively cycle through the object.

Shane
+1  A: 

If you use ESS you get to scroll through the R output buffer, and of course search etc as well, at your leisure. Just saying...

Dirk Eddelbuettel
+2  A: 

For those of us who don't want to use Emacs... ;-) @ Dirk

more <- function(x, n=6) {
  i <- 1
  while(i <= length(x)) {
    j <- min(length(x),i+n-1)
    print(x[i:j])
    i <- i+n
    if(i <= length(x)) readline()
  }
}

This isn't going to be pretty on all objects. It's just an example of a default method. You would need to write methods for matrix, data.frame, etc.

Joshua Ulrich
Sure, you can always re-invent your own operating system :)
Dirk Eddelbuettel
I should write an assembleR package. Multi-platform may be an issue though...
Joshua Ulrich
Just to note: to use this with a data.frame or matrix, you need to replace `length` with `nrow`.
Shane
A: 

I rarely scroll through a whole data set in R. When I do I tend to push it to a CSV then use a spreadsheet to peruse it. For just looking at the output in short chunks I use head() or tail()

I have, of course, been asked by my coworkers if I tail(head)) (yes, head in tail jokes never get old to me)

If you want to look at only a vector, you could do this:

system("more", input=as.character(rnorm(1000)))

This doesn't work well with data frames or matrices because the input param needs a character vector.

edit

for data frames and matrices you could bring together my "export to CSV" and the command line more function like this:

myDF <- data.frame(a=rnorm(1000), b=rnorm(1000))

more <- function(dataFrame) {
  myTempFile <- tempfile()
  write.csv(dataFrame, file=myTempFile, row.names = F)
  system(paste("more", myTempFile))
}

more(myDF)
JD Long
A: 

Or just use sytem more:

more<-function(x){
    tempfile()->fn;
    sink(fn);print(x);sink();
    system(sprintf('more %s',fn));
    system(sprintf('rm %s',fn));
}

...or less which I like because I does not mess the terminal:

less<-function(x){
    tempfile()->fn;
    sink(fn);print(x);sink();
    system(sprintf('less %s',fn));
    system(sprintf('rm %s',fn));
}

Both are for *nixes; for Windows, I think it is better to make something based on edit (and string connections).

mbq
+5  A: 

Why not use the built-in file.show?

more <- function(x) {
  file <- tempfile()
  sink(file); on.exit(sink())
  print(x)
  file.show(file, delete.file = T)
}

more(mtcars)
more(more)
hadley
I like this. Very nice!
Joshua Ulrich
I had never heard of the file.show function! very nifty.
JD Long