@@ -91,3 +91,58 @@ summary.network.list <- function (object, stats.print=TRUE,
9191 }
9292 object
9393}
94+
95+ # ' @describeIn network.list A method for concatenating network lists preserving metadata.
96+ # '
97+ # ' @param check_attr Logical: should the attributes of the combined network
98+ # ' lists be checked for consistency. If `TRUE` inconsistencies result in
99+ # ' errors.
100+ # '
101+ # ' @importFrom purrr map
102+ # ' @export
103+ c.network.list <- function (... , check_attr = TRUE ) {
104+ dots <- list (... )
105+
106+ # Merge network lists without attributes
107+ lapply(dots , function (x ) {
108+ attributes(x ) <- NULL
109+ x
110+ }) - > l_networks
111+ rval <- do.call(" c" , l_networks )
112+
113+ # Check attributes
114+ if (check_attr ) {
115+ # Names of attributes to check with `all.equal()`
116+ attr_names <- c(" coefficients" , " control" , " response" ,
117+ " formula" , " constraints" , " reference" )
118+ for (an in attr_names ) {
119+ al <- map(dots , ~ attr(.x , an ))
120+ ok <- all_identical(al , all.equal )
121+ if (! ok ) stop(paste0(" network lists do not have equal values on attribute " , an ))
122+ }
123+
124+ # Check if "stats" have identical columns
125+ l_stats <- map(dots , ~ attr(.x , " stats" ))
126+ ok <- all_identical(
127+ lapply(l_stats , function (x ) colnames(x )),
128+ fun = identical
129+ )
130+ if (! ok ) stop(" network lists do not have identical columns of 'stats' attribute" )
131+ }
132+ # Return the list of networks with attributes merged or taken from the
133+ # first object
134+ structure(
135+ rval ,
136+ class = " network.list" ,
137+ coefficients = attr(dots [[1 ]], " coefficients" ),
138+ control = attr(dots [[1 ]], " control" ),
139+ response = attr(dots [[1 ]], " response" ),
140+ stats = structure(
141+ do.call(" rbind" , map(dots , ~ attr(.x , " stats" ))),
142+ monitored = do.call(" c" , map(dots , ~ attr(attr(.x , " stats" ), " monitored" )))
143+ ),
144+ formula = attr(dots [[1 ]], " formula" ),
145+ constraints = attr(dots [[1 ]], " constraints" ),
146+ reference = attr(dots [[1 ]], " reference" )
147+ )
148+ }
0 commit comments