mk <- function() { data.frame( Sampling_Date = rep(c("2016-08-01","2016-09-01"), each = 4), Site = "Gosaba", Treatment = "N120", Replication = rep(1:4, 2), Biomass = c(1000, 1100, 900, 1000, 4000, 4200, 3800, 4000), stringsAsFactors = FALSE) } test_that("replicates collapse to mean, sd and n", { ob <- map_obs(mk(), "Sampling_Date", "Biomass", treatment_col = c("Site","Treatment"), rep_col = "Replication") expect_equal(nrow(ob), 2L) expect_equal(ob$value[1], 1000) expect_equal(ob$obs_sd[1], sd(c(1000,1100,900,1000))) expect_equal(ob$obs_n, c(4L, 4L)) }) test_that("several id columns join into one simulation label", { ob <- map_obs(mk(), "Sampling_Date", "Biomass", treatment_col = c("Site","Treatment"), rep_col = "Replication") expect_equal(unique(ob$sim), "Gosaba_N120") expect_true(all(c("Site","Treatment") %in% names(ob))) }) test_that("a single replicate gets NA sd, not zero", { d <- mk()[1, ] ob <- map_obs(d, "Sampling_Date", "Biomass", treatment_col = "Treatment", rep_col = "Replication") expect_equal(ob$obs_n, 1L) expect_true(is.na(ob$obs_sd)) }) test_that("replicate column is guessed and kept out of the treatments", { g <- attr(read_obs_raw(local({ f <- tempfile(fileext = ".csv"); utils::write.csv(mk(), f, row.names = FALSE); f })), "guess") expect_equal(g$rep, "Replication") expect_false("Replication" %in% g$treatment) expect_false("Replication" %in% g$values) expect_true(all(c("Site","Treatment") %in% g$treatment)) }) test_that("error bars are drawn only where an SD exists", { sim <- data.frame(Date = as.Date("2016-01-01") + 0:400, value = 1:401) obs <- data.frame(Date = as.Date(c("2016-03-01","2016-06-01")), value = c(60, 150), obs_sd = c(10, NA)) p <- plot_ts(sim, obs = obs) eb <- Filter(function(l) inherits(l$geom, "GeomErrorbar"), p$layers) expect_length(eb, 1L) expect_equal(nrow(eb[[1]]$data), 1L) }) test_that("no error bar layer when no SD is present at all", { sim <- data.frame(Date = as.Date("2016-01-01") + 0:100, value = 1:101) obs <- data.frame(Date = as.Date("2016-02-01"), value = 32) p <- plot_ts(sim, obs = obs) expect_length(Filter(function(l) inherits(l$geom, "GeomErrorbar"), p$layers), 0L) })