Equivalent to unix less command within R console
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")
Pause print of large object when it fills the screen
Here is a quick and dirty more
function:
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 chunks of lines (default to 20, but you can change that). You need to press 'enter' to move on to the next chunk. Only 'Enter' will work, you can't just use the space bar or other keys like other programs and it does not have options for searching, going back, etc.
You can try it with a command like:
more( rnorm(250) )
Here is an expanded version that will quit if you type 'q' or 'Q' (or anything starting with either) then press 'Enter', it will print out the last lines
rows of the output if you type 'T' then enter, and if you type a number it will jump to that decile through the output (e.g. typing 5 will jump to half way through, 8 will be 80% of the way through). Anything else and it will continue.
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)
}
Show special characters in Unix while using 'less' Command
less will look in its environment to see if there is a variable named LESS
You can set LESS in one of your ~/.profile (.bash_rc, etc, etc) and then anytime you run less
from the comand line, it will find the LESS.
Try adding this
export LESS="-CQaix4"
This is the setup I use, there are some behaviors embedded in that may confuse you, so you can find out about what all of these mean from the help function in less
, just tap the 'h' key and nose around, or run less --help
.
Edit:
I looked at the help, and noticed there is also an -r
option
-r -R .... --raw-control-chars --RAW-CONTROL-CHARS
Output "raw" control characters.
I agree that cat
may be the most exact match to your stated needs.
cat -vet file | less
Will add '$' at end of each line and convert tab char to visual '^I'.
cat --help
(edited)
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
I hope this helps.
Execute less from command-line PHP w/ Scrolling
You could use proc_open
to feed input to and get output back from a process via pipes. However, it doesn't seem like less allows for user interaction via pipes as it basically degrades to a cat
command. Here's my first (failed) approach:
<?php
$dspec = array(
0 = array('pipe', 'r'), // pipe to child process's stdin
1 = array('pipe', 'w'), // pipe from child process's stdout
2 = array('file', 'error_log', 'a'), // stderr dumped to file
);
// run the external command
$proc = proc_open('less name_of_file_here', $dspec, $pipes, null, null);
if (is_resource($proc)) {
while (($cmd = readline('')) != 'q') {
// if the external command expects input, it will get it from us here
fwrite($pipes[0], $cmd);
fflush($pipes[0]);
// we can get the response from the external command here
echo fread($pipes[1], 1024);
}
fclose($pipes[0]);
fclose($pipes[1]);
echo proc_close($proc);
I guess for some commands this approach might actually work - and there are some examples in the php manpage for proc_open
that might be helpful to look over - but for less
, you get the whole file back and no possibility for interaction, maybe for reasons mentioned by Viper_Sb's answer.
...But it seems easy enough to simulate less
if that's all you need. For example, you could read the output of the command into an array of lines and feed it in bite-sized chunks:
<?php
$pid = popen('cat name_of_file_here', 'r');
$buf = array();
while ($s = fgets($pid, 1024))
$buf[] = $s;
pclose($pid);
for ($i = 0; $i < count($buf)/25 && readline('more') != 'q'; $i++) {
for ($j = 0; $j < 25; $j++) {
echo array_shift($buf);
}
}
Equivalent of 'more' or 'less' command in Powershell?
Well... There is "more", which is more or less (...) the same you'd expect from other platforms. Try the following example:
dir -rec | more
Set default help type for the help system in R for Windows
To set the default for help_type
option:
> options (help_type = "text")
> getOption ("help_type")
[1] "text"
And, to set the console as the pager instead of R's internal one:
> options (pager = "console")
> getOption ("pager")
[1] "console"
You can even set a particular command as the pager. For example, if you want to use unix's less
program provided by cygwin or msys2 or git for Windows:
> options (pager = "less")
> getOption ("pager")
[1] "less"
N.B. Make sure that less
is in your path
. Otherwise, you would have to provide the full path of it:
> options (pager = "C:\\Program Files\\Git\\usr\\bin\\less.exe")
Where the less
program is situated in C:\Program Files\Git\usr\bin\
.
See ?options
and ?file.show
for details.
Related Topics
Finding 2 & 3 Word Phrases Using R Tm Package
Referring to Data.Table Columns by Names Saved in Variables
How to Directly Select the Same Column from All Nested Lists Within a List
Using Dynamic Column Names in 'Data.Table'
Removing Display of Row Names from Data Frame
Duplicate 'Row.Names' Are Not Allowed Error
Legend Placement, Ggplot, Relative to Plotting Region
What Are the Double Colons (::) in R
Add Objects to Package Namespace
Inst and Extdata Folders in R Packaging
R: Assign Variable Labels of Data Frame Columns
Using Substitute to Get Argument Name
Multiple Graphs in One Canvas Using Ggplot2
Mean of a Column in a Data Frame, Given the Column's Name