-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest-writeData.R
68 lines (54 loc) · 2.01 KB
/
test-writeData.R
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
test_that("write_formula", {
set.seed(123)
df <- data.frame(C = rnorm(10), D = rnorm(10))
# array formula for a single cell
exp <- structure(
list(row_r = "2", c_r = "E", c_s = "_openxlsx_NA_",
c_t = "_openxlsx_NA_", c_cm = "_openxlsx_NA_",
c_ph = "_openxlsx_NA_", c_vm = "_openxlsx_NA_",
v = "_openxlsx_NA_", f = "SUM(C2:C11*D2:D11)",
f_t = "array", f_ref = "E2:E2",
f_ca = "_openxlsx_NA_", f_si = "_openxlsx_NA_",
is = "_openxlsx_NA_", typ = NA_character_, r = "E2"),
row.names = "3", class = "data.frame")
# write data add array formula later
wb <- wb_workbook()
wb <- wb_add_worksheet(wb, "df")
wb$add_data("df", df, startCol = "C")
write_formula(wb, "df", startCol = "E", startRow = "2",
x = "SUM(C2:C11*D2:D11)",
array = TRUE)
cc <- wb$worksheets[[1]]$sheet_data$cc
got <- cc[cc$row_r == "2" & cc$c_r == "E",]
expect_equal(exp[1:16], got[1:16])
rownames(exp) <- "31"
# write formula first add data later
wb <- wb_workbook()
wb <- wb_add_worksheet(wb, "df")
write_formula(wb, "df", startCol = "E", startRow = "2",
x = "SUM(C2:C11*D2:D11)",
array = TRUE)
wb$add_data("df", df, startCol = "C")
cc <- wb$worksheets[[1]]$sheet_data$cc
got <- cc[cc$row_r == "2" & cc$c_r == "E",]
expect_equal(exp[1:11], got[1:11])
})
test_that("silent with numfmt option", {
wb <- wb_workbook()
wb$add_worksheet("S1")
wb$add_worksheet("S2")
wb$add_data_table("S1", x = iris)
wb$add_data_table("S2",
x = mtcars, xy = c("B", 3), rowNames = TRUE,
tableStyle = "TableStyleLight9"
)
# [1:4] to ignore factor
expect_equal(iris[1:4], wb_to_df(wb, "S1")[1:4], ignore_attr = TRUE)
expect_equal(iris[1:4], wb_to_df(wb, "S1")[1:4], ignore_attr = TRUE)
# handle rownames
got <- wb_to_df(wb, "S2", rowNames = TRUE)
attr(got, "tt") <- NULL
attr(got, "types") <- NULL
expect_equal(mtcars, got)
expect_equal(rownames(mtcars), rownames(got))
})