Skip to content

Instantly share code, notes, and snippets.

@joebrew
joebrew / decodingapi.R
Last active September 22, 2017 09:37
Decoding API
# Query the raw data
# https://dbs.manhica.net:4843/dhis/api/27/dataValueSets.json?orgUnit=gA8TPImqcD3&period=2017W30&dataSet=KrDJDTu8M7j&children=true
rd <- jsonlite::fromJSON(txt = 'data/rrs/rd.json')$dataValues
# Query the organization units
# https://dbs.manhica.net:4843/dhis/api/27/dataValueSets.json?orgUnit=gA8TPImqcD3&period=2017W30&dataSet=KrDJDTu8M7j&children=true
ou <- jsonlite::fromJSON(txt = 'data/rrs/ou.json')$dataValues
# Then get data elements
@joebrew
joebrew / make_animation.R
Created September 8, 2017 13:07
Make animation
# Define a function for adding zerio
add_zero <-
function (x, n)
{
x <- as.character(x)
adders <- n - nchar(x)
adders <- ifelse(adders < 0, 0, adders)
for (i in 1:length(x)) {
@joebrew
joebrew / vis_data.R
Created September 8, 2017 13:06
Visualize hurricane data
# Map the world
map('world',
xlim = c(-90, -10),
ylim = c(10, 40),
fill = TRUE,
col = adjustcolor('black', alpha.f = 0.6))
# Add Irma
lines(irma)
@joebrew
joebrew / process_data.R
Created September 8, 2017 13:06
Process hurricane data
# Process hurricanes data
hurricanes <-
hurricanes %>%
mutate(year = Season) %>%
# Keep only those since 1917
filter(year >= 1917) %>%
mutate(date_time = ISO_time,
lon = Longitude,
lat = Latitude,
@joebrew
joebrew / get_data.R
Created September 8, 2017 13:05
Getting hurricane data
# Libraries
library(tidyverse)
library(rworldmap)
library(gganimate)
library(rgdal)
# Read data
world <- map_data(map="world")
hurricanes <- read_csv('data/Allstorms.ibtracs_wmo.v03r09.csv', skip = 1) # https://www.ncdc.noaa.gov/ibtracs/index.php?name=wmo-data
cnames <- names(hurricanes)
@joebrew
joebrew / leaf_country.R
Created August 4, 2017 16:39
View Strava data with leaflet
# Define function for leaflet plot of only a country
leaf_country <- function(location,
limit = NULL,
city = FALSE){ # or NULL
if(city){
sub_data <- df %>%
filter(city == location)
} else {
sub_data <- df %>%
@joebrew
joebrew / munge_data.R
Created August 4, 2017 16:37
Munge data retrieved from get_data.R
library(tidyverse)
library(rworldmap)
library(sp)
library(feather)
library(leaflet)
library(maptools)
library(rgeos)
library(rgdal)
library(htmltools)
library(htmlwidgets)
@joebrew
joebrew / get_data.R
Created August 4, 2017 16:35
Get lots of geographic activity data from the Strava API
library(tidyverse)
# https://github.com/fawda123/rStrava
library(rStrava)
library(yaml)
library(feather)
options(scipen = '999')
# Get access / authentication token for Strava's API
credentials <- yaml::yaml.load_file(input = 'credentials/credentials.yaml')
@joebrew
joebrew / zip.geojson
Created September 30, 2014 22:59
test
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@joebrew
joebrew / zip.geojson
Created September 30, 2014 20:43
test
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.