I've been reading the R source code trying to understand how it paginates help pages (for example ?c
). I think it's using less
, but I haven't been able to find the function that does this. I guess I could do system(gettextf("echo %s | less", my_text))
, but it won't work in Windows.

- 13,328
- 21
- 91
- 149
3 Answers
Have a look at ?page
and ?file.show
:
page(runif(1e5))

- 25,396
- 3
- 68
- 74
-
Thanks. Do you know if there's a way to page text without saving it to a file? – nachocab Jul 06 '13 at 21:01
-
@nochocab: `page` use a temporary file, too. I don't know whether/how you could achieve pagination without a temporary file. – sgibb Jul 06 '13 at 21:10
+1 to @sgibb, page()
is really useful. There are some cases where I want to go with a more complicated solution though. You can also use ?sink in conjunction with ?file.show:
sink(file="tempSink", type="output")
...
# various commands
...
sink()
file.show(file="tempSink", delete.file=TRUE, title="my output")
For example, page()
only displays one output, but you may want to look at several together. I've also noted that sometimes page()
doesn't work, but the above will (I don't know why--it might just be a bug).

- 11,583
- 7
- 60
- 79
-
1It is exactly the same what `page` does if you use `method="print"` (have a look at: `page(page)`). Sure, you have to include your *various commands* into a function. – sgibb Jul 07 '13 at 06:46
Remember that with page()
you need to specify method="print"
to get the readable formatting you saw from the prompt:
> smry = summary(...)
> page(smry) # this is incomprehensible
structure(list(`Response Y1` = structure(list(call = lm(formula = Y1 ~
designmatrix + 0), terms = Y1 ~ designmatrix + 0, residuals = new("VectorSpaceModel",
.Data = structure(c(0.0787047361879546, -1.45136789927732,
...
> page(smry, method="print") # much better
Response Y1 :
Call:
lm(formula = Y1 ~ designmatrix + 0)
Residuals:
[,1]
Min -5.48880
...
If you don't want to wait for the whole darn thing to finish outputting before you get to look at the first line, try this:
> capture.output(smry, file=pipe("less"))
It produces the same result as page(.., method="print")
for me, but the first line comes up immediately rather than after 4 seconds of silent buffering (for this particular example).
Also, sometimes after playing around with capture.output
and friends you find that stuff you enter on the R prompt stops producing output, then you need to call sink()
or something similar.

- 732
- 6
- 16