[R] queue implementation?

Spencer Graves spencer.graves at structuremonitoring.com
Tue Sep 7 16:26:47 CEST 2010


        I don't know what you mean by "a queue implementation", but 
consider the following:


install.packages('sos') # if you don't have it already
library(sos)
qu <- ???queue # to search for help pages matching the search term
#found 29 matches;  retrieving 2 pages
summary(qu)
# in 12 packages
qu


       The print method for "qu" (class "findFn") opens a table in a web 
browser showing all 29 matches sorted to put the package with the most 
links first, with hot links to the help pages.  The package also 
includes union and intersection of "findFn" objects, writing an Excel 
file that includes an extended summary by package in one sheet with the 
search results in another, documented in a vignette.


       Hope this helps.
       Spencer Graves
lead author of "sos"


On 9/7/2010 12:56 AM, Barry Rowlingson wrote:
> On Tue, Sep 7, 2010 at 8:11 AM, rajeshj at cse.iitm.ac.in
> <rajeshj at cse.iitm.ac.in>  wrote:
>> Hi,
>>
>> is there a queue implementation in R?
>   Tried RSiteSearch("queue")? There's one in the filehash package that
> uses on-disk databases, which means it's probably fast and can handle
> massive data sets.
>
>   Conversely try this implementation which I hacked up between
> breakfast and being late to work. It uses environments to handle the
> dreaded "R only has pass-by-value" problem, and is probably okay for
> smallish queues. And could be improved in many ways:
>
> queue = function(){
>    e=new.env()
>    q=list()
>    assign("q",q,envir=e)
>    class(e)=c("queue","environment")
>    e
> }
>
> push.queue=function(e,v){
>    q=c(v,get("q",envir=e))
>    assign("q",q,envir=e)
>    v
> }
>
> pop.queue=function(e){
>    q=get("q",envir=e)
>    v=q[[length(q)]]
>    if(length(q)==1){
>        assign("q",list(),e)
>      }else{
>        assign("q",q[1:(length(q)-1)],e)
>      }
>    return(v)
> }
>
> print.queue=function(x,...){
>    print(get("q",envir=x))
> }
>
>   And some usage:
>
>   >  q=queue()
>   >  push.queue(q,34)
>   [1] 34
>   >  push.queue(q,5.44)
>   [1] 5.44
>   >  push.queue(q,"Hello")
>   [1] "Hello"
>   >  pop.queue(q)
>   [1] 34
>   >  pop.queue(q)
>   [1] 5.44
>   >  pop.queue(q)
>   [1] "Hello"
>   >  pop.queue(q)
>   Error in q[[length(q)]] : attempt to select less than one element
>
> Barry
>
> ______________________________________________
> 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.
>


-- 
Spencer Graves, PE, PhD
President and Chief Operating Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567



More information about the R-help mailing list