Is there another R equivalent to the bash command?

If I want to view long output or a variable in R, is there an equivalent to the bash command?

+7
r
source share
7 answers

Why not use the embedded file.show file?

more <- function(x) { file <- tempfile() sink(file); on.exit(sink()) print(x) file.show(file, delete.file = T) } more(mtcars) more(more) 
+11
source share

If you use ESS, you can scroll through the output buffer R and, of course, search, etc., in your free time. Just saying ...

+3
source share

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 will not be good for all objects. This is just an example of the default method. You will need to write methods for matrix , data.frame , etc.

+3
source share

I don’t believe that, but it should be easy to create. Just find user input using readline("\nType <Return> to go to the next page : ") and a recursive loop through the object.

+1
source share

Here is my trick: I use screen or byobu on Linux and then F7, which allows me to scroll back and forth through everything I want. Again, I rarely use more in bash when I get much more from less . ;-) less more than more . Very puny.

Another nice multi-platform option is RStudio, which allows you to quickly and easily scroll back.

+1
source share

I rarely look at the entire dataset in R. When I do this, I try to push it to the CSV, and then use the spreadsheet to view it. To just view the output in short pieces, I use head() or tail()

Of course, my colleagues asked me if I’m tail(head)) (yes, the head in the tail is joking, it never gets old for me)

If you want to look only at the vector, you can do this:

 system("more", input=as.character(rnorm(1000))) 

This does not work very well with data frames or matrices, because a character vector is required for the input parameter.

change

for data frames and matrices, you can combine my "export to CSV" and the command line function more as follows:

 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) 
0
source share

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 have not ruined 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's better to do something based on edit (and string connections).

0
source share

All Articles