---
title: "Quick start"
author: "Jan Caha"
date: "`r Sys.Date()`"
output: 
  html_document: # md_document # html_vignette
    self_contained: false
always_allow_html: yes
vignette: >
  %\VignetteIndexEntry{Vignette Title}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

## Loading the package and looking at prepared data

We start by loading `tidyverse` package to manipulate with data and then load the package itself.

```{r, message=FALSE}
library(tidyverse)
library(sf)
library(CzechData)
library(leaflet)
library(RColorBrewer)
library(htmlwidgets)
library(rmapshaper)
```

```{r}
data(obce)
data(okresy)
```

```{r}
id_okresy_Brno <- okresy %>% 
  filter(str_detect(nazev, "Brno")) %>% 
  pull(lau1_kod)

obce_obce <- obce %>% 
  filter(lau1_kod %in% id_okresy_Brno) %>% 
  datatable(options = list(scrollX = TRUE))

# saveWidget(obce_obce, file = "tabulka.html", selfcontained = FALSE)
```

```{r}
id_obec <- obce %>% 
  filter(nazev == "Brno") %>% 
  pull(kod)
```

```{r}
volebni_okrsky <- load_RUIAN_settlement(id_obec, layer = "volebni okrsky")

volebni_okrsky <- volebni_okrsky %>% 
  select(cislo) %>% 
  st_transform(4326) 

volebni_okrsky <- ms_simplify(volebni_okrsky, keep = 0.1)

names(st_geometry(volebni_okrsky)) <- NULL
```

```{r}
labels <- sprintf("<strong>%s</strong>", volebni_okrsky$cislo) %>% 
  lapply(htmltools::HTML)

l <- leaflet(data = volebni_okrsky, width = "100%") %>%
  addTiles() %>%
  addPolygons(fillColor = brewer.pal(8, "Set2"), fillOpacity = 0.4,
              stroke = TRUE, weight = 0.5, color = "black",
              highlight = highlightOptions(weight = 3,
                                           bringToFront = TRUE),
              label = labels)

# saveWidget(l, file = "mapa.html", selfcontained = FALSE)
```
