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

johannes rara johannesraja at gmail.com
Sat Sep 19 17:58:27 CEST 2009


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.

-Johannes

2009/9/19 David Winsemius <dwinsemius at comcast.net>:
>
> On Sep 19, 2009, at 10:58 AM, Duncan Murdoch wrote:
>
>> 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 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)
>
> mean> x <- c(0:10, 50)
>
> mean> xm <- mean(x)
>
> mean> c(xm, mean(x, trim = 0.10))
> [1] 8.75 5.50
>
> mean> mean(USArrests, trim = 0.2)
>  Murder  Assault UrbanPop     Rape
>    7.42   167.60    66.20    20.16
>
>> x <- c(0:10, 50)
>
> --
>
> 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