#auunconf slack users’ timezone locations

[This article was first published on R – Irregularly Scheduled Programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I had never used slack before, but had read a heap of tech articles extolling its virtues. Apparently this is what our current Prime Minister advocates within Cabinet. The upcoming #auunconf organising team set up a channel and invited the participants, so I checked it out. Slack is pretty awesome as far as a unified workspace/messaging protocol can go. What makes it even more awesome, is that someone (@hrbrmstr, no surprise) has made an R package that talks to it.

After installing/loading the slackr package, obtaining an API key (the usual drill; create an app, request key, save it somewhere and pray you don’t lose it or share it) and saving it in ~/.slackr (so I don’t have to remember to delete it from shared code) it was as simple as calling slackr_users() to get a data.frame of the users and their relevant data. Neat!

The only geographical information in there was the timezone, so I figured I would merge that with a shapefile of such and plot it. Here’s the code I ended up creating

## Create a world timezone map of the #auunconf slack users' timezones
## data extracted via hrbrmstr/slackr
## Timezone shapefile: http://efele.net/maps/tz/world/
##
## Blogged @ http://jcarroll.com.au/2016/04/14/slack-timezones/
## load relevant packages
pacman::p_load(rgdal, maptools, ggplot2)
pacman::p_load(ggthemes, albersusa, ggalt)
pacman::p_load(extrafont)
# devtools::install_github("hrbrmstr/slackr")
pacman::p_load(slackr)
## load the world timezone shapefile and process
worldtz <- readOGR(dsn=".", layer="tz_world_mp")
worldtz@data$id = rownames(worldtz@data)
worldtz_map <- broom::tidy(worldtz, region="id")
## I wanted to shift the center of the projection to the Pacific.
## It looks like @hrbrmstr has some code that does this on stackoverflow,
## but between one computer failing the ogr2ogr steps and another
## crashing when generating the ggplot, I am abandoning this for now.
## http://stackoverflow.com/questions/32591368/pacific-centric-robinson-projection-with-ggplot-in-r
# system("ogr2ogr world_part1.shp tz_world_mp.shp -clipsrc -180 -90 0 90")
# system("ogr2ogr world_part2.shp tz_world_mp.shp -clipsrc 0 -90 180 90")
# system('ogr2ogr world_part1_shifted.shp world_part1.shp -dialect sqlite -sql "SELECT ShiftCoords(geometry,360,0) FROM world_part1"')
# system("ogr2ogr world_0_360_raw.shp world_part2.shp")
# system("ogr2ogr -update -append world_0_360_raw.shp world_part1_shifted.shp -nln world_0_360_raw")
# world <- readOGR("world_0_360_raw.shp", "world_0_360_raw")
# world_robin <- spTransform(world, CRS("+proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
# worldtz_map <- broom::tidy(world_robin)
# save(worldtz_map, file="world_robin_pacific.RData")
# load("world_robin_pacific.RData")
## merge the map with the map data
worldtz.df = merge(worldtz_map, worldtz@data, by="id")
## users via slackr
## access API token and environment variables stored in ~/.slackr
slackr_setup()
users <- slackr_users()
# save(users, file="users.RData")
load("users.RData")
## the available users data (names redacted)
# dplyr::glimpse(users)
# Observations: 32
# Variables: 35
# $ id (chr) "U0ZQXD867", "U0ZSDK6GH", "U0ZQTP351", "U0ZQVHU03", "U1088MC9E", "U0ZTED8J1", "U0ZS72KEU", "U0ZR0L5S7", ...
# $ team_id (chr) "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", ...
# $ name (chr) ###############################
# $ deleted (lgl) FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,...
# $ status (lgl) NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
# $ color (chr) "9b3b45", "5b89d5", "99a949", "4cc091", "43761b", "235e5b", "684b6c", "5a4592", "d58247", "e0a729", "df3...
## <snip>
## merge the users in with the data/map
worldtz.df.users <- merge(worldtz.df, users, by.x="TZID", by.y="tz", all=TRUE)
## create a factor identifying whether a timezone has some users
worldtz.df.users$hasUser <- as.factor(ifelse(is.na(worldtz.df.users$name), 0, 1))
## create the projected map
gg <- ggplot(worldtz.df.users)
gg <- gg + theme_map()
gg <- gg + labs(title=paste0("#auunconf slack users' timezone locations"),
subtitle="Data extracted via slackr::slackr_users on 14-Apr-2016",
caption="Timezone shapefile: http://efele.net/maps/tz/world/ | Blog: www.jcarroll.com.au ")
gg <- gg + geom_map(map=worldtz_map,
aes(x=long, y=lat, map_id=id.x, fill=hasUser),
color="grey60", size=0.2)
gg <- gg + coord_proj()
gg <- gg + scale_fill_manual(guide="none", values=c("white", "cyan"))
gg <- gg + theme(text=element_text(size=16, family="Arial Narrow"))
gg
## save a .png copy
ggsave(gg, filename="auunconf_slackr_users_map.png", height=8, width=8, dpi=300)
## upload to the #auunconf #general channel
dev_slackr("#general")
view raw slackrs.R hosted with ❤ by GitHub

Once I had plotted the map I wished the projection was more Pacific-centered, and looked into making that happen. It appears to be trickier than I wanted to bother with for such a small project, so I ended up abandoning it. I did find a stackoverflow answer that seemed to have all the right ingredients (again, @hrbrmstr at work) but I couldn’t get it to plot in any sort of reasonable time.

#auunconf slack users' timezones

#auunconf slack users’ timezones

The unique users so far claim to come from:

  • Australia/Brisbane
  • Australia/Canberra
  • Asia/Ulaanbaatar
  • America/Indiana/Indianapolis
  • Australia/Adelaide
  • Europe/Amsterdam
  • Pacific/Auckland

so quite the diverse crowd.

Once all was done and plotted, uploading the image to the slack team was as easy as dev_slackr("#general") which sends the current graphic to the #general channel of the slack team that slackr was configured for. Sure enough, it worked!

It works!

It works!

I’m not entirely sure what I’ll use this for, but it was certainly a fun exercise to get working. Perhaps I can generalise it enough to submit a pull-request to make it available in slackr?

To leave a comment for the author, please follow the link and comment on their blog: R – Irregularly Scheduled Programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)