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

Gabor Grothendieck ggrothendieck at gmail.com
Sat Sep 19 18:36:57 CEST 2009


Combining the code posted by myself, Duncan and David we have:

# Usage: copy code from r-help to clipboard, then in R enter this:
#   source.commands()
#
source.commands <- function(echo = TRUE, max.deparse.length = Inf, ...) {
   # L <- readLines(pipe("pbpaste")) # use this instead for Mac
   L <- readLines("clipboard")
   L <- grep("^[[:blank:]]*[^>+[:blank:]]*[>+]", L, value = TRUE)
   L <- sub("^[[:blank:]]*[^>+[:blank:]]*[>+] ?", "", L)
   source(textConnection(L), echo = echo,
      max.deparse.length = max.deparse.length, ...)
}

It might be possible to automate the check for Mac using .Platform$GUI


On Sat, Sep 19, 2009 at 12:08 PM, David Winsemius
<dwinsemius at comcast.net> wrote:
>
> On Sep 19, 2009, at 11:58 AM, johannes rara wrote:
>
>> Thanks for the responses.
>>
>> I think that the best way to avoid lots of hassle is that people
>> copy-paste their solutions from their code editor, NOT from R console.
>> For example, I usually save those solutions for my code archive, and
>> if I want to run these later on (using Tinn-R), I have to parse ">"
>> and "+" marks anyway.
>
> I agree entirely but trying to change posting behavior appears to be a
> difficult exercise. It would also be much preferred if people would learn to
> post the output of dput on an object, rather than what is displayed on the
> console when the object is print()ed.
>
> --
> David.
>>
>> -Johannes
>>
>> 2009/9/19 David Winsemius <dwinsemius at comcast.net>:
>>>
>>> On Sep 19, 2009, at 10:58 AM, Duncan Murdoch wrote:
>>>
>>>
> snip
>>>>
>>>>
>>>> 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 Macs (and possibly other *NIXen) the equivalent to reading from the
>>> "clipboard" is: pipe("pbpaste")
>>>
>>> Testing shows that a simple modification after defining CleanTranscript
>>> produces no error on the example above:
>>>
>>>> source(textConnection(CleanTranscript(readLines(pipe("pbpaste")))),
>>>
>>> +        echo = TRUE, max.deparse.length=Inf)
>>>
>>>> example(mean)
>
>>
> snip
> ----
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> 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