File: test-asNetwork.R

package info (click to toggle)
r-cran-intergraph 2.0-4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: sh: 13; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download
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
# From data.frames --------------------------------------------------------

test_that("Disassembling directed network to d.f. and assembling back to network works", {
  l <- asDF(exNetwork)
  g <- asNetwork( l$edges, vertices=l$vertexes)
  expect_identical(g, exNetwork)
} )

test_that("Disassembling undirected network to d.f. and assembling back to network works", {
  l <- asDF(exNetwork2)
  g <- asNetwork( l$edges, vertices=l$vertexes, directed=FALSE)
  expect_identical(g, exNetwork2)
} )

test_that("Disassembling directed igraph to d.f. and assembling back to network: edgecount", {
  l <- asDF(exIgraph)
  g <- asNetwork(l$edges, vertices=l$vertexes)
  expect_equal( network::network.edgecount(g), nrow(l$edges))
} )

test_that("Disassembling directed igraph to d.f. and assembling back to network: vcount", {
  l <- asDF(exIgraph)
  g <- asNetwork(l$edges, vertices=l$vertexes)
  expect_equal( network::network.size(g), nrow(l$vertexes))
} )

test_that("Disassembling undirected igraph to d.f. and assembling back to network: edgecount", {
  l <- asDF(exIgraph2)
  g <- asNetwork(l$edges, vertices=l$vertexes, directed=FALSE)
  expect_equal( network::network.edgecount(g), nrow(l$edges))
} )

test_that("Disassembling undirected igraph to d.f. and assembling back to network: vcount", {
  l <- asDF(exIgraph2)
  g <- asNetwork(l$edges, vertices=l$vertexes, directed=FALSE)
  expect_equal( network::network.size(g), nrow(l$vertexes))
} )




# From igraphs ------------------------------------------------------------

test_that("Directed igraphs via netcompare", {
  res <- netcompare( asNetwork(exIgraph), exIgraph, test=TRUE )
  expect_true(res)
} )

test_that("Undirected igraphs via netcompare", {
  res2 <- netcompare( asNetwork(exIgraph2), exIgraph2, test=TRUE )
  expect_true(res2)
} )

test_that("NAs in igraph vertex labels are copied (bug 1926)", {
  # network with NA on edge attribute
  g <- igraph::graph( c(0,1, 1,2, 2,3, 3,4, 4,2)+1, directed=TRUE)
  igraph::E(g)$label <- c(1,2,3,NA,4)
  # convert to 'network'
  net <- asNetwork(g)
  # warning that NA is converted to "NA"?
  expect_true( is.na( network::get.edge.attribute(net, "label")[4] ) )
} )



# From tibbles ------------------------------------------------------------

test_that("Network is created from edgelist as tibble", {
  edb <- tibble::tibble(
    from = 1:4,
    to = 2:5
  )
  expect_silent(
    net <- asNetwork(edb)
    )
  expect_equal( network::network.size(net), 5)
  expect_equal( network::network.edgecount(net), 4)
})

test_that("Network is created from tibbles", {
  edb <- tibble::tibble(
    from = 1:4,
    to = 2:5
  )
  vdb <- tibble::tibble(
    id = 1:5,
    ch = letters[1:5],
    stringsAsFactors = FALSE
  )
  expect_silent( net <- asNetwork(edb, vertices=vdb))
})