Skip to content

Commit

Permalink
Merge pull request #88 from animint/dont-rm-non-animint-dir
Browse files Browse the repository at this point in the history
error if animint.js does not exist
  • Loading branch information
tdhock authored Nov 16, 2023
2 parents ce1cc32 + 4b3e420 commit 1e9a837
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 66 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: animint2
Title: Animated Interactive Grammar of Graphics
Version: 2023.11.15
Version: 2023.11.16
URL: https://animint.github.io/animint2/
BugReports: https://github.com/animint/animint2/issues
Authors@R: c(
Expand Down
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Changes in 2023.11.15
# Changes in version 2023.11.16 (PR#88)

- Before creating a new out.dir, do not remove old out.dir, and
instead stop with an error, if out.dir already exists, but
animint.js does not.

# Changes in 2023.11.15 (PR#101)

- New function `animint2pages(viz,"new_github_repo")` for
publishing/sharing animints, replacement for animint2gist, which
Expand Down
4 changes: 4 additions & 0 deletions R/z_animint.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ animint2dir <- function(plot.list, out.dir = NULL,
if(is.null(out.dir)){
out.dir <- tempfile()
}
animint.js <- file.path(out.dir, "animint.js")
if(dir.exists(out.dir) && !file.exists(animint.js)){
stop(animint.js, " does not exist, so not removing out.dir. If you really want to save your animint in out.dir, then please remove that directory entirely")
}
unlink(out.dir, recursive=TRUE)
## Check plot.list for errors
checkPlotList(plot.list)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/helper-HTML.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#'
#' @param plotList A named list of ggplot2 objects
animint2HTML <- function(plotList) {
unlink("animint-htmltest", recursive=TRUE)
res <- animint2dir(plotList, out.dir = "animint-htmltest",
open.browser = FALSE)
remDr$refresh()
Expand Down Expand Up @@ -40,7 +41,6 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) {
testDir <- file.path(testPath, "animint-htmltest")
# if the htmltest directory exists, wipe clean, then create an empty folder
unlink(testDir, recursive = TRUE)
dir.create(testDir)
# start-up remote driver
remotePort <- 4444L
OS <- Sys.info()[['sysname']]
Expand Down
35 changes: 18 additions & 17 deletions tests/testthat/test-compiler-chunk-vars.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
acontext("chunk vars")

test_that("produce as many chunk files as specified", {
viz <- list(iris=ggplot()+
geom_point(aes(Petal.Width, Sepal.Length),
showSelected="Species",
data=iris, chunk_vars="Species",
validate_params = FALSE))
viz <- list(
iris=ggplot()+
geom_point(aes(
Petal.Width, Sepal.Length),
showSelected="Species",
data=iris, chunk_vars="Species",
validate_params = FALSE))
tdir <- tempfile()
dir.create(tdir)
tsv.files <- Sys.glob(file.path(tdir, "*.tsv"))
expect_equal(length(tsv.files), 0)
animint2dir(viz, tdir, open.browser=FALSE)
tsv.files <- Sys.glob(file.path(tdir, "*.tsv"))
expect_equal(length(tsv.files), 3)

viz <- list(iris=ggplot()+
geom_point(aes(Petal.Width, Sepal.Length),
showSelected="Species",
data=iris, chunk_vars=character(), validate_params = FALSE))
viz <- list(
iris=ggplot()+
geom_point(aes(
Petal.Width, Sepal.Length),
showSelected="Species",
data=iris,
chunk_vars=character(), validate_params = FALSE))
tdir <- tempfile()
dir.create(tdir)
tsv.files <- Sys.glob(file.path(tdir, "*.tsv"))
expect_equal(length(tsv.files), 0)
animint2dir(viz, tdir, open.browser=FALSE)
Expand Down Expand Up @@ -103,13 +105,12 @@ for(f in test.paths){
}
du.bytes <- bytes.used(test.paths)
apparent.bytes <- bytes.used(test.paths, apparent.size = TRUE)
byte.df <- data.frame(du.bytes, apparent.bytes,
file.size=file.size(test.paths),
test.paths)

byte.df <- data.frame(
du.bytes, apparent.bytes,
file.size=file.size(test.paths),
test.paths)
test_that("default chunks are at least 4KB", {
tdir <- tempfile()
dir.create(tdir)
tsv.files <- Sys.glob(file.path(tdir, "*.tsv"))
expect_equal(length(tsv.files), 0)
expect_no_warning({
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-compiler-unlink-previous.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ acontext("compiler unlink previous")
data(WorldBank)

out.dir <- file.path(tempdir(), "animint2-wb")
unlink(out.dir, recursive=TRUE)
viz <- list(
scatter=ggplot()+
geom_point(aes(
Expand Down Expand Up @@ -33,3 +34,12 @@ test_that("only two geom.tsv files in out.dir", {
"geom2_point_scatter_chunk1.tsv")
expect_identical(geom.tsv.vec, expected.tsv.vec)
})

test_that("error if dir exists and no animint.js inside", {
animint.js <- file.path(out.dir, "animint.js")
unlink(animint.js)
expect_error({
animint2dir(viz2, out.dir, open.browser=FALSE)
}, "animint.js does not exist, so not removing out.dir. If you really want to save your animint in out.dir, then please remove that directory entirely")
})

72 changes: 26 additions & 46 deletions tests/testthat/test-renderer4-update-axes.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,45 @@ acontext("update_axes")

# Plots with axis updates
mtcars$cyl <- as.factor(mtcars$cyl)

no_updates <- ggplot()+geom_point(aes(mpg, disp,
colour=cyl),
data = mtcars)

no_updates <- ggplot()+geom_point(aes(
mpg, disp,
colour=cyl),
data = mtcars)
update_x <- no_updates+
theme_animint(update_axes=c("x"))
update_y <- no_updates+
theme_animint(update_axes=c("y"))
update_xy <- no_updates+
theme_animint(update_axes=c("x","y"))

viz <- (list(neither=no_updates,
x=update_x,
y=update_y,
both=update_xy))

expect_warning(animint2HTML(viz),
paste("update_axes specified for X axis on plot x",
"but found no geoms with showSelected=singleSelectionVariable,",
"so created a plot with no updates for X axis"))


viz <- list(
neither=no_updates,
x=update_x,
y=update_y,
both=update_xy)
expect_warning(animint2HTML(viz), paste(
"update_axes specified for X axis on plot x",
"but found no geoms with showSelected=singleSelectionVariable,",
"so created a plot with no updates for X axis"))
# We only update axes for single selectors
viz$selector.types = list(cyl="single")

expect_no_warning(info <- animint2HTML(viz))

# We only apply axes updates for numeric data
# This test fails if any column used has non-numeric data
# and does not produce an error
d <- mtcars
d$disp <- as.factor(d$disp)
non_numeric_updates <- ggplot()+geom_point(aes(mpg, disp, colour=cyl),
data = d) +
non_numeric_updates <- ggplot()+geom_point(aes(
mpg, disp, colour=cyl),
data = d) +
theme_animint(update_axes = c("x", "y"))
viz_fac <- list(nonNum = non_numeric_updates)
viz_fac$selector.types = list(cyl="single")
# No error for X(mpg) axis, but one for Y(disp) axis
expect_error(animint2HTML(viz_fac),
paste("'update_axes' specified for 'Y' axis on plot",
"'nonNum' but the column 'disp' is non-numeric.",
"Axes updates are only available for numeric data."))
expect_error(animint2HTML(viz_fac), paste(
"'update_axes' specified for 'Y' axis on plot",
"'nonNum' but the column 'disp' is non-numeric.",
"Axes updates are only available for numeric data."))

# Update selection and get HTML
clickID(c("plot_neither_cyl_variable_8"))
Expand All @@ -61,8 +57,10 @@ info$html_updated2 <- getHTML()
## Test for tick updates

rect_path <- "//svg[@id='plot_%s']//g[contains(@class, '%saxis')]"
all_rect_paths <- lapply(names(viz), sprintf, fmt=rect_path,
c("x","y"))[1:4]
all_rect_paths <- lapply(
names(viz), sprintf, fmt=rect_path,
c("x","y")
)[1:4]

# Take tick diffs for all 4 plots
rect_nodes1 <- sapply(all_rect_paths, getNodeSet, doc=info$html)
Expand Down Expand Up @@ -95,10 +93,8 @@ test_that("axis ticks change when plots are updated",{

## ------------------------------------------------------------------- ##
## Test for grid updates

minor_grid_attr1 <- minor_grid_attr2 <- minor_grid_attr3 <- list()
major_grid_attr1 <- major_grid_attr2 <- major_grid_attr3 <- list()

p_names <- names(viz)[1:4]
for(p.name in p_names){
major_grid_attr1[[p.name]] <- get_grid_lines(info$html, p.name, "major")
Expand Down Expand Up @@ -127,7 +123,6 @@ test_that("major grids are updated",{
tolerance=0.01))
expect_true(unequal(major_grid_attr1$both, major_grid_attr1$neither,
tolerance=0.01))

# no_updates
expect_equal(major_grid_attr2$neither, major_grid_attr1$neither)
expect_equal(major_grid_attr3$neither, major_grid_attr1$neither)
Expand All @@ -141,7 +136,6 @@ test_that("major grids are updated",{
major_grid_attr1$x$vert, tolerance=0.01))
expect_true(unequal(major_grid_attr3$x$vert,
major_grid_attr2$x$vert, tolerance=0.01))

# update_y -> only hor grids are updated
expect_true(unequal(major_grid_attr2$y$hor,
major_grid_attr1$y$hor, tolerance=0.01))
Expand All @@ -151,7 +145,6 @@ test_that("major grids are updated",{
major_grid_attr2$y$hor, tolerance=0.01))
expect_equal(major_grid_attr2$y$vert, major_grid_attr1$y$vert)
expect_equal(major_grid_attr3$y$vert, major_grid_attr1$y$vert)

# update_xy -> both vert and hor grids updated
expect_true(unequal(major_grid_attr2$both$hor,
major_grid_attr1$both$hor, tolerance=0.01))
Expand Down Expand Up @@ -181,11 +174,9 @@ test_that("minor grids are updated",{
tolerance=0.01))
expect_true(unequal(minor_grid_attr1$both, minor_grid_attr1$neither,
tolerance=0.01))

# no_updates
expect_equal(minor_grid_attr2$neither, minor_grid_attr1$neither)
expect_equal(minor_grid_attr3$neither, minor_grid_attr1$neither)

# update_x -> only vert grids are updated
expect_equal(minor_grid_attr2$x$hor, minor_grid_attr1$x$hor)
expect_equal(minor_grid_attr3$x$hor, minor_grid_attr1$x$hor)
Expand All @@ -195,7 +186,6 @@ test_that("minor grids are updated",{
minor_grid_attr1$x$vert, tolerance=0.01))
expect_true(unequal(minor_grid_attr3$x$vert,
minor_grid_attr2$x$vert, tolerance=0.01))

# update_y -> only hor grids are updated
expect_true(unequal(minor_grid_attr2$y$hor,
minor_grid_attr1$y$hor, tolerance=0.01))
Expand All @@ -205,7 +195,6 @@ test_that("minor grids are updated",{
minor_grid_attr2$y$hor, tolerance=0.01))
expect_equal(minor_grid_attr2$y$vert, minor_grid_attr1$y$vert)
expect_equal(minor_grid_attr3$y$vert, minor_grid_attr1$y$vert)

# update_xy -> both vert and hor grids updated
expect_true(unequal(minor_grid_attr2$both$hor,
minor_grid_attr1$both$hor, tolerance=0.01))
Expand All @@ -223,45 +212,37 @@ test_that("minor grids are updated",{

## -------------------------------------------------------------------- ##
## Test for zooming of geoms

## Get ranges of geoms
no_updates_ranges1 <- get_pixel_ranges(info$html_updated1,
"geom1_point_neither")
no_updates_ranges2 <- get_pixel_ranges(info$html_updated2,
"geom1_point_neither")

x_updates_ranges1 <- get_pixel_ranges(info$html_updated1,
"geom2_point_x")
x_updates_ranges2 <- get_pixel_ranges(info$html_updated2,
"geom2_point_x")

y_updates_ranges1 <- get_pixel_ranges(info$html_updated1,
"geom3_point_y")
y_updates_ranges2 <- get_pixel_ranges(info$html_updated2,
"geom3_point_y")

xy_updates_ranges1 <- get_pixel_ranges(info$html_updated1,
"geom4_point_both")
xy_updates_ranges2 <- get_pixel_ranges(info$html_updated2,
"geom4_point_both")

test_that("geoms get zoomed-in upon changing selection", {
# no_updates
expect_true(unequal(no_updates_ranges2$x, no_updates_ranges1$x,
tolerance=0.01))
expect_true(unequal(no_updates_ranges2$y, no_updates_ranges1$y,
tolerance=0.01))

# x_updates
expect_equal(x_updates_ranges2$x, x_updates_ranges1$x)
expect_true(unequal(x_updates_ranges2$y, x_updates_ranges1$y,
tolerance=0.01))

# y_updates
expect_true(unequal(y_updates_ranges2$x, y_updates_ranges1$x,
tolerance=0.01))
expect_equal(y_updates_ranges2$y, y_updates_ranges1$y)

# xy_updates
expect_equal(xy_updates_ranges2$x, xy_updates_ranges1$x)
expect_equal(xy_updates_ranges2$y, xy_updates_ranges1$y)
Expand All @@ -280,7 +261,8 @@ ribbondata <- rbind(cbind(ribbondata, group="low"),
ribbondata[12:22,2:3] <- ribbondata[12:22,2:3]+runif(11, 1, 10)
ribbondata[23:33,2:3] <- ribbondata[12:22,2:3]+runif(11, 1, 10)
ribbon <- ggplot() +
geom_ribbon(data=ribbondata, aes(x=x, ymin=ymin, ymax=ymax, group=group, fill=group), alpha=.5) +
geom_ribbon(data=ribbondata, aes(
x=x, ymin=ymin, ymax=ymax, group=group, fill=group), alpha=.5) +
ggtitle("geom_ribbon") +
theme_animint(update_axes = c("y"))
viz <- list(ribbon=ribbon, selector.types=list(group="single"))
Expand All @@ -290,10 +272,8 @@ expect_no_warning(info <- animint2HTML(viz))
clickID(c("plot_ribbon_group_variable_high"))
Sys.sleep(0.5)
info$html_updated <- getHTML()

minor_grid_attr1 <- major_grid_attr1 <- minor_grid_attr2 <-
major_grid_attr2 <- list()

minor_grid_attr1 <- get_grid_lines(info$html, names(viz)[[1]], "minor")
minor_grid_attr2 <- get_grid_lines(info$html_updated, names(viz)[[1]], "minor")
major_grid_attr1 <- get_grid_lines(info$html, names(viz)[[1]], "major")
Expand Down

0 comments on commit 1e9a837

Please sign in to comment.