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

Gabor Grothendieck ggrothendieck at gmail.com
Sun Sep 20 06:09:34 CEST 2009


OK.  I've combined both approaches into a single process.source()
function.  Just place the mixed R code and output in the clipboard
and run:

  source.commands()

it will 1. first display clipboard in a suitable manner for pasting into
an r-help post and then 2. execute it.

Thus you can either copy the code from an r-help post into the
clipboard and run the command or you can copy it from earlier
in your session and run the same command.  Just ignore whichever
of the two outputs you don't need (or use the action= argument
to select the appropriate one).

# If clipboard contains R source statements prefaced by
# > or + (or similar prefaces used in example()) together
# with unprefixed output then:
#    source.commands()
# will perform these actions in sequence:
# 1. display the source removing the prefixes and commenting
#    out the output and
# 2. run the source
# Alternately the first argument, action, can select only one of these.
# In all cases it invisibly returns the source that would have been
# displayed under #1.
#
process.source <- function(action = c("both", "run", "show"), echo = TRUE,
   max.deparse.length = Inf, ...) {
   # L <- readLines(pipe("pbpaste")) # use this instead for Mac
   L <- readLines("clipboard")
   rx <- "^[[:blank:]]*[^>+[:blank:]]*[>+]"
   is.cmd <- grepl(rx, L)
   L[is.cmd] <- gsub(paste(rx, "?"), "", L[is.cmd])
   L[!is.cmd] <- paste("#", L[!is.cmd])
   action <- match.arg(action)
   if (action != "run") for(el in L) cat(el, "\n")
   if (action == "both") cat("##################################\n")
   if (action != "show") source(textConnection(L), echo = echo,
         max.deparse.length = max.deparse.length, ...)
   invisible(L)
}

On Sat, Sep 19, 2009 at 12:46 PM, baptiste auguie
<baptiste.auguie at googlemail.com> wrote:
> Neat!
>
> What if, instead, one wanted to format his/her code in the console
> before sending it by email? Any tips for that?
>
> (I proposed something like options("prompt"=" ") above, but got stuck
> with adding a comment # to printed results)
>
> Thanks,
>
> baptiste
>
>
>
>
> 2009/9/19 Gabor Grothendieck <ggrothendieck at gmail.com>:
>> 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.
>>>
>>
>> ______________________________________________
>> 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