[R] How to avoid copy-paste when copying code from this list
johannes rara
johannesraja at gmail.com
Sat Sep 19 19:39:44 CEST 2009
I made a python script to parse ">" and "+" marks, if someone is interested:
#------------- start ------------------
#!/Python26/
# -*- coding: utf-8 -*-
import sys
def main(argv):
if len(argv) < 2:
sys.stderr.write("Usage: %s <file.txt>" % (argv[0],))
return 1
f = open(sys.argv[1])
lines = f.readlines()
f.close()
for line in lines:
line = line.strip()
if not (line[:1] == ">" or line[:1] == "+"):
continue
print line[2:]
if __name__ == "__main__":
sys.exit(main(sys.argv))
#------------- end ------------------
And to use it:
~ > python strip_r-help.py code_from_r-help.txt
TP <- function(x){
L <- length(x)
which( ((x[1:(L-2)]<x[2:(N-1)])&(x[2:(L-1)]>x[3:L]))
|((x[1:(L-2)]>x[2:(N-1)])&(x[2:(L-1)]<x[3:L])) ) + 1
}
y<-c(93,93,93,93,93,93,93,93,93,93,93,
93,93,93,93,93,93,93,93,93,93,93)
m<-c(02,02,02,02,02,02,02,02,02,02,02,
02,02,02,02,02,02,02,02,02,02,02)
d<-c(07,08,09,10,11,12,13,14,15,16,17,
18,19,20,21,22,23,24,25,26,27,28)
ix <- TP(count)
rbind(d[ix],count[ix])
~ >
This is not a good solutions, but works especially when there is lots
of code to parse.
-Johannes
2009/9/19 baptiste auguie <baptiste.auguie at googlemail.com>:
> 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