File: attrmap.R

package info (click to toggle)
r-cran-intergraph 2.0-4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: sh: 13; makefile: 2
file content (80 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#' Network attribute copying/renaming table
#' 
#' Setting and retrieving rules through which network/edge/vertex attributes
#' are copied or renamed when converting network objects.
#' 
#' Different classes for network data use different attribute names to store
#' the same information. Some of the classes define attributes not used by
#' other classes. This function is used to retrieve or set the rules in which
#' these attributes are copied, renamed or dropped when converting network
#' objects.
#' 
#' The rules are stored in a data frame with the following columns (all of mode
#' character):
#' \describe{
#' \item{type}{type, or level of the attribute, one of "vertex", "edge" or
#' "network"}
#' \item{fromcls}{name of the class which is being coerced}
#' \item{fromattr}{name of the "source" attribute}
#' \item{tocls}{name of the class to which coercion is being done}
#' \item{toattr}{name of the attribute in the result of coercion, or NA}
#' }
#' When converting network \code{x}, of class \code{xclass} to network \code{y}
#' of class \code{yclass} the coercion method looks up the rows in this table
#' for which \code{fromcls=xclass} and \code{tocls=yclass}. If network \code{x}
#' has any of the attributes listed in the \code{fromattr} in the resulting
#' subset of the table then, it is renamed to the corresponding value of
#' \code{toattr}.  If \code{toattr} is \code{NA} the attribute is dropped from
#' the result.
#' 
#' @param newdf data.frame, new set of copy/rename rules, see Details
#'
#' @return If \code{newdf} is NULL, a data frame with the current copy/rename
#' rules. Otherwise \code{newdf} are set as new rules and the old ones are
#' returned invisibly, much like \code{\link{par}}.
#'
#' @seealso This is used by \code{\link{asIgraph}} and \code{\link{asNetwork}}
#'
#' @export
#'
#' @examples
#'
#'# Current values
#'attrmap()
#'
attrmap <- function(newdf=NULL)
{
  cur <- utils::getFromNamespace(".attrmap", "intergraph")
  if( is.null(newdf) )
  {
    # return current
    return(cur)
  } else
  {
    # assign new
    utils::assignInNamespace(".attrmap", newdf, "intergraph")
    # return old invisibly
    invisible(cur)
  }
}

# subset of attrmap depending on type and from/to classes
attrmapmat <- function(from, to, atype, db=attrmap())
{
  i <- with(db, type==atype & fromcls==from & tocls==to)
  as.matrix( db[i, c("fromattr", "toattr")] )
}

# attribute renaming table
.attrmap <- matrix(ncol=5, byrow=TRUE, c(
        "network" , "network" , "directed"     , "igraph"  , NA             , 
        "network" , "network" , "bipartite"    , "igraph"  , NA             , 
        "network" , "network" , "loops"        , "igraph"  , NA             , 
        "network" , "network" , "mnext"        , "igraph"  , NA             , 
        "network" , "network" , "multiple"     , "igraph"  , NA             , 
        "network" , "network" , "n"            , "igraph"  , NA             , 
        "network" , "network" , "hyper"        , "igraph"  , NA             , 
        "vertex"  , "igraph"  , "name"         , "network" , "vertex.names"
        ) )
.attrmap <- as.data.frame(.attrmap, stringsAsFactors=FALSE)
names(.attrmap) <- c("type", "fromcls", "fromattr", "tocls", "toattr")