[R] How to export the examples in help(something) to a file?

Peng Yu pengyu.ut at gmail.com
Thu Feb 4 23:46:21 CET 2010


On Thu, Feb 4, 2010 at 4:34 PM, Henrik Bengtsson <hb at stat.berkeley.edu> wrote:
> On Thu, Feb 4, 2010 at 9:18 PM, Liaw, Andy <andy_liaw at merck.com> wrote:
>> From: Peng Yu
>>>
>>> On Wed, Feb 3, 2010 at 10:01 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
>>> > Some examples in the help page are too long to be copied
>>> from screen.
>>> > Could somebody let me know some easy way on how to extract
>>> the example
>>> > to a file so that I can play with them?
>>>
>>> I forget to mention. I use a terminal version of R.
>>
>> One possibility is to make a copy of example() to something else, and
>> remove the last line where it source() the .Rex file, and modify it to
>> suit your need.
>
> Here is a version of that:
>
> readExample <- function(...) {
>  exampleL <- utils::example;
>  environment(exampleL) <- new.env();
>  source <- function(zfile, ...) readLines(zfile);
>  exampleL(...);
> } # readExample()
>
>> readExample(diff);
>  [1] "### Name: diff"
>  [2] "### Title: Lagged Differences"
>  [3] "### Aliases: diff diff.default diff.POSIXt diff.Date"
>  [4] "### Keywords: arith"
>  [5] ""
>  [6] "### ** Examples"
>  [7] ""
>  [8] "diff(1:10, 2)"
>  [9] "diff(1:10, 2, 2)"
> [10] "x <- cumsum(cumsum(1:10))"
> [11] "diff(x, lag = 2)"
> [12] "diff(x, differences = 2)"
> [13] ""
> [14] "diff(.leap.seconds)"
> [15] ""
> [16] ""
> [17] ""

Thank you for the code!



More information about the R-help mailing list