---
title: "Days in office of Czech ministers"
author: "Michael Škvrňák"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Days in office of Czech ministers}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

This vignette illustrates a basic workflow how to get data from
Hlídač státu API using `hlidacr` package. 

For accessing data from the API, you need to obtain API token at
the website of [Hlídač státu](https://www.hlidacstatu.cz/api/v1/Index).
To get a token, you need to register. 

I store the token in the environment variable `HLIDAC_TOKEN`.

For the purpose of the illustration, the following lines show
how to get data from the dataset on Czech ministers' days in office
which are stored in the dataset with id `ministri`.
To get the data, you need to call the function `get_dataset_data` which
returns a list with three elements: Total, Page, and Results. 
Total indicates the total number of records, Page indicates the current
page queried from the API and Results contain data.frame with the data. 
Therefore, you need to iterate over all of the pages which I do using
`purrr::map_df`. 

```{r, fig.show='hold', eval=FALSE}
library(dplyr)
library(hlidacr)

TOKEN <- Sys.getenv("HLIDAC_TOKEN")

ministers <- get_dataset_data("ministri", token = TOKEN)
total_records <- ministers$Total
n_rows <- nrow(ministers$Results)

total_pages <- ceiling(total_records / n_rows)

purrr::map_df(1:total_pages, function(x) {
  get_dataset_data("ministri", page = x, token = TOKEN)$Results
}) -> ministers_all

ministers_all %>%
  mutate(start_date = as.Date(zacatek, format = "%Y-%m-%dT%H:%M:%S"), 
         end_date = as.Date(konec, format = "%Y-%m-%dT%H:%M:%S"), 
         term_days = end_date - start_date) -> ministers_terms

# Descriptive statistics of days in office
summary(as.numeric(ministers_terms$term_days))
```

