套件載入
library(ggmap)
library(leaflet)
library(DT)
options(stringsAsFactors = F)
苗栗(市)在哪?
- 粗略的定義為苗栗火車站方圓5公里
- Windows用戶要將第一行改為
(Miaoli <- geocode(iconv("苗栗火車站","big5","UTF-8")))
(Miaoli <- geocode("苗栗火車站"))
## lon lat
## 1 120.8223 24.57002
leaflet() %>% addTiles() %>%
fitBounds(0, 40, 10, 50) %>%
setView(Miaoli$lon, Miaoli$lat, zoom = 12) %>%
addCircles(Miaoli$lon, Miaoli$lat, radius = 5000)
打卡資料擷取
library(jsonlite)
經緯度 <- paste(Miaoli$lat, Miaoli$lon, sep=",")
方圓幾公尺 <- 5 * 1000
token <- "EAACEdEose0cBAGSxrzfb7UmCn2Ih9y9muyObMtYIM4ly5pp03DbxQ0UYjmJkZBtXXZAyadgjR7wXQ1cajBn3sZAxD8pVyAEfnDb3xzYMxNoLyc9fkqnZAozX54FeKZCuPQJuvntuOcQT4hqzblVuwJU4IH1e0ZCJYDmRYry35spWovIDlfgL64nt7SZCp2qDz8ZD"
url <- sprintf("https://graph.facebook.com/search?type=place¢er=%s&distance=%s&limit=50000&access_token=%s", 經緯度, 方圓幾公尺, token)
tmp <- fromJSON(url)
result <- tmp$data[c("category","name","id")]
while(!is.null(tmp$paging$`next`)){
tmp <- fromJSON(tmp$paging$`next`)
result <- rbind(result, tmp$data[c("category","name","id")])
}
datatable(result,
extensions = 'Scroller', options = list(
deferRender = TRUE,
scrollY = 500,
scroller = TRUE
))
取得各點的打卡次數
checkin <- sapply(result$id, function(id){
fields <- "name,checkins,category"
url <- sprintf("https://graph.facebook.com/%s?fields=%s&access_token=%s", id, fields, token)
unlist(fromJSON(url))
})
checkin <- as.data.frame(t(checkin))
checkin$checkins <- as.integer(checkin$checkins)
checkin <- checkin[order(checkin$checkins, decreasing = T),]
row.names(checkin) <- NULL
datatable(checkin,
extensions = 'Scroller', options = list(
deferRender = TRUE,
scrollY = 200,
scroller = TRUE
))
結論
- 這樣你知道苗栗人有多愛唱歌了(誤),而且還沒有錢櫃好樂迪…
- 雖然湯家肉圓被排到63名,但我覺得真的很好吃,尤其是他的肉羹湯