lingtypology
and other packagesdplyr
and magritr
%>%It is possible to use dplyr
functions and pipes with lingtypology
. It is widely used, so I will give some examples, how to use it with thelingtypology
package. Using query “list of languages csv” I found Vincent Garnier’s languages-list repository. Let’s download and map all the languages from that set. First download the data:
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)
.
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.
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)
leaflet
There is also a possibility to use lingtypology
with other leaflet
functions (thanks to Niko Partanen for the idea):
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 = .
:
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:
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”.
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:
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
:
map.feature(c("Adyghe", "Russian")) %>%
mapedit::editMap() ->
my_polygone
map.feature(c("Adyghe", "Russian")) %>%
leaflet::addPolygons(data = my_polygone$finished)
rmarkdown
This section is inspired by talks with Niko Partanen. It is possible to create an atlas website using lingtypology
and 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.
df <- wals.feature(c("1a", "20a"))
Second we can create a website using atlas.database()
function:
languages
argument is a language listfeatures
argument is a data.frame with corresponding featureslatitude
and longitude
arguments are optionalatlas.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:
list.files("./atlas_Some_WALS_features/")
The last step is to run a command:
rmarkdown::render_site("./atlas_Some_WALS_features/")
Then the atlas website will be created (here is a result). If you want to change something in the website, just change some files:
.Rmd
filermarkdown::render_site("./atlas_Some_WALS_features/")
command.