Skip to content

Documentation on how to put classes and methods in packages with namespace?

3 messages · Jens Henrik Badsberg, Brian Ripley, Uwe Ligges

#
Documentation on how to put classes and methods in packages with namespace?

 How should I define my classes and methods in "dynamicGraph"???

 (That is - Can some one point me to the documentation on
 how to put classes and methods in packages with a namespace? )

 Currently it is done by the code below.

 This gives problems, when a workspace with a "dynmaicGraph" is restored:

 1. run:

     local({a <- CRAN.packages()
     install.packages(select.list(a[,1],,TRUE), .libPaths()[1], available=a)})
     search()
     library(dynamicGraph)
     a <- DynamicGraph(1:10)
     search()
     ls(pos=3, all.names=TRUE)
     history()
     q(save="yes")

 2. run:

     search()
     ls()
     library(dynamicGraph)
     search()
     ls(pos=3, all.names=TRUE)
     b <- DynamicGraph(1:10)
     history()

 The classes and methods of dynamicGraph is not assigned, see the attached "run2.txt",
 and compare with the list of the objects of dynamicGraph in "run1.txt".

 I have experimented with the argument "where" to setClass, etc., see the attached
 "onLoad.R".

 Regards,

 Jens Henrik Badsberg



".onAttach" <-
function (lib, pkg) 
{
    require(tcltk)
}
".onLoad" <-
function (lib, pkg) 
{
    .onLoad.dynamicGraph()
}
".onLoad.dynamicGraph" <-
function () 
{
    library(methods)
    setClass("GraphLatticeProto", representation(vertices = "list", 
        blocks = "list", blockTree = "list", graphs = "list"))
    setClass("CanvasProto", representation(top = "tkwin", canvas = "tkwin", 
        tags = "list", id = "numeric", visibleVertices = "numeric", 
        graphEdges = "list", blockEdges = "list", factorVertices = "list", 
        factorEdges = "list"))
    setClass("NodeProto", representation(color = "character", 
        label = "character", label.position = "numeric"), prototype(color = "black", 
        label = "Label", label.position = c(0, 0, 0)))

    ### .... Many lines deleted ....

    if (!isGeneric("label")) {
        if (is.function("label")) 
            fun <- label
        else fun <- function(object) standardGeneric("label")
        setGeneric("label", fun)
    }
    setMethod("label", "NodeProto", function(object) object at label)
    setGeneric("label<-", function(x, value) standardGeneric("label<-"))
    setReplaceMethod("label", "NodeProto", function(x, value) {
        x at label <- value
        x
    })

    ### .... Many lines deleted ....

}
 <<run1.txt>>  <<run2.txt>>  <<onLoad.R>> 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: run1.txt
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20040316/9729437f/run1.txt
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: run2.txt
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20040316/9729437f/run2.txt
#
See `Writing R Extensions' in 1.9.0 alpha and the stats4 package there as 
an example.  At this point you probably only want to check a package 
against 1.9.0 alpha, anyway.
On Tue, 16 Mar 2004, Jens Henrik Badsberg wrote:

            
[...]
#
Jens Henrik Badsberg wrote:

            
Jens Henrik,

without reading all the lines below, I'd like to point you to the 
"Writing R Extensions" manual of R-devel (1.9.0 alpha), which includes 
some lines on handling S4 classes/methods in Namespaces.

Uwe