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

Duncan Murdoch murdoch at stats.uwo.ca
Sat Sep 19 16:58:52 CEST 2009


On 19/09/2009 10:12 AM, Gabor Grothendieck wrote:
> One solution would be to have a portable version of the
> Windows
> 
>    Edit | Paste commands only
> 
> functionality that works on all platforms.
> 
> For example if a command such as this were available:
> 
> source.commands <- function(echo = TRUE, ...) {
>     L <- readLines("clipboard")
>     L <- grep("^[>+] ", L, value = TRUE)
>     L <- gsub("^..", "", L)
>     source(textConnection(L), echo = echo, ...)
> }
> 
> one could just copy the email snippet and the issue this command in
> the R session:
> 
> source.commands()
> 
> Building this directly into source as an option might be nice.

The Windows Rgui is uses slightly more general patterns than those so it 
can take output from running examples, e.g.

 > example(mean)

mean> x <- c(0:10, 50)

mean> xm <- mean(x)

m
[1] 8.75 5.50

mean> mean(USArrests, trim = 0.2)
   Murder  Assault UrbanPop     Rape
     7.42   167.60    66.20    20.16

(and does it in C code, not using grep/gsub; see CleanTranscript in 
https://svn.r-project.org/R/trunk/src/gnuwin32/console.c), but that's 
basically what it does.

However, it's hard to do this in a portable way, because not all systems 
support "clipboard" as the name of the clipboard for cut and paste.  I 
think Unix/Linux systems need to be running X11, and OSX systems don't 
normally support it in the GUI.  So it might makes sense to have a 
portable version of the CleanTranscript function available, but it's 
really up to each different system to connect it up to cut and paste.

Here's a quick version of CleanTranscript, translated to R:

CleanTranscript <- function(lines) {
   lines <- grep("^[[:blank:]]*[^>+[:blank:]]*[>+]", lines, value = TRUE)
   lines <- sub("^[[:blank:]]*[^>+[:blank:]]*[>+] ?", "", lines)
}

So on systems where "clipboard" is supported, executing

source(textConnection(CleanTranscript(readLines("clipboard"))),
         echo = TRUE, max.deparse.length=Inf)

will do something similar to what the Windows "Paste commands only" menu 
option does, but you'd need a different incantation on other systems. 
And even this will sometimes mess up, e.g. it will sometimes 
misinterpret output that contains > or + as input.

Duncan Murdoch

> 
> 
> On Sat, Sep 19, 2009 at 5:46 AM, Ted Harding
> <Ted.Harding at manchester.ac.uk> wrote:
>> 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 ------------------------------
>>
>> ______________________________________________
>> 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.




More information about the R-help mailing list