RISE <- function(X1, X2, sim.fun = function(x, ...) -as.matrix(stats::dist(x, ...)), K = 10, 
                 rank.type = "RgNN", n.perm = 0, dist.args = NULL, seed = NULL) {
  
  if(!requireNamespace("GraphRankTest", quietly = TRUE)) {
    stop("Package \"GraphRankTest\" required for using method RISE().")
  }
  if(!is.null(seed)) {
    set.seed(seed)
  }
  stopifnot(n.perm >= 0)
  if(!(inherits(X1, "matrix") | inherits(X1, "data.frame"))) {
    stop("X1 must be provided as a data.frame or matrix.")
  }
  if(!(inherits(X2, "matrix") | inherits(X2, "data.frame"))) {
    stop("X1 must be provided as a data.frame or matrix.")
  }
  dname <- c(deparse1(substitute(X1)), deparse1(substitute(X2)))
  if(anyNA(X1)) {
    warning(paste0("Missing values in ", dname[1], " were omitted from the calculation."))
    X1 <- na.omit(X1)
  }
  if(anyNA(X2)) {
    warning(paste0("Missing values in ", dname[2], " were omitted from the calculation."))
    X2 <- na.omit(X2)
  } 
  if(ncol(X1) != ncol(X2)) {
    stop("All datasets must have the same number of variables.")
  }
  
  colnames(X1) <- colnames(X2) <- paste0("X", 1:ncol(X1))
  n1 <- nrow(X1)
  n2 <- nrow(X2)
  
  sims <- do.call(sim.fun, c(list(rbind(X1, X2)), dist.args))
  
  res <- GraphRankTest::RISE(S = sims, sample1ID = 1:n1, 
                             sample2ID = (n1+1):(n1+n2), 
                             k = K, rank.type = rank.type, 
                             perm = n.perm)
  
  
  stat <- res$test.statistic
  names(stat) <- "chi^2"
  param <- 2
  names(param) <- "df"
  if(n.perm > 0) {
    pval <- res$pval.perm
  } else {
    pval <- res$pval.approx
  }
  
  res <- list(statistic = stat, 
              parameter = param,
              p.value = pval, 
              estimate = NULL,
              alternative = paste0("The distributions of ", paste0(dname, collapse = " and "), 
                                   " are unequal."), 
              method = paste0(ifelse(n.perm > 0, "Permutation ", "Approximative "), 
                              "Rank In Similarity Graph Edge-count two-sample test (RISE)"), 
              data.name = paste0(dname, collapse = " and "))
  class(res) <- "htest"
  return(res)
}