tests/dg7d-installed-self-containment.R

script <- tempfile("dg7d-installed-self-containment-", fileext = ".R")

writeLines(c(
    "assert.no.gflow <- function(where) {",
    "    if (\"package:gflow\" %in% search()) {",
    "        stop(sprintf(\"gflow was attached during %s\", where), call. = FALSE)",
    "    }",
    "    if (\"gflow\" %in% loadedNamespaces()) {",
    "        stop(sprintf(\"gflow namespace was loaded during %s\", where), call. = FALSE)",
    "    }",
    "}",
    "",
    "assert.graph <- function(graph, name) {",
    "    if (!is.list(graph)) {",
    "        stop(sprintf(\"%s did not return a list graph\", name), call. = FALSE)",
    "    }",
    "    if (!all(c(\"adj_list\", \"weight_list\") %in% names(graph))) {",
    "        stop(sprintf(\"%s is missing adjacency or weight lists\", name), call. = FALSE)",
    "    }",
    "    if (length(graph$adj_list) == 0L || length(graph$weight_list) == 0L) {",
    "        stop(sprintf(\"%s returned an empty graph\", name), call. = FALSE)",
    "    }",
    "}",
    "",
    "assert.no.gflow(\"startup\")",
    "library(dgraphs)",
    "assert.no.gflow(\"library(dgraphs)\")",
    "",
    "X <- matrix(c(",
    "    0.0, 0.0,",
    "    0.2, 0.0,",
    "    0.4, 0.0,",
    "    0.0, 0.3,",
    "    0.2, 0.3,",
    "    0.4, 0.3",
    "), ncol = 2, byrow = TRUE)",
    "",
    "mknn <- create.mknn.graph(X, k = 2, prune.method = \"none\")",
    "assert.graph(mknn, \"create.mknn.graph\")",
    "assert.no.gflow(\"create.mknn.graph\")",
    "",
    "rknn <- create.rknn.graph(X, type = \"fixed\", radius = 0.31)",
    "assert.graph(rknn, \"create.rknn.graph\")",
    "assert.no.gflow(\"create.rknn.graph\")",
    "",
    "adaptive <- suppressWarnings(create.adaptive.radius.graph(",
    "    X,",
    "    k.scale = 2,",
    "    graph.detail = \"minimal\"",
    "))",
    "assert.graph(adaptive, \"create.adaptive.radius.graph\")",
    "assert.no.gflow(\"create.adaptive.radius.graph\")",
    "",
    "sknn <- create.sknn.graph(X, k = 2, connect.components = TRUE)",
    "assert.graph(sknn, \"create.sknn.graph\")",
    "assert.no.gflow(\"create.sknn.graph\")",
    "",
    "iknn <- create.single.iknn.graph(",
    "    X,",
    "    k = 2,",
    "    prune.method = \"none\",",
    "    verbose = FALSE",
    ")",
    "assert.graph(iknn, \"create.single.iknn.graph\")",
    "assert.no.gflow(\"create.single.iknn.graph\")",
    "",
    "geodesic.iknn <- create.geodesic.iknn.graph(mknn, k = 1)",
    "assert.graph(geodesic.iknn, \"create.geodesic.iknn.graph\")",
    "assert.no.gflow(\"create.geodesic.iknn.graph\")",
    "",
    "distances <- graph.geodesic.distances(mknn)",
    "if (!is.matrix(distances) || !all(dim(distances) == c(nrow(X), nrow(X)))) {",
    "    stop(\"graph.geodesic.distances returned an unexpected shape\", call. = FALSE)",
    "}",
    "assert.no.gflow(\"graph.geodesic.distances\")",
    "",
    "edge.count <- count.edges(mknn$adj_list)",
    "if (!is.numeric(edge.count) || length(edge.count) != 1L || edge.count <= 0) {",
    "    stop(\"count.edges returned an unexpected value\", call. = FALSE)",
    "}",
    "assert.no.gflow(\"count.edges\")",
    "",
    "spectrum <- graph.spectrum(mknn$adj_list, nev = 3)",
    "if (!is.list(spectrum) || !all(c(\"evalues\", \"evectors\") %in% names(spectrum))) {",
    "    stop(\"graph.spectrum returned an unexpected result\", call. = FALSE)",
    "}",
    "assert.no.gflow(\"graph.spectrum\")"
), script)

cmd <- file.path(R.home("bin"), "Rscript")
output <- tempfile("dg7d-installed-self-containment-output-")
status <- system2(
    cmd,
    c("--vanilla", script),
    stdout = output,
    stderr = output
)

if (!identical(status, 0L)) {
    cat(readLines(output, warn = FALSE), sep = "\n")
    stop("DG7d installed-package self-containment check failed.",
         call. = FALSE)
}

Try the dgraphs package in your browser

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

dgraphs documentation built on Aug. 21, 2026, 1:08 a.m.