1
#' Create animation
2
#' 
3
#' Create a gif or mpeg animation from a tmap plot. The free tool ImageMagick is required.
4
#' 
5
#' @param tm tmap object. In order to create a series of tmap plots, which will be the frames of the animation, it is important to set nrow and ncol in \code{\link{tm_facets}}, for otherwise a small multiples plot is generated. Commonly, where one map is shown at a time, both nrow and ncol are set to 1.
6
#' @param filename filename of the video (should be a .gif or .mpg file)
7
#' @param width width of the animation file (in pixels)
8
#' @param height height of the animation file (in pixels)
9
#' @param delay delay time between images (in 1/100th of a second)
10
#' @param loop logical that determined whether the animation is looped, or an integer value that determines how many times the animation is looped.
11
#' @param restart.delay delay time between the loops (in 1/100th of a second)
12
#' @note Not only tmap plots are supported, but any series of R plots.
13
#' @keywords animation
14
#' @example ./examples/tmap_animation.R
15
#' @import tmaptools
16
#' @export
17
tmap_animation <- function(tm, filename="animation.gif", width=NA, height=NA, delay=40, loop = TRUE, restart.delay = 0) {
18 0
	if (!is.numeric(delay) || !(length(delay) == 1L)) stop("delay must be a numeric value", call. = FALSE)
19 0
	if ((!is.numeric(loop) && !is.logical(loop)) || !(length(loop) == 1L)) stop("loop must be a logical or numeric value", call. = FALSE)
20 0
	if (!is.numeric(restart.delay) || !(length(restart.delay) == 1L)) stop("restart.delay must be a numeric value", call. = FALSE)
21

22 0
	syscall <- if (.Platform$OS.type == "unix") system else shell ## macOS == unix
23

24 0
	checkIM <- syscall("convert -version")
25 0
	if (checkIM==0) {
26 0
		program <- "convert"
27 0
	} else {
28
		# For Windows, convert -version does not work if the box "install legacy files (e.g., convert.exe)" hasn't been checked.
29 0
		checkIM2 <- syscall("magick convert -version")
30 0
		if (checkIM2!=0) {
31 0
			stop("Could not find ImageMagick. Make sure it is installed and included in the systems PATH")	
32 0
		} else program <- "magick convert"
33 0
	}
34

35
	# create plots
36 0
	d <- paste(tempdir(), "/tmap_plots", sep="/")
37 0
	dir.create(d, showWarnings = FALSE)
38 0
	suppressMessages(tmap_save(tm, filename = paste(d, "plot%03d.png", sep="/"), width=width, height=height))
39

40 0
	files <- list.files(path = d, pattern = "^plot[0-9]{3}\\.png$")
41 0
	k <- length(files)
42
	
43
	# convert pngs to one gif using ImageMagick
44 0
	if (is.logical(loop)) loop <- 1L - as.integer(loop)
45
	
46 0
	if ((loop == 1) || (restart.delay == 0)) {
47 0
		output <- syscall(paste(program, " -loop ", loop, " -delay ", delay, " ", d, "/*.png \"", filename, "\"", sep=""))
48 0
	} else {
49 0
		part1 <- paste(paste("-delay ", delay, " ", d, "/", files[1L:(k-1)], sep = ""), collapse = " ")
50 0
		part2 <- paste("-delay ", restart.delay, " ", d, "/", files[k], sep = "")
51 0
		output <- syscall(paste(program, " -loop ", loop, " ", part1, " ", part2, " \"", filename, "\"", sep=""))
52 0
	}
53
	
54
	# cleaning up plots
55 0
	unlink(d, recursive = TRUE)
56

57 0
	if (get(".tmapOptions", envir = .TMAP_CACHE)$show.messages) {
58 0
		message("Animation saved to ", suppressWarnings(normalizePath(filename)))
59 0
	}
60
	
61 0
	invisible()	
62
}

Read our documentation on viewing source code .

Loading