File: test-data_relocate.R

package info (click to toggle)
r-cran-datawizard 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: sh: 13; makefile: 2
file content (126 lines) | stat: -rw-r--r-- 3,264 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
test_that("data_relocate works as expected", {
  expect_error(
    data_relocate(iris, select = "Species", before = 2, after = 3),
    "You must supply only one of `before` or `after`."
  )

  expect_error(
    data_relocate(iris, select = "Species", before = 10),
    "No valid position defined in `before`."
  )

  expect_error(
    data_relocate(iris, select = "Species", after = 10),
    "No valid position defined in `after`."
  )

  expect_named(
    data_relocate(iris, select = "Species", before = "Sepal.Length"),
    c("Species", "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
  )
  expect_named(
    data_relocate(iris, select = "Species", before = "Sepal.Width"),
    c("Sepal.Length", "Species", "Sepal.Width", "Petal.Length", "Petal.Width")
  )

  expect_named(
    data_relocate(iris, select = "Sepal.Width", after = "Species"),
    names(data_relocate(iris, select = "Sepal.Width", after = -1))
  )

  expect_named(
    data_relocate(iris, select = c("Species", "Petal.Length"), after = "Sepal.Width"),
    names(data_relocate(iris, select = c("Species", "Petal.Length"), after = 2))
  )
})


test_that("data_relocate select-helpers", {
  expect_identical(
    colnames(data_relocate(iris, select = starts_with("Sepal"), after = 5)),
    colnames(iris[c(3:5, 1:2)])
  )
  expect_identical(
    colnames(data_relocate(iris, select = 1:2, after = 5)),
    colnames(iris[c(3:5, 1:2)])
  )
  expect_identical(
    colnames(data_relocate(iris, select = -1)),
    colnames(iris[c(2:5, 1)])
  )
  expect_identical(
    colnames(data_relocate(iris, select = Species, after = 1)),
    colnames(iris[c(1, 5, 2:4)])
  )
  expect_identical(
    colnames(data_relocate(iris, select = ~ Sepal.Width + Species)),
    colnames(iris[c(2, 5, 1, 3:4)])
  )
  expect_identical(
    colnames(data_relocate(iris, select = starts_with("sepal"), after = 5)),
    colnames(iris)
  )
  expect_identical(
    colnames(data_relocate(iris, select = starts_with("sepal"), after = 5, ignore_case = TRUE)),
    colnames(iris[c(3:5, 1:2)])
  )
})


# preserve attributes --------------------------

test_that("data_relocate preserves attributes", {
  skip_if_not_installed("parameters")

  m <- lm(Sepal.Length ~ Species, data = iris)
  out <- parameters::parameters(m)
  a1 <- attributes(out)

  out2 <- data_relocate(out, 4:6)
  a2 <- attributes(out2)

  # attributes may not be in the same order
  expect_true(all(names(a1) %in% names(a2)))
  expect_identical(length(a1), length(a2))
})


# select helpers ------------------------------
test_that("data_relocate regex", {
  expect_identical(
    names(data_relocate(mtcars, select = "pg", regex = TRUE, after = "carb"))[11],
    "mpg"
  )
})


# fuzzy matching ------------------------------
out <- data.frame(
  Parameter = "Test",
  Median = 0.5,
  CI_low = 0.4,
  CI_high = 0.6,
  pd = 0.97,
  Rhat = 0.99,
  ESS = 1000,
  log_BF = 3,
  stringsAsFactors = FALSE
)

test_that("data_relocate misspelled", {
  # close match
  expect_error(
    data_relocate(out, "pd", before = "BF"),
    "log_BF"
  )
  # close multiple matches
  expect_error(
    data_relocate(out, "pd", before = "CIl"),
    "CI_low"
  )
  # not even close
  expect_error(
    data_relocate(out, "pd", before = "xyz"),
    "misspelled"
  )
})