Equivalent to unix “less” command within R console

2020-01-25 01:37发布

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

标签: r shell
5条回答
Deceive 欺骗
2楼-- · 2020-01-25 02:03

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楼-- · 2020-01-25 02:03

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

查看更多
够拽才男人
4楼-- · 2020-01-25 02:05

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.

查看更多
别忘想泡老子
5楼-- · 2020-01-25 02:07

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
查看更多
倾城 Initia
6楼-- · 2020-01-25 02:18

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")
查看更多
登录 后发表回答