# please download the Github version
# devtools::install_github("hadrilec/insee")
library(tidyverse)
library(lubridate)
library(insee)

idbank_list = get_idbank_list()

df_idbank_list_selected =
  idbank_list %>%
  filter(nomflow == "IPC-2015") %>% #Inflation dataset
  filter(dim1 == "M") %>% # monthly
  filter(str_detect(dim4, "^[0-9]{2}$")) %>% # coicop aggregation level
  filter(dim6 == "INDICE") %>% # index
  filter(dim7 == "ENSEMBLE") %>% # all kinds of household
  filter(dim8 == "FE") %>% # all France including overseas departements
  mutate(title = get_insee_title(idbank))

list_idbank = df_idbank_list_selected %>% pull(idbank)

data = 
  get_insee_idbank(list_idbank, startPeriod = "2015-01") %>% 
  split_title()

data_plot = data %>%
  mutate(TITLE_EN6 = case_when(is.na(TITLE_EN6) ~ TITLE_EN5,
                            TRUE ~ as.character(TITLE_EN6))) %>%
  mutate(TITLE_EN6 = substr(TITLE_EN6, 1 , 22)) %>%
  mutate(month = month(DATE)) %>%
  arrange(DATE) %>%
  group_by(TITLE_EN6, month) %>%
  mutate(growth = 100 * (OBS_VALUE / dplyr::lag(OBS_VALUE) - 1))

ggplot(data_plot, aes(x = DATE, y = growth)) +
  geom_col() +
  facet_wrap(~TITLE_EN6, scales = "free") +
  ggtitle("French inflation, by product category, year-on-year") +
  labs(subtitle = sprintf("Last updated : %s", data_plot$TIME_PERIOD[nrow(data_plot)]))