Code returning 'Error: The animation object does not specify a save_animation method'

I'm not sure what I'm doing wrong but my code is returning the error message 'Error: The animation object does not specify a save_animation method', though I've used anim_save ?
I'm very new to R so would appreciate any help!

The script is as follows:

library(tidyverse)
library(janitor)

data_in <- read_csv("https://query.data.world/s/7xr3tfsrw45oawdqhlacd4tposio34")

# Read location codes to use full names
country_codes <- read_delim("http://download.geonames.org/export/dump/countryInfo.txt", delim = "\t",skip = 49) %>%
select(ISO3, Country) %>%
clean_names() %>%
rename(location = iso3)

# indicator, frequency, and flag_codes are superfluous columns
data <- data_in %>%
clean_names() %>%
select(-indicator, -frequency, -flag_codes) %>%
left_join(country_codes) %>%
mutate(country = case_when(
is.na(country) ~ location,
TRUE ~ country)) %>%
select(-location)


# confirm that some NAs for KG_CAP
# Only in early years
data %>%
pivot_wider(names_from = measure,
values_from = value) %>%
arrange(KG_CAP) %>%
View("check_nas")

data_kg <- data %>%
filter(measure == "KG_CAP",
!is.na(measure),
# look at select countrys
country %in% c("Philippines", "United Kingdom",
"Australia", "EU27")) %>%
select(-measure) %>%
group_by(country, time) %>%
summarise(kg_cap = sum(value)) %>%
ungroup() %>%
# data below this value looks dodgy
# Aussie 1990 value looks dodgy too
filter(kg_cap > 10^-4,
time >= 2000,
!(country == "Australia" &
time == 1990)) %>%
group_by(time) %>%
mutate(rank = min_rank(-kg_cap) * 1) %>%
ungroup() %>%
arrange(country, time)


ggplot(data_kg,
aes(x = time, y = kg_cap, colour = country)) +
geom_line() +
labs(title = "Meat Consumption over Time by Location",
y = "Meat Consumption per Capita (kg)",
x = "Calendar Year",
colour = "Location") +
scale_x_continuous(breaks = seq(2000,
2030,
by = 5))

# function to generate plot
f_animate <- function(data, x, y, trans, flnm, ttl,
rnd = 0, dr = 20, spaces = " ",
lab_title = "Meat Consumption per Capita (kg)"){

p <- ggplot(data, aes(rank, group = !! x,
fill = as.factor(!! x),
colour = as.factor(!! x))) +
geom_tile(aes(y = !! y / 2,
height = !! y,
width = 0.9), alpha = 0.8, colour = NA) +

geom_text(aes(y = 0, label = paste(!! x, spaces)),
vjust = 0.2, hjust = 1, size = 5) +
# This line for the numbers that tick up
geom_text(aes(y = !! y,
label = sprintf(paste0("%.", rnd, "f"), !! y)),
hjust = 0, nudge_y = 6, size = 6) +

# text in x-axis (requires clip = "off" in coord_*)
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(colour = FALSE, fill = FALSE) +

labs(title = ttl,
subtitle = '{closest_state %>% as.numeric %>% floor}',
x = "",
y = lab_title) +
theme(plot.title = element_text(hjust = 0.5, size = 20),
plot.subtitle = element_text(hjust = 0.5, size = 30),
axis.text = element_text(size = 15),
axis.title = element_text(size = 15),
axis.ticks.y = element_blank(), # These relate to the axes post-flip
axis.text.y = element_blank(), # These relate to the axes post-flip
plot.margin = margin(2,2,2,4, "cm")
) +

gganimate::transition_states(!! trans, transition_length = 1, state_length = 0) +
gganimate::enter_grow() +
gganimate::exit_shrink() +
gganimate::ease_aes('linear')

# run animation
gganimate::animate(p, fps = 20, duration = dr

# # get maximum transition
# data %>%
# arrange(desc(!! trans)) %>%
# slice(1) %>%
# pull(!! trans)
#
# -
#
# # get minimum transition
# data %>%
# arrange(!! trans) %>%
# slice(1) %>%
# pull(!! trans)
,

width = 600, height = 600, end_pause = 12)

# save the animation to disk
gganimate::anim_save(filename = flnm)

}

f_animate(
data = data_kg,
x = quo(country),
y = quo(kg_cap),
trans = quo(time),
flnm = "meat_consumption_animated.gif",
ttl = "Meat Consumption over Time by Location",
dr = 30
)

You either need to add (+) the anim_save to the animation OR store the animation to and object and call it...

gganimate(p) +
anim_save(filename)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.