Skip to content

Instantly share code, notes, and snippets.

View geotheory's full-sized avatar

Robin Edwards geotheory

View GitHub Profile
require(rtweet)
twitter_list_id_to_name = function(listid){
lm = lists_members(listid)
listed_by = lists_memberships(lm$screen_name[1], n = 1000)
gsub('@', '', filter(listed_by, list_id == listid)$full_name)
}
twitter_list_id_to_name('992463105195114496')
#'
#' Distinctive colours for qualitative data visualisation
#'
#' Depends: cluster, TSP
#'
distinct_hues = function(n, H = c(0,360), C = c(30,80), L = c(15,90), alpha = NULL, sample_size = 250,
metric = c("euclidean", "manhattan"), ordered = FALSE, seed = NULL, ...){
if(!is.null(seed)) set.seed(seed)
ran_cols = data.frame(lapply(list(h=H,c=C,l=L), function(x) sample(x[1]:x[2], sample_size, replace=TRUE)))
require(stringr)
bool_detect = function(x, qry, ignore_case = TRUE, print_call = FALSE){
ops = c(AND = '&', `&` = '&', OR = '|', `|` = '|', `(` = '(', `)` = ')') # boolean operators
# add spaces around parentheses;
qry = qry %>% str_trim %>% str_replace_all(fixed('('), '( ') %>% str_replace_all(fixed(')'), ' )') %>%
str_replace_all("'", '"') %>% # enforce double quotes
str_replace_all('(?<="[^ ]{0,100}) (?=[^ ]+")', '\u00A0') # sub spaces in quoted text for a unicode space
# split elements by ascii space character
comps = str_split(qry, '[ ]+')[[1]] %>% str_replace_all('\u00A0', ' ') %>% str_remove_all('"')
require(ggplot2)
gg_donut = function(x, lab, textsize = 5){
ggplot() +
geom_rect(aes(ymax = c(100, x), ymin = 0, xmax = 4, xmin = 3.2, fill = c('A','B'))) +
geom_text(aes(y = 0, x = 0, label = paste0(round(x,1), '%')), size = textsize) +
coord_polar(theta="y") + xlim(c(0, 4)) +
scale_y_continuous(limits = c(0,100)) +
scale_fill_manual(values = c('grey80','dodgerblue2')) +
labs(title = lab) +
@geotheory
geotheory / snowflake.R
Created October 4, 2020 17:59 — forked from qykong/snowflake.R
twitter id decomposation in R
library(bit64)
twepoch <- as.integer64('1288834974657')
base <- as.integer64(2)
datacenter_id_bits <- 5
worker_id_bits <- 5
sequence_id_bits <- 12
max_datacenter_id <- 1 * base^datacenter_id_bits
max_worker_id <- 1 * base^worker_id_bits
max_sequence_id <- 1 * base^sequence_id_bits
fn = function(){ print("hello"); message("bye bye") }
sink(tt <- textConnection("results","w"),split=TRUE)
msg_results <- utils::capture.output(eval(parse(text = 'fn()')), type='message')
sink()
results
close(tt)
cat(paste(results, msg_results, collapse='\n'))
[{"val":752.9611,"wgt":89},{"val":2209.0059,"wgt":260},{"val":720.7838,"wgt":103},{"val":192.604,"wgt":25},{"val":60.1375,"wgt":7},{"val":20.2293,"wgt":3},{"val":77.3015,"wgt":11},{"val":512.4341,"wgt":68},{"val":188.5246,"wgt":23},{"val":0,"wgt":0},{"val":16.6528,"wgt":2},{"val":1624.0071,"wgt":184},{"val":52.1739,"wgt":6},{"val":223.6652,"wgt":31},{"val":25.1256,"wgt":3},{"val":738.9831,"wgt":109},{"val":20.3666,"wgt":3},{"val":170.9402,"wgt":24},{"val":145.3224,"wgt":16},{"val":77.2798,"wgt":10},{"val":137.0906,"wgt":18},{"val":865.5462,"wgt":103},{"val":308.642,"wgt":45},{"val":211.3156,"wgt":31},{"val":18.1324,"wgt":2},{"val":39.1236,"wgt":5},{"val":47.0114,"wgt":7},{"val":4457.9288,"wgt":551},{"val":886.6995,"wgt":126},{"val":1357.6159,"wgt":164},{"val":686.7925,"wgt":91},{"val":45.8015,"wgt":6},{"val":391.9861,"wgt":45},{"val":43.0725,"wgt":6},{"val":587.7342,"wgt":69},{"val":1125.6746,"wgt":146},{"val":72.6744,"wgt":10},{"val":1069.9242,"wgt":127},{"val":490.9984,"wgt":60},{"val":69.2641,"wgt":8},{"va
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@geotheory
geotheory / parts-of-speech-highlighting.R
Last active September 18, 2020 12:57
Function for rendering Spacy-processed text in RStudio to visualise its parts-of-speech classifications
require(tidyverse)
# Other required packages: xml2, rvest, shiny
render_pos = function(s, cols = NULL){
if(!exists('.lookup_tbls')){
message('initialising tool')
.lookup_tbls <<- xml2::read_html('https://spacy.io/api/annotation') %>% rvest::html_table()
.lookup_pos <<- as_tibble(.lookup_tbls[[1]]) %>% janitor::clean_names() %>% select(pos, pos_desc = description)
.lookup_dep_rel <<- as_tibble(.lookup_tbls[[5]]) %>% janitor::clean_names() %>% select(dep_rel = label, dep_rel_desc = description)
.tempDir <<- tempfile()
@geotheory
geotheory / emojis-dataframe.R
Last active October 3, 2025 11:11
emojis-dataframe.R
require(tidyverse)
require(emo)
emojis = readLines('http://www.unicode.org/Public/emoji/13.0/emoji-test.txt') %>%
.[ji_detect(.)] %>%
str_replace_all('[ ]+', ' ') %>%
str_replace('#', ';') %>%
enframe(name = NULL) %>%
separate(value, c('code', 'status', 'desc'), sep = ' ; ') %>%
mutate(unicode = ji_extract_all(desc) %>% map_chr(~ paste(.x, collapse='')),