Inspired by http://www2.technologyreview.com/tr50/2014/
Is there no innovation outside USA?
Just 2 steps.
For maping locations, we need to get latidude and longitude.
I used Google Geocoding API through the code from the article below.
http://allthingsr.blogspot.jp/2012/01/geocode-your-data-using-r-json-and.html
getGeoCode <- function(gcStr){
# FROM http://allthingsr.blogspot.jp/2012/01/geocode-your-data-using-r-json-and.html
library("RJSONIO") #Load Library
gcStr <- gsub(' ','%20',gcStr) #Encode URL Parameters
#Open Connection
connectStr <- paste('http://maps.google.com/maps/api/geocode/json?sensor=false&address=',gcStr, sep="")
con <- url(connectStr)
data.json <- fromJSON(paste(readLines(con), collapse=""))
close(con)
#Flatten the received JSON
data.json <- unlist(data.json)
lat <- data.json["results.geometry.location.lat"]
lng <- data.json["results.geometry.location.lng"]
gcodes <- data.frame(lat=lat, lng=lng, stringsAsFactors=FALSE)
rownames(gcodes) <- NULL
return (gcodes)
}
d <- read.csv("http://dl.dropboxusercontent.com/u/956851/company.csv", as.is=TRUE) # Companies list
res <- NULL
for(i in 1:50){
res <- rbind(res, getGeoCode(d$address[i]))
Sys.sleep(0.5) # API restriction
}
d <- cbind(d, res)
I used Ramnath Vaidyanathan's excellent library rMaps to make a map. It is very easy.
library(rMaps)
map <- Leaflet$new()
map$setView(c(0, 0), zoom = 1)
for(i in seq_len(nrow(d))){
map$marker(
c(d[i,"lat"], d[i,"lng"]),
bindPopup = paste0('<a href="', d$URL, '" target="_blank">', d$company, '</a>')[i]
)
}
map$print('mychart1', include_assets = TRUE, cdn = TRUE)