[R] How to avoid copy-paste when copying code from this list

(Ted Harding) Ted.Harding at manchester.ac.uk
Sat Sep 19 11:46:06 CEST 2009


On 19-Sep-09 08:48:45, johannes rara wrote:
> The R help mailing list posting guide
> 
> http://www.r-project.org/posting-guide.html
> 
> suggests to give an example in this form
> 
> ...snip...
> f I have a matrix x as follows:
>   > x <- matrix(1:8, nrow=4, ncol=2,
>                 dimnames=list(c("A","B","C","D"), c("x","y"))
>   > x
>     x y
>   A 1 5
>   B 2 6
>   C 3 7
>   D 4 8
>   >
> ...snip...
> 
> Would it be reasonable to consider changing this guide about this
> matter?

Yes, I think there is a case for a change. I must have read the
above myself, once, but ignored it later for the reasons I gave
below. In any case, that citation is not in the context of advice
about "how to format R code when posting" (there is no such advice
explicitly given in the posting-guide), but in the context that it
can be helpful to provide an example:

  "Examples: Sometimes it helps to provide a small example that
   someone can actually run. For example:"

(then the example above). Possibly, people may tend to read that
example as if it were advice on formatting the code.

So maybe the change which could be helpful in the present context
would be to follow the above example with a section which advised
on how to format the code for examples which "someone can actually
run" (they cannot "actually run" the code as given in the example).

In other words, on the lines of

  When posting R code for examples, when this is copied from an R
  console remove any command prompts ">" and continuation prompts
  "+" from the code as it appears on the R console, and precede
  each line of R output, messages, etc., with "#" (to make it a
  "comment", so that if the code is copy-pasted from R-help into an
  R console it will work as-is without the need for further editing.
  Example:
  [...]

What do other people (in particular the maintainers of the posting
guide) think?

Some might argue that the ">" and "+" prompts serve to mark the
presence of R code and distinguish it from message text. But then
the result is something that nobody "can actually run". I think,
myself, that (especially with indenting of the code by a couple
of spaces) the format I describe is clearly enough distinguished.

Ted.

> 2009/9/19 Ted Harding <Ted.Harding at manchester.ac.uk>:
>> On 19-Sep-09 08:00:18, Cedrick W. Johnson wrote:
>>> At least in windows, if you right click directly in the r console,
>>> there's a command for 'Paste commands only' which may be one
>>> solution...
>>> Not sure about other platforms..
>>>
>>> hth
>>> c
>>
>> It was precisely for this kind of reason that, when including
>> R code in postings to the list, I took to formatting it in the
>> following kind of way:
>>
>> _a <- 1:10
>> _a
>> _# [1] _1 _2 _3 _4 _5 _6 _7 _8 _9 10
>>
>> _a[1:5]
>> _# [1] 1 2 3 4 5
>>
>> In this way, any R commands copy-pasted into R will work as-is,
>> anything else is a comment and will not interfere. I notice that
>> some other people also post their code in this way.
>>
>> I recommend it to all! If the code has been copy-pasted into the
>> email from an R console, then of course the ">" prompts will be
>> there. But then I just edit these out of the email. A bit more
>> trouble for me, but a lot less trouble for others.
>>
>> For instance, if someone had posted the above as copied from the
>> R console in its original form
>>
>>> a <- 1:10
>>> a
>> _[1] _1 _2 _3 _4 _5 _6 _7 _8 _9 10
>>
>>> a[1:5]
>> [1] 1 2 3 4 5
>>
>> and I wanted to try it out, then I would either have to re-open the
>> email in "edit" mode so as to edit the email itself, or else
>> copy-paste
>> the above into a text-edit window[*] and pre-edit it there before
>> copying into R.
>>
>> [*] I would be using 'vim' in a Linux xterm. Removal of the "> "
>> prompts (or "+ " continuation prompts) from a long series of commands
>> is relatively easy: Just higlight a column-block of the first two
>> columns, then press "d" to delete them. But you would first need to
>> enter " _# " for other stuff by hand.
>>
>> Best wishes to all,
>> Ted.
>>
>>>
>>> johannes rara wrote:
>>>> Hi,
>>>>
>>>> How do you people avoid copy-pasting and manual editing of the code
>>>> posted in this list? I mean that if some one post a solution for an
>>>> answer like this:
>>>>
>>>>
>>>>> a <- 1:10
>>>>> a
>>>>>
>>>> _[1] _1 _2 _3 _4 _5 _6 _7 _8 _9 10
>>>>
>>>>> a[1:5]
>>>>>
>>>> [1] 1 2 3 4 5
>>>>
>>>>
>>>> I have to copy-paste it to e.g. Tinn-R and remove "> " part of the
>>>> line to try it in my R. When you keep doing this it gets quite
>>>> annoying. How do you people avoid this (search and replace,
>>>> perhaps?).
>>>> The best way would be to able to send this straight from your e-mail
>>>> reader into R (e.g. from gmail).
>>>>
>>>> -Johannes
>>
>> --------------------------------------------------------------------
>> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
>> Fax-to-email: +44 (0)870 094 0861
>> Date: 19-Sep-09 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Time: 09:33:48
>> ------------------------------ XFMail ------------------------------
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 19-Sep-09                                       Time: 10:46:00
------------------------------ XFMail ------------------------------




More information about the R-help mailing list