Equivalent to unix “less” command within R console

2020-01-25 01:54发布

问题:

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

回答1:

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")


回答2:

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().



回答3:

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


回答4:

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



回答5:

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.



标签: r shell