draw <- function(expr) { grDevices::pdf(NULL) on.exit(grDevices::dev.off()) force(expr) } test_that("means are drawn at their values with the right error bars and letters", { f <- design_anova(maize_nitrogen, "Yield_t_ha", "Nitrogen_kg_ha", block = "Block") cmp <- compare_means(f, "Nitrogen_kg_ha") r <- draw(plot(cmp)) expect_equal(r$mean, cmp$means$mean) expect_equal(r$upper - r$mean, cmp$means$se) expect_equal(r$letters, cmp$means$group) expect_true(all(r$letter_y > r$upper)) r <- draw(plot(cmp, style = "points", error = "ci")) expect_equal(r$upper - r$mean, stats::qt(0.975, f$residual[["df"]]) * cmp$means$se) r <- draw(plot(cmp, style = "box")) expect_true(all(r$letter_y > tapply(maize_nitrogen$Yield_t_ha, maize_nitrogen$Nitrogen_kg_ha, max))) r <- draw(plot(cmp, sort = TRUE, error = "none")) expect_equal(r$mean, sort(cmp$means$mean, decreasing = TRUE)) expect_equal(r$upper, r$mean) }) test_that("every figure draws without errors and restores the graphical parameters", { mar0 <- graphics::par("mar") f <- design_anova(tomato_factorial, "Yield_kg_plant", c("Variety", "Fertilizer"), block = "Block") mz <- design_anova(maize_nitrogen, "Yield_t_ha", "Nitrogen_kg_ha", block = "Block") sp <- design_anova(split_irrigation, "Yield_t_ha", c("Irrigation", "Variety"), block = "Block", design = "split_rcbd") lt <- design_anova(wheat_latin, "Yield_t_ha", "Treatment", row = "Row", col = "Column") dz <- design_anova(disease_scores, "Severity_score", "Fungicide", block = "Block") expect_silent(draw(plot(mz))) expect_silent(draw(plot(mz, which = 2))) expect_silent(draw(plot(transformations(f)))) expect_silent(draw(plot(trend(mz, "Nitrogen_kg_ha")))) expect_silent(draw(plot_interaction(f, "Fertilizer", "Variety"))) expect_silent(draw(plot_cell_means(f, "Variety", "Fertilizer"))) expect_silent(draw(plot(compare_means(sp, "Variety", by = "Irrigation")))) expect_silent(draw(plot_differences(compare_means(sp, "Irrigation", by = "Variety")))) expect_silent(draw(plot(nonparametric(dz)))) expect_silent(draw(plot_field(lt, value = "residuals"))) r <- draw(plot_ss_partition(sp)) expect_equal(sum(r$pct), 100) expect_equal(graphics::par("mar"), mar0) }) test_that("the field map needs the field coordinates", { f <- design_anova(maize_nitrogen, "Yield_t_ha", "Nitrogen_kg_ha", block = "Block") expect_error(draw(plot_field(f)), "row") d <- maize_nitrogen; d$FieldRow <- d$Block; d$FieldCol <- rep(1:4, 4) f <- design_anova(d, "Yield_t_ha", "Nitrogen_kg_ha", block = "Block") expect_error(draw(plot_field(f, row = "FieldRow", col = "FieldCol")), "pass 'data'") r <- draw(plot_field(f, row = "FieldRow", col = "FieldCol", data = d)) expect_equal(nrow(unique(r[c("row", "col")])), 16) }) test_that("without a working cairo device (macOS without XQuartz) figures and reports still work", { env <- AgriDesign:::.dev_env old <- env$cairo on.exit(assign("cairo", old, envir = env)) AgriDesign:::.cairo_ok() assign("cairo", FALSE, envir = env) f <- design_anova(maize_nitrogen, "Yield_t_ha", "Nitrogen_kg_ha", block = "Block") n0 <- length(grDevices::dev.list()) png <- tempfile(fileext = ".png") save_figure(png, plot(f), width = 8, height = 6, res = 300) b <- readBin(png, "raw", file.size(png)); i <- grepRaw("pHYs", b) expect_equal(round(sum(as.integer(b[(i + 4):(i + 7)]) * 256^(3:0)) * 0.0254), 300) h <- design_report(f, assumptions = FALSE, pairwise = FALSE) expect_gte(attr(h, "figures"), 2) expect_true(grepl("data:image/png;base64", h, fixed = TRUE)) expect_error(save_figure(tempfile(fileext = ".svg"), plot(f)), "cairo") expect_equal(length(grDevices::dev.list()), n0) }) test_that("the PNG resolution chunk is written with a valid CRC", { expect_equal(AgriDesign:::.crc32(charToRaw("123456789")), 3421780262) expect_equal(AgriDesign:::.crc32(charToRaw("IEND")), 2923585666) }) test_that("save_figure writes raster files with their resolution and closes the device", { f <- design_anova(maize_nitrogen, "Yield_t_ha", "Nitrogen_kg_ha", block = "Block") n0 <- length(grDevices::dev.list()) png <- tempfile(fileext = ".png") save_figure(png, plot(compare_means(f, "Nitrogen_kg_ha")), width = 8, height = 6, res = 300) b <- readBin(png, "raw", file.size(png)); i <- grepRaw("pHYs", b) ppm <- sum(as.integer(b[(i + 4):(i + 7)]) * 256^(3:0)) expect_equal(round(ppm * 0.0254), 300) pdf <- tempfile(fileext = ".pdf") save_figure(pdf, plot(f), width = 20, height = 14) expect_true(file.size(pdf) > 1000) expect_equal(length(grDevices::dev.list()), n0) expect_error(save_figure(tempfile(fileext = ".bmp"), plot(f)), "unknown format") expect_equal(length(grDevices::dev.list()), n0) expect_equal(agri_colors(3), c("#2f7d4f", "#c8842a", "#2b7bb9")) })