Pause printing a large object when the screen is full

Say I want to print a large object in R, for example

x <- 1:2e3

When I print x, the R console fills the screen with its own elements, and since it does not fit everyone on the screen, it will scroll down. Then I need to scroll back to see everything on the screen.

I would like to have a command that prints xand stops when the screen fills up, requiring the user to do something (for example, click enter) to have another screen with the data displayed . What I mean is similar to the MS DOS command dir /p. Is there such a thing?

As @baptiste suggested, this solution, page(x, method = 'print')does not really solve my problem. To be more clear, I would like this solution to not include printing the object into another window, as this would disrupt my workflow. If it didn’t bother me, I would just use View()something like that.

+3
source share
1 answer

Here is a quick and dirty function more:

more <- function(expr, lines=20) {
    out <- capture.output(expr)
    n <- length(out)
    i <- 1
    while( i < n ) {
        j <- 0
        while( j < lines && i <= n ) {
            cat(out[i],"\n")
            j <- j + 1
            i <- i + 1
        }
        if(i<n) readline()
    }
    invisible(out)
}

It will evaluate the expression and print pieces of lines (the default is 20, but you can change that). You need to press 'enter' to go to the next fragment. Only 'enter' will work, you can’t just use the space or other keys like other programs, and it doesn’t have search, return, etc. options.

, :

more( rnorm(250) )

, , "q" "Q" ( -, ), 'Enter', lines , 'T', , , (, 5 8, 80% ). , .

more <- function(expr, lines=20) {
    out <- capture.output(expr)
    n <- length(out)
    i <- 1
    while( i < n ) {
        j <- 0
        while( j < lines && i <= n ) {
            cat(out[i],"\n")
            j <- j + 1
            i <- i + 1
        }
        if(i<n){
            rl <- readline()
            if( grepl('^ *q', rl, ignore.case=TRUE) ) i <- n
            if( grepl('^ *t', rl, ignore.case=TRUE) ) i <- n - lines + 1
            if( grepl('^ *[0-9]', rl) ) i <- as.numeric(rl)/10*n + 1
        }
    }
    invisible(out)
}
+6

All Articles