0

In a Unix terminal, it is possible to look at a text file page by page using commands "less" or "more". I have a character vector with 300 lines and I would like to look at it page by page. Do you know a similar function in R ?

PAC
  • 5,178
  • 8
  • 38
  • 62
  • `head(dat)` or `tail(dat)` ? – zx8754 Jun 25 '13 at 08:25
  • Yes but then only show the first and the last lines. – PAC Jun 25 '13 at 08:26
  • 1
    Try some of the solutions on: http://stackoverflow.com/questions/3837520/how-to-prevent-the-output-of-r-to-scroll-away-in-bash `page(dat, method="print")` looks promising – Scott Ritchie Jun 25 '13 at 08:27
  • [Equivalent to unix “less” command within R console](http://stackoverflow.com/questions/2842579/equivalent-to-unix-less-command-within-r-console) – zx8754 Jun 25 '13 at 08:28
  • I would like to have a look at the character vector in the console, not in a new page like with the page() function. – PAC Jun 25 '13 at 08:38
  • You can still use your system command , using `system` or `shell`,for example `system('less dat.txt',intern=TRUE)` – agstudy Jun 25 '13 at 08:53
  • If you are just interested in the first/last line of a vector, wouldn't then dat[1] and dat[length(dat)] work for you? – Daniel Fischer Jun 25 '13 at 09:30

1 Answers1

1

If you're referring to an object in your R environment (as opposed to a file on your drive),

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
  • How does this work. It seems similar to head(). Am I wrong ? – PAC Jun 25 '13 at 13:22
  • @PAC yes it's similar, but `numel` specifies how many elements to return, `skipel` lets you start anywhere (not just at the top), and it returns `numel` elements from both the beginning and the end of the data. You can easily mod the code if you want only "head" xor "tail" returned. – Carl Witthoft Jun 25 '13 at 13:59