pattern in history

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

pattern in history

Romain François
Hi,

Sometimes I need to consult the history of commands that are matching a
regex, so I modified the utils::history function for that purpose. I
found it useful.

I append the code ( I only added the two lines with #**)

Romain.


history2 <-
function (pattern="", max.show = 25, reverse = FALSE, unique =
pattern!="", ...)
{
    file1 <- tempfile("Rrawhist")
    savehistory(file1)
    rawhist <- scan(file1, what = "", quiet = TRUE, sep = "\n")
    rawhist <- rawhist[grep(pattern, rawhist, ...)] #**
    if(unique) rawhist <- unique(rawhist) #**
    unlink(file1)
    nlines <- length(rawhist)
   
    inds <- max(1, nlines - max.show):nlines
    if (reverse)
        inds <- rev(inds)
    file2 <- tempfile("hist")
    write(rawhist[inds], file2)
    file.show(file2, title = "R History", delete.file = TRUE)
}


--
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques
mixmod 1.7 is released : http://www-math.univ-fcomte.fr/mixmod/index.php
+---------------------------------------------------------------+
| Romain FRANCOIS - http://francoisromain.free.fr               |
| Doctorant INRIA Futurs / EDF                                  |
+---------------------------------------------------------------+

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: pattern in history

Brian Ripley
That's an interesting suggestion, but

1) it is not a good idea to change the order of arguments in a function.

2) is the 'unique' argument useful?  I cannot see it being used if
there is no pattern search, nor I do see the merit in showing repeated
lines if I have subselected.

3) like ls(), testing if 'pattern' were missing or NULL would be a better
idea.


On Tue, 11 Apr 2006, Romain Francois wrote:

> Hi,
>
> Sometimes I need to consult the history of commands that are matching a
> regex, so I modified the utils::history function for that purpose. I
> found it useful.
>
> I append the code ( I only added the two lines with #**)
>
> Romain.
>
>
> history2 <-
> function (pattern="", max.show = 25, reverse = FALSE, unique =
> pattern!="", ...)
> {
>    file1 <- tempfile("Rrawhist")
>    savehistory(file1)
>    rawhist <- scan(file1, what = "", quiet = TRUE, sep = "\n")
>    rawhist <- rawhist[grep(pattern, rawhist, ...)] #**
>    if(unique) rawhist <- unique(rawhist) #**
>    unlink(file1)
>    nlines <- length(rawhist)
>
>    inds <- max(1, nlines - max.show):nlines
>    if (reverse)
>        inds <- rev(inds)
>    file2 <- tempfile("hist")
>    write(rawhist[inds], file2)
>    file.show(file2, title = "R History", delete.file = TRUE)
> }
>
>
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: pattern in history

Romain François
Le 11.04.2006 14:12, Prof Brian Ripley a écrit :
> That's an interesting suggestion, but
>
> 1) it is not a good idea to change the order of arguments in a function.
Yes, my mystake. It's because i like to call 'history(something)'
directly and not 'history(pattern=something)'.
I hadn't in mind the possibility to break existing code.
> 2) is the 'unique' argument useful?  I cannot see it being used if
> there is no pattern search, nor I do see the merit in showing repeated
> lines if I have subselected.
>
> 3) like ls(), testing if 'pattern' were missing or NULL would be a
> better idea.
Right.
There is also a problem with the order of the calls.
If I do :

 > ls()
 > l <- mean(rnorm(50))
 > ls()
 > history(pattern="^l")
it prints :
ls()
l <- mean(rnorm(50))

when it should print :
l <- mean(rnorm(50))
ls()

We can use rev twice (like in history3 below), but is it worth it ?

history3 <-
function (max.show = 25, reverse = FALSE, pattern, ...)
{
   file1 <- tempfile("Rrawhist")
   savehistory(file1)
   rawhist <- scan(file1, what = "", quiet = TRUE, sep = "\n")
   if(!missing(pattern)) rawhist <- rev( unique(
rev(rawhist[grep(pattern, rawhist, ...)] ) ) )
   unlink(file1)
   nlines <- length(rawhist)
     inds <- max(1, nlines - max.show):nlines
   if (reverse)
       inds <- rev(inds)
   file2 <- tempfile("hist")
   write(rawhist[inds], file2)
   file.show(file2, title = "R History", delete.file = TRUE)
}

--
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques
mixmod 1.7 is released : http://www-math.univ-fcomte.fr/mixmod/index.php
+---------------------------------------------------------------+
| Romain FRANCOIS - http://francoisromain.free.fr               |
| Doctorant INRIA Futurs / EDF                                  |
+---------------------------------------------------------------+

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: pattern in history

Brian Ripley
On Tue, 11 Apr 2006, Romain Francois wrote:

> Le 11.04.2006 14:12, Prof Brian Ripley a écrit :
>> That's an interesting suggestion, but
>>
>> 1) it is not a good idea to change the order of arguments in a function.
> Yes, my mystake. It's because i like to call 'history(something)' directly
> and not 'history(pattern=something)'.
> I hadn't in mind the possibility to break existing code.
>> 2) is the 'unique' argument useful?  I cannot see it being used if
>> there is no pattern search, nor I do see the merit in showing repeated
>> lines if I have subselected.
>>
>> 3) like ls(), testing if 'pattern' were missing or NULL would be a better
>> idea.
> Right.
> There is also a problem with the order of the calls.
> If I do :
>
>> ls()
>> l <- mean(rnorm(50))
>> ls()
>> history(pattern="^l")
> it prints :
> ls()
> l <- mean(rnorm(50))
>
> when it should print :
> l <- mean(rnorm(50))
> ls()
Well, maybe sorting would be useful here.

BTW, your code also fails (prints NA) if there are not matches.


>
> We can use rev twice (like in history3 below), but is it worth it ?
>
> history3 <-
> function (max.show = 25, reverse = FALSE, pattern, ...)
> {
>  file1 <- tempfile("Rrawhist")
>  savehistory(file1)
>  rawhist <- scan(file1, what = "", quiet = TRUE, sep = "\n")
>  if(!missing(pattern)) rawhist <- rev( unique( rev(rawhist[grep(pattern,
> rawhist, ...)] ) ) )
>  unlink(file1)
>  nlines <- length(rawhist)
>    inds <- max(1, nlines - max.show):nlines
>  if (reverse)
>      inds <- rev(inds)
>  file2 <- tempfile("hist")
>  write(rawhist[inds], file2)
>  file.show(file2, title = "R History", delete.file = TRUE)
> }
>
>
--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel