Nothing
# WARNING - Generated by {fusen} from dev/flat_import_export.Rmd: do not edit by hand # nolint: line_length_linter.
test_that("plain data.frame writes a single named sheet", {
f <- tempfile(fileext = ".xlsx")
on.exit(unlink(f))
out <- export_xlsx(mtcars, f, sheet_name = "cars")
expect_true(file.exists(f))
expect_identical(unname(out), f)
expect_identical(readxl::excel_sheets(f), "cars")
})
test_that("list input: single workbook with auto-filled names", {
f <- tempfile(fileext = ".xlsx")
on.exit(unlink(f))
export_xlsx(list(a = iris, mtcars), f)
expect_identical(readxl::excel_sheets(f), c("a", "Sheet1"))
})
test_that("list input: directory mode writes one file per element", {
d <- tempfile()
on.exit(unlink(d, recursive = TRUE))
out <- export_xlsx(list(res1 = iris, res2 = mtcars), d)
expect_identical(names(out), c("res1", "res2"))
expect_true(all(file.exists(out)))
})
test_that("sheet names are sanitised and de-duplicated", {
f <- tempfile(fileext = ".xlsx")
on.exit(unlink(f))
long <- strrep("x", 35)
res <- stats::setNames(rep(list(iris), 4),
c("a/b", "a:b", paste0(long, 1), paste0(long, 2)))
export_xlsx(res, f)
sheets <- readxl::excel_sheets(f)
expect_identical(sheets[1:2], c("a_b", "a_b_2"))
expect_true(all(nchar(sheets) <= 31))
expect_false(anyDuplicated(tolower(sheets)) > 0)
})
test_that("file_col + sheet_col round-trip into files and sheets", {
d <- tempfile()
on.exit(unlink(d, recursive = TRUE))
df <- data.frame(excel_name = c("a", "a", "b"),
sheet_name = c("s1", "s2", "s1"),
v = c(1, 2, 3)) # Excel stores numbers as double
out <- export_xlsx(df, d)
expect_identical(basename(out), c("a.xlsx", "b.xlsx"))
expect_identical(readxl::excel_sheets(out[["a"]]), c("s1", "s2"))
back <- as.data.frame(import_xlsx(out))
back <- back[order(back$v), names(df)]
rownames(back) <- NULL
expect_equal(back, df)
})
test_that("NA group values are kept with a warning", {
d <- tempfile()
on.exit(unlink(d, recursive = TRUE))
df <- data.frame(excel_name = c("a", NA), v = 1:2)
expect_warning(out <- export_xlsx(df, d), "NA")
expect_identical(basename(out), c("a.xlsx", "NA.xlsx"))
})
test_that("overwrite = FALSE fails before writing anything", {
d <- tempfile()
on.exit(unlink(d, recursive = TRUE))
dir.create(d)
file.create(file.path(d, "res2.xlsx"))
expect_error(export_xlsx(list(res1 = iris, res2 = mtcars), d, overwrite = FALSE),
"overwrite = FALSE")
expect_false(file.exists(file.path(d, "res1.xlsx")))
})
test_that("invalid inputs give informative errors", {
f <- tempfile(fileext = ".xlsx")
expect_error(export_xlsx(1:3, f), "`data` must be")
expect_error(export_xlsx(list(), f), "empty list")
expect_error(export_xlsx(list(a = 1), f), "Non-conforming: a")
expect_error(export_xlsx(mtcars, c("a.xlsx", "b.xlsx")), "`path`")
expect_error(export_xlsx(mtcars, f, verbose = NA), "`verbose`")
expect_error(export_xlsx(mtcars, tempfile()), "no `excel_name` column")
expect_error(export_xlsx(mtcars, f, file_col = "x", sheet_col = "x"),
"different columns")
expect_error(export_xlsx(mtcars, file.path(tempdir(), "a.xls")), "only writes .xlsx")
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.