---
title: "`lingtypology` and other packages"
author: "George Moroz"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{`lingtypology` and other packages}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
```{r, include=FALSE}
library(lingtypology)
knitr::opts_chunk$set(message= FALSE, eval=FALSE)
```

### 1. `dplyr` and `magritr` %>%

It is possible to use [`dplyr`](https://github.com/tidyverse/dplyr) functions and pipes with `lingtypology`. It is widely used, so I will give some examples, how to use it with the`lingtypology` package. Using query "list of languages csv" I found Vincent Garnier's [languages-list repository](https://github.com/forxer/languages-list). Let’s download and map all the languages from that set. First download the data:
 
```{r}
new_data <- read.csv("https://goo.gl/GgscBE")
tail(new_data)
```

As we see, some values of the `Language.name` variable contain more than one language name. Some of the names probably have different names in our database. Imagine that we want to map all languages from Africa. So that the following examples work correctly, use `library(dplyr)`.

```{r, message= FALSE}
library(dplyr)
new_data %>%
  mutate(Language.name = gsub(pattern = " ", replacement = "", Language.name)) %>% 
  filter(is.glottolog(Language.name) == TRUE) %>% 
  filter(area.lang(Language.name) == "Africa") %>% 
  select(Language.name) %>% 
  map.feature()
```

We start with a dataframe, here a `new_data`. First we remove spaces at the end of each string. Then we check, whether the language names are in the glottolog database. Then we select only rows that contain languages of Africa. Then we select the `Language.name` variable. And the last line maps all selected languages.

By default, the values that came from the pipe are treated as the first argument of a function. But when there are some additional arguments, point sign specify what exact position should be piped to. Let’s produce the same map with a minimap.

```{r, message= FALSE}
new_data %>%
  mutate(Language.name = gsub(pattern = " ", replacement = "", Language.name)) %>% 
  filter(is.glottolog(Language.name) == TRUE) %>% 
  filter(area.lang(Language.name) == "Africa") %>% 
  select(Language.name) %>% 
  map.feature(., minimap = TRUE)
```

### 2. `leaflet`

There is also a possibility to use `lingtypology` with other [`leaflet`](https://github.com/rstudio/leaflet) functions (thanks to [Niko Partanen](https://github.com/nikopartanen) for the idea):

```{r}
library(leaflet)
map.feature(c("French", "Occitan")) %>% 
  fitBounds(0, 40, 10, 50) %>% 
  addPopups(2, 48, "Great day!")
```

If you add `leaflet` arguments befor `map.feature` function, you need to use argument `pipe.data = .`:

```{r}
leaflet() %>% 
  fitBounds(0, 40, 10, 50) %>% 
  addPopups(2, 48, "Great day!") %>% 
  map.feature(c("French", "Occitan"), pipe.data = .)
```

The other usage of this `pipe.data` argument is to put there a variable with a `leaflet` object:

```{r}
m <- leaflet() %>% 
  fitBounds(0, 40, 10, 50) %>% 
  addPopups(2, 48, "Great day!")

map.feature(c("French", "Occitan"), pipe.data = m)
```

If you want to define tiles in `leaflet` part, you need to change tile argument in `map.feature` function, because the default value for the `tile` argument is "OpenStreetMap.Mapnik".

```{r}
leaflet()  %>% 
  addProviderTiles("OpenStreetMap.BlackAndWhite") %>% 
  fitBounds(0, 40, 10, 50) %>% 
  addPopups(2, 48, "Great day!") %>% 
  map.feature(c("French", "Occitan"), pipe.data = ., tile = "none")
```

It is also possible to use some tools provided by [`leaflet.extras` package](https://github.com/RCura/leaflet.extras):

```{r}
map.feature(c("French", "Occitan")) %>% 
  leaflet.extras::addDrawToolbar()  %>%
  leaflet.extras::addStyleEditor()
map.feature(c("French", "Occitan")) %>% 
  leaflet.extras::addFullscreenControl()
```

Also there is a nice package `mapedit`:
```{r, eval = FALSE}
map.feature(c("Adyghe", "Russian")) %>% 
  mapedit::editMap() ->
  my_polygone

map.feature(c("Adyghe", "Russian")) %>% 
  leaflet::addPolygons(data = my_polygone$finished)
```
![](https://raw.githubusercontent.com/ropensci/lingtypology/master/docs/use_mapedit.gif)

### 3. Create your atlas with `rmarkdown`
This section is inspired by talks with [Niko Partanen](https://github.com/nikopartanen). It is possible to create an atlas website using `lingtypology` and [`rmarkdown`](https://github.com/rstudio/rmarkdown) packages. The function `atlas.database()` creates a folder in the working directory that contains an `rmarkdown` template for a web-site.

First, lets create a `dataframe` with some data.
```{r}
df <- wals.feature(c("1a", "20a"))
```

Second we can create a website using `atlas.database()` function:

* `languages` argument is a language list
* `features` argument is a data.frame with corresponding features
* `latitude` and `longitude` arguments are optional

```{r}
atlas.database(languages = df$language,
               features = df[,c(4:5)],
               latitude = df$latitude,
               longitude = df$longitude,
               atlas_name = "Some WALS features",
               author = "Author Name")
```

We can see that this function creates a subfolder with following files:
```{r}
list.files("./atlas_Some_WALS_features/")
```

The last step is to run a command:
```{r, eval=FALSE}
rmarkdown::render_site("./atlas_Some_WALS_features/")
```

Then the atlas website will be created (here is [a result](https://agricolamz.github.io/lingtypology_atlas_example/index.html)). If you want to change something in the website, just change some files:

* write information about atlas in index.Rmd file
* list citation information
* change any `.Rmd` file
* ...
* and on the end rerun the `rmarkdown::render_site("./atlas_Some_WALS_features/")` command.

```{r, include=FALSE}
unlink("./atlas_Some_WALS_features/", recursive = TRUE)
```
