1

Possible Duplicate:
Equivalent to unix “less” command within R console

I am using R under unix in bash.

Sometimes the output of a command has more lines than the bash.

How do I prevent the output from scrolling away? I.e. is there some equivalent of less and less -S in R?

Community
  • 1
  • 1
  • 3
    Possible duplicate of [Equivalent to unix "less" command within R console](http://stackoverflow.com/questions/2842579/) – rcs Oct 01 '10 at 08:44
  • Thanks! yes that basically covered it already –  Oct 01 '10 at 08:50
  • 1
    Possible duplicate of [Is there an R equivalent to the bash command more?](http://stackoverflow.com/q/3503811/271616) – Joshua Ulrich Oct 01 '10 at 12:25

6 Answers6

3

A way to do this in R is also to redirect to a file:

sink("a_file.txt")
...your_commands...
sink()
Benoit
  • 76,634
  • 23
  • 210
  • 236
3

I think the page() function is like having | less in an R session. It allows two representations of the object; i) a version you'd get from dput(), and ii) a version you'd get if you print()-ed the object.

dat <- data.frame(matrix(rnorm(2000), ncol = 5))
page(dat, method = "print")
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
1

It might be possible to wrap your expression in capture.output, and then page the result to the terminal.

pager <- function(cmd,nlines=10){
  output = capture.output(cmd)
  pages = seq(1,length(output),by=nlines)
  for(p in pages){
    f = p
    l = min(p+nlines-1,length(output))
    cat(paste(output[f:l],"\n"))
    readline("*more*")
  }
  return(invisible(0))
}

Usage: pager(ls()), then hit Return (not space or anything else) at each 'more' prompt.

currently it doesn't return the value. Oh and it fails if there's no output. But you can fix these :)

Or use emacs with ESS and let it all scroll back...

Spacedman
  • 92,590
  • 12
  • 140
  • 224
0

Wouldnt

any_command | more 

work fine?

siliconpi
  • 8,105
  • 18
  • 69
  • 107
  • Probably the guy is inside R, so bash commands are not possible at that stage. – Benoit Oct 01 '10 at 08:22
  • Forgot to make it clear: I am using R in bash in interactive mode, bash commands don't work there. –  Oct 01 '10 at 08:36
0

“the bash” has no lines, your terminal has.

You can set the number of lines of your terminal in the settings of that application.

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • Still, how do I prevent the output from scrolling away if the output has more lines than the currently set number of lines of the terminal? Do you seriously suggest setting the number of lines of the currently used shell according to the length of R's output? –  Oct 01 '10 at 08:41
0

Your question is unclear. If you're talking about using R interactively and accidentally running a command which spits out a huge number of lines, run something like this in your R session: options(max.print=4000)

atp
  • 39
  • 3