Eh oui, 2017 est déjà (presque) fini… À l’heure qu’il est, on boucle les derniers dossiers de l’année, et on se prépare psychologiquement à aller célébrer la nouvelle année comme il se doit. Bref, « pas le temps de niaiser » comme le disent si bien nos amis d’outre-Atlantique.
Histoire de se changer les idées avant de décoller, nous vous avons concocté un deuxième volet de R de jeu, avec la thématique fin d’année !
All I want for Christmas is data
Se connecter à Spotify, on l’a déjà fait, on ne va pas vous en reparler 😉
My Dear Acquaintance [A Happy New Year] – iTunes Live Session Performance
52
What Are You Doing New Year’s Eve?
58
All I Want for Christmas Is New Year’s Day
61
All I Want for Christmas Is New Year’s Day
65
What Are You Doing New Year’s Eve?
59
New Year’s Day
54
Et les artistes les plus présents dans notre jeu de données ?
clean_songs %>%
count(artists, sort = TRUE) %>%
top_n(10) %>%
ggplot() +
aes(reorder(artists, n), n) +
geom_col(fill = sample(full_palette, 1)) +
coord_flip() +
labs(title = "Artistes et tracks du nouvel an",
subtitle = "données via Spotify",
x = "Artistes",
y = "Nombre de morceaux",
caption = "thinkr.fr") +
theme_few()
Maintenant, on se ferait bien une fonction de recommandation de playlist, non ? Disons, une fonction qui permet de créer une playlist adaptée au temps qu’on veut y passer. Commençons par une fonction qui va sampler jusqu’à atteindre un certain niveau :
sample_until<- function(tbl, col, threshold){
col <- enquo(col)
vec <- pull(tbl, !! col)
if ( min(vec) > threshold ) stop("Impossible de trouver une combinaison pour ce threshold")
res <- sample_n(tbl, 1)
while( sum( pull(res, !! col) ) < threshold) {
res <- bind_rows(res, sample_n(tbl, 1)) %>% distinct()
}
res
}
sample_until(iris, Sepal.Length, 30) %>% knitr::kable()
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
Species
6.4
3.2
5.3
2.3
virginica
4.9
3.1
1.5
0.2
setosa
4.8
3.4
1.9
0.2
setosa
5.8
2.7
5.1
1.9
virginica
4.6
3.1
1.5
0.2
setosa
5.4
3.0
4.5
1.5
versicolor
Et voilà ! Combinons maintenant ça avec notre jeu de données de chansons :
Survival – The Champions Olympionic Music – Karaoke Version Originally Performed By Muse
320261
Saturday Mix Dj.
Royals
190918
\o/
À la recherche de la soirée parfaite
C’est bien beau d’avoir une belle playlist, mais il serait mieux de trouver un endroit pour jouer ces morceaux. Qu’est-ce que nous propose Facebook ?
get_event <- function(fb_url){
res <- GET(fb_url)
res <- content(res)
return(list(id = map(res$data, "id"), paging = res$paging))
}
# La première page :
# Le token est temporaire, ça ne marchera pas chez vous ;)
res <- get_event("https://graph.facebook.com/v2.11/search?q=new%20year&type=event&access_token=EAACEdEose0cBAEwhntvjdDENCoU19y3He5q7L6cHoyL9J3RkpxuZCHGxXxN2UZCfFO5d7Ng90MnMh8lbnSP8cZA71iked9ZBLr6wrredSmGCZCY7BgwIXUGEEd0PRdyuD8lhymPdEJjB413T6fqGra0rQ4Wzb3l23auncQDE2xl5cVpgqyIiBAcazYzbFVo8ZD")
next_page <- try(res$paging$`next`)
id_list <- res$id
while( length(next_page) != 0 ) {
res <- get_event(next_page)
next_page <- try(res$paging$`next`)
id_list <- c(id_list, res$id)
}
76 événements, c’est plutôt pas mal. On va scraper tout ça !
Comme souvent lorsque l’on appelle des API, nous avons une liste irrégulière (en JSON, non-tabulaire), que nous avons besoin de transformer en tableau. Go pour une fonction qui réglera ça !
library(lubridate)
tibble_that <- function(list){
tibble(desc = list$description %||% NA,
start_time = ymd_hms(list$start_time) %||% NA,
end_time = ymd_hms(list$end_time) %||% NA,
name = list$name %||% NA,
place = list$place$name %||% NA,
city = list$place$location$city %||% NA,
country = list$place$location$country %||% NA,
street = list$place$location$street %||% NA,
zip = list$place$location$zip %||% NA,
placeid = list$place$id %||% NA
)
}
events <- map_df(all_events, tibble_that)
Alors, quels pays peut-on viser ?
library(lubridate)
tibble_that <- function(list){
tibble(desc = list$description %||% NA,
start_time = ymd_hms(list$start_time) %||% NA,
end_time = ymd_hms(list$end_time) %||% NA,
name = list$name %||% NA,
place = list$place$name %||% NA,
city = list$place$location$city %||% NA,
country = list$place$location$country %||% NA,
street = list$place$location$street %||% NA,
zip = list$place$location$zip %||% NA,
placeid = list$place$id %||% NA
)
}
events <- map_df(all_events, tibble_that)
events %>%
count(city) %>%
na.omit() %>%
top_n(5) %>%
arrange(desc(n)) %>%
ggplot() +
aes(reorder(city, n), n) +
geom_col(fill = sample(full_palette, 1)) +
coord_flip() +
labs(title = "Villes avec des événements 'New Year'",
subtitle = "données via Facebook",
x = "Ville",
y = "Nombre d'événements",
caption = "thinkr.fr") +
theme_few()
Il va falloir qu’on voit ce qu’on a de plus proche. Petit tour du côté de l’API distance24 ?
get_route <- function(from, to){
if (is.na(to)) {
return(NULL)
}
from_clean <- URLencode(from)
to_clean <- URLencode(to)
a <- GET(glue("http://fr.distance24.org/route.json?stops={from_clean}|{to_clean}"))
tibble(from = from,
to = to,
dist = content(a)$distances[[1]])
}
travel <- map_df(events$city, ~ get_route("Rennes", .x))
Laisser un commentaire