26

Is there an equivalent to the unix less command that can be used within the R console?

user207421
  • 305,947
  • 44
  • 307
  • 483
fmark
  • 57,259
  • 27
  • 100
  • 107
  • Are you trying to look at things in the file system, or within the R environment (I presume the latter)? – Shane May 16 '10 at 16:41
  • 1
    Within the R environment. For example, if I `print` a moderately sized dataframe I want to be able to scroll through it. – fmark May 17 '10 at 00:02

5 Answers5

39

There is also page() which displays a representation of an object in a pager, like less.

dat <- data.frame(matrix(rnorm(1000), ncol = 10))
page(dat, method = "print")
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • 1
    Perfect, exactly what I was looking for! – fmark Oct 02 '10 at 08:05
  • 4
    When I run `page(data, method="print")` in RStudio in Windows 8, I get a prompt "How do you want to open this type of file?" with only option "Look for an app in the Store". Do others see this, how can I get it to work? – yic Jan 10 '15 at 19:59
12

Not really. There are the commands

  • head() and tail() for showing the beginning and end of objects
  • print() for explicitly showing an object, and just its name followed by return does the same
  • summary() for concise summary that depends on the object
  • str() for its structure

and more. An equivalent for less would be a little orthogonal to the language and system. Where the Unix shell offers you less to view the content of a file (which is presumed to be ascii-encoded), it cannot know about all types.

R is different in that it knows about the object types which is why summary() -- as well as the whole modeling framework -- are more appropriate.

Follow-up edit: Another possibility is provided by edit() as well as edit.data.frame().

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thanks for the informative answer. I would disagree that "less" would be inappropriate - the main function for which I use less is to scroll string buffers in a console. The R console outputs lots of string buffers. I thought perhaps there might be a use here for buffer scrolling functionality. – fmark May 16 '10 at 05:02
  • 1
    You can always use `system("less")`... of course, if you use any of *NIX systems... – aL3xa May 16 '10 at 05:32
  • 1
    fmark: another possibility is provided by `edit()` and `edit.data.frame()` which you could try. – Dirk Eddelbuettel May 16 '10 at 11:34
  • 1
    aL3xa: Yes, but not for R objects. – Dirk Eddelbuettel May 16 '10 at 11:35
  • @Dirk edit(), while not ideal, does what I need. Thanks. If you write that out as an answer I will accept it. – fmark May 16 '10 at 12:14
  • @Dirk, yup... that's the catch... =) – aL3xa May 16 '10 at 13:46
7

I save the print output to a file and then read it using an editor or less.

Type the following in R

sink("Routput.txt")
print(varname)
sink()

Then in a shell:

less Routput.txt
Sameer
  • 1,807
  • 1
  • 15
  • 16
  • Works well when you want to scroll through the str() of a large data set (page() does not work for this) – avriis Oct 24 '17 at 13:37
  • If you use sink("Routput.txt", split=TRUE) you can view the output in another terminal window (at least on unix) and still operate the R console. If the file gets too big, you can truncate it with ">Routput.txt" and R will keep writing to it. – Steve Dutky Nov 27 '19 at 22:14
2

If the file is already on disk, then you can use file.show

hadley
  • 102,019
  • 32
  • 183
  • 245
1

You might like my little toy here:

short <- function(x=seq(1,20),numel=4,skipel=0,ynam=deparse(substitute(x))) {
ynam<-as.character(ynam)
#clean up spaces
ynam<-gsub(" ","",ynam)
#unlist goes by columns, so transpose to get what's expected
if(is.list(x)) x<-unlist(t(x))
if(2*numel >= length(x)) {
    print(x)
    }
    else {  
        frist=1+skipel
        last=numel+skipel
        cat(paste(ynam,'[',frist,'] thru ',ynam,'[',last,']\n',sep=""))
        print(x[frist:last])
        cat(' ... \n')
        cat(paste(ynam,'[',length(x)-numel-skipel+1,'] thru ', ynam, '[', length(x)-skipel,']\n',sep=""))
        print(x[(length(x)-numel-skipel+1):(length(x)-skipel)])
        }
}

blahblah copyright by me, not Disney blahblah free for use, reuse, editing, sprinkling on your Wheaties, etc.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73