R/method_registration.R

Defines functions register_rcpp_net_methods

Documented in register_rcpp_net_methods

#' Register setter methods for Rcpp net objeccts
#' @name register_rcpp_net_methods
#' @return no return value
register_rcpp_net_methods <- function() {
  if (!methods::isClass("Rcpp_DirectedNet")) return()
  if (!methods::isClass("Rcpp_UndirectedNet")) return()
  
  setMethod("[", c("Rcpp_DirectedNet"),
            function(x, i, j, ..., maskMissing=TRUE, drop=TRUE)
            {
              x$`[`(i,j,maskMissing)
            })
  
  setMethod("[", c("Rcpp_UndirectedNet"),
            function(x, i, j, ..., maskMissing=TRUE, drop=TRUE)
            {
              x$`[`(i,j,maskMissing)
            })
  
  setMethod("[<-", c("Rcpp_DirectedNet"),
            function(x, i, j, ..., value)
            {
              if(is.vector(value)){
                if(length(value)==length(i) && length(j)==1)
                  value <- as.matrix(as.logical(value))
                else if(length(value)==length(j) && length(i)==1)
                  value <- t(as.matrix(as.logical(value)))
                else
                  stop("invalid assignment")
              }
              x$`[<-`(i,j,value)
              x
            })
  
  setMethod("[<-", c("Rcpp_UndirectedNet"),
            function(x, i, j, ..., value)
            {
              if(is.vector(value)){
                if(length(value)==length(i) && length(j)==1)
                  value <- as.matrix(as.logical(value))
                else if(length(value)==length(j) && length(i)==1)
                  value <- t(as.matrix(as.logical(value)))
                else
                  stop("invalid assignment")
              }
              x$`[<-`(i,j,value)
              x
            })
}

Try the ernm package in your browser

Any scripts or data that you put into this service are public.

ernm documentation built on Aug. 8, 2025, 7:33 p.m.