[R] How to avoid copy-paste when copying code from this list
(Ted Harding)
Ted.Harding at manchester.ac.uk
Sat Sep 19 16:57:10 CEST 2009
On 19-Sep-09 14:12:08, 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.
Neat, Gabor! I just tried it out on the following:
> 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])
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 8.0 10 12.0 13 15 17.0 20.0 23.0 27.0
[2,] 3976.7 3955 3972.8 3961 4008 3981.2 4029.5 3848.3 3936.7
(which was the basis of my response on 17 Sept 2009 to Ogbos Okike's
query about turning-points, though then I edited out the prompts, etc.).
After entering the definition of your function source.commands(),
with the above already executed in an R session running in a Linux
xterm, I highlighted all the above with the mouse (which puts it in
the "clipboard") and then, leaving it highlighted, entered the
command souce.commands().
This was the result (as it appeared on-screen):
> source.commands()
> 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 .... [TRUNCATED]
> 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(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2)
> d <- c(7, 8, 9, 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])
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 8.0 10 12.0 13 15 17.0 20.0 23.0 27.0
[2,] 3976.7 3955 3972.8 3961 4008 3981.2 4029.5 3848.3 3936.7
To check again, I opened Jens Oehlschlägel'e query on 18 Sept.
about "basename", which contains the lines:
> dirname("./.")
[1] "."
> basename("./.")
[1] "."
> dirname("./")
[1] "."
> basename("./")
[1] "."
I highlighted these, went back to the R window, and again issued
your command. Result:
> source.commands()
> dirname("./.")
[1] "."
> basename("./.")
[1] "."
> dirname("./")
[1] "."
> basename("./")
[1] "."
So it seems to work fine!
At any rate for basic use. I think it could have problems in some
cases, such as if the user had executed
cat( rbind(d[ix], count[ix]))
8 3976.7 10 3955 12 3972.8 13 3961 15 4008 17 3981.2 20 4029.5 23 3848.3
27 3936.7>
But, most of the time, it should just work.
Thanks for the tip. One for my .Rprofile, I think!
Ted.
> 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.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 19-Sep-09 Time: 15:57:04
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list