If i want to look through a long output or variable in R, is there an equivalent to the bash command more?
I don't believe so, but it should be easy to create. Just look for user input with readline("\nType <Return> to go to the next page : ")
and recursively cycle through the object.
If you use ESS you get to scroll through the R output buffer, and of course search etc as well, at your leisure. Just saying...
For those of us who don't want to use Emacs... ;-) @ Dirk
more <- function(x, n=6) {
i <- 1
while(i <= length(x)) {
j <- min(length(x),i+n-1)
print(x[i:j])
i <- i+n
if(i <= length(x)) readline()
}
}
This isn't going to be pretty on all objects. It's just an example of a default method. You would need to write methods for matrix
, data.frame
, etc.
I rarely scroll through a whole data set in R. When I do I tend to push it to a CSV then use a spreadsheet to peruse it. For just looking at the output in short chunks I use head()
or tail()
I have, of course, been asked by my coworkers if I tail(head))
(yes, head in tail jokes never get old to me)
If you want to look at only a vector, you could do this:
system("more", input=as.character(rnorm(1000)))
This doesn't work well with data frames or matrices because the input
param needs a character vector.
edit
for data frames and matrices you could bring together my "export to CSV" and the command line more
function like this:
myDF <- data.frame(a=rnorm(1000), b=rnorm(1000))
more <- function(dataFrame) {
myTempFile <- tempfile()
write.csv(dataFrame, file=myTempFile, row.names = F)
system(paste("more", myTempFile))
}
more(myDF)
Or just use sytem more
:
more<-function(x){
tempfile()->fn;
sink(fn);print(x);sink();
system(sprintf('more %s',fn));
system(sprintf('rm %s',fn));
}
...or less
which I like because I does not mess the terminal:
less<-function(x){
tempfile()->fn;
sink(fn);print(x);sink();
system(sprintf('less %s',fn));
system(sprintf('rm %s',fn));
}
Both are for *nixes; for Windows, I think it is better to make something based on edit
(and string connections).
Why not use the built-in file.show?
more <- function(x) {
file <- tempfile()
sink(file); on.exit(sink())
print(x)
file.show(file, delete.file = T)
}
more(mtcars)
more(more)