-
Notifications
You must be signed in to change notification settings - Fork 4
/
mully_constructor.R
106 lines (98 loc) · 2.57 KB
/
mully_constructor.R
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
########### Constructor ##################
#' Create an empty multilayered graph
#'
#' @param name The name to be assigned to the graph.
#' @param direct A boolean value, if the graph is directed or not. By default TRUE.
#'
#' @return The created multilayered graph, a mully object.
#'
#' @export
#' @import igraph
#' @examples
#' g = mully("MyFirstMully",direct = FALSE)
mully <- function(name = NA, direct = TRUE) {
#Create the layers Indexation Table
#Vector for the layers indexation
#One row is not enough (in case we delete layers)
layersVect = data.frame(ID = is.numeric(c()),
Name = is.character(c()),
NameLower = is.character(c()),
stringsAsFactors = FALSE)
#Delete first entry F F F
layersVect = layersVect[-1,]
#Create an empty igraph instance
g <- graph.empty(directed = direct)
#Assign the attributes to the igraph instance
if(is.na(name))
name=paste0("mully",as.character(sample(1:100000,1)))
g$name = name
#Last inserted layer number
g$iLayer = 0
#DataFrame that contains the layers
g$layers = layersVect
#name the class
class(g) = c("mully",class(g))
return(g)
}
#' Is this a mully graph?
#'
#' @param g The input graph
#'
#' @return A boolean whether the graph is or not a mully object
#' @export
#' @import igraph
is.mully<-function(g){
if(!is.igraph(g) || is.null(g$layers) || is.null(g$iLayer))
return(FALSE)
return(TRUE)
}
#' Print function
#'
#' @param x The input graph
#' @param ... Other arguments to be passed to \code{print}
#' @return No return value. The given graph is printed
#' @export
#' @import igraph
#' @importFrom utils capture.output
#' @examples
#' g=mully::demo()
#' print(g)
print.mully<-function(x,...){
if(missing(x) || !is.mully(x)){
stop("Invalid Arguments")
}
cat("mully")
if(!is.na(x$name)){
cat(paste(" -- ",x$name))
}
if(dim(x$layers)[1]==0){
cat("\nEmpty Graph")
return()
}
else{
olayers=capture.output(print.data.frame(x$layers))
olayers <- paste(olayers, "\n", sep="")
cat("\n",x$iLayer ,"Layers:\n")
cat(olayers)
}
if(length(V(x))!=0){
nodes=getNodeAttributes(x)
onodes=capture.output(print.data.frame(nodes))
onodes <- paste(onodes, "\n", sep="")
cat("\n",dim(nodes)[1],"Nodes:\n")
cat(onodes)
}
else{
cat("\nNo Nodes\n")
}
if(length(E(x))!=0){
edges=getEdgeAttributes(x)
oedges=capture.output(print.data.frame(edges))
oedges <- paste(oedges, "\n", sep="")
cat("\n",dim(edges)[1],"Edges:\n")
cat(oedges)
}
else{
cat("\nNo Edges")
}
}