Skip to content

Instantly share code, notes, and snippets.

Avatar

Jeffrey Arnold jrnold

View GitHub Profile
@jrnold
jrnold / read_sitemap.R
Created Jan 19, 2019
Read/write XML sitemaps in R
View read_sitemap.R
library("xml2")
handle_node <- function(x) {
name <- xml_name(x)
content <- xml_text(x)
if (name == "lastmod") {
content <- lubridate::ymd(content)
}
content <- list(content)
names(content) <- name
View gist:daa039f02486009a24cf3e83403dabf0
"""Run LDA models."""
import gzip
import json
import logging
import os
import os.path
import re
from argparse import ArgumentParser
from collections import OrderedDict, defaultdict
from random import shuffle, seed
@jrnold
jrnold / ancestor.R
Created Jun 28, 2018
Is an R environment an ancestor of another
View ancestor.R
# test if env has parent
has_ancestor <- function(env, ancestor, last = global_env(), n = NULL) {
# special cases
if (!typeof(env) == "environment") {
abort("`env` must be an environment")
}
if (is_empty_env(env)) {
return(FALSE)
}
if (!typeof(ancestor) %in% c("environment", "NULL")) {
View hey_gurl.R
# The print.warnings method only handles printing the last.warning object
# in the warnings() function.
print.warnings <- function(object,
header = "Hey Gurl \U0001f308:\n", ...) {
base:::print.warnings(object, header = header, ...)
}
# I think the message hearder is hardcoded into the do_dfltWarn() function in C
# It uses gettext so one could probably write a "translation" of that message
# and create a custom domain
@jrnold
jrnold / app.R
Created Feb 5, 2018
Visualizing linear regression cost function
View app.R
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
@jrnold
jrnold / zipexample.R
Created Jan 21, 2018
Using file.exists to conditionally download a file - and multiple ways to access the files inside a zipfile in R
View zipexample.R
# Using file.exists to conditionally download a file - and multiple ways to access
# the files inside a g
library("tidyverse")
library("haven")
# name of output directory
OUTDIR <- file.path("data", "leaders")
# URL of the file
# file.path() is a "safe" way to create paths
URL <- "https://www.aeaweb.org/aej/mac/data/2008-0058_data.zip"
View config.json
{
"embeddings": [
{
"tensorName": "Time-Varying Word2Vec Embeddings from 2014 South African Newspapers",
"tensorShape": [
30000,
100
],
"tensorPath": "https://www.dropbox.com/s/pepew73w16xmrlz/word2vec1tv-word_vectors.tsv?dl=0",
"metadataPath": "https://www.dropbox.com/s/s5qfpzgij92ew9u/tensorboard-word_vectors.tsv?dl=0"
@jrnold
jrnold / pgfkeys.R
Created Aug 22, 2017
Write R object to pgfkeys
View pgfkeys.R
write_pgfkeys <- function(x, ...) {
UseMethod("write_pgfkeys")
}
.pgfkey <- function(key, value) {
str_c("\\pgfkeysetvalue{", key, "}{", value, "}")
}
write_pgfkeys.default <- function(x, path=NULL, root = "/data/", ...) {
keys <- str_c(root, names(x)) %>%
@jrnold
jrnold / preprocess-twitter.py
Created Aug 16, 2017 — forked from tokestermw/preprocess-twitter.py
Python version of Ruby script to preprocess tweets for use in GloVe featurization http://nlp.stanford.edu/projects/glove/
View preprocess-twitter.py
"""
preprocess-twitter.py
python preprocess-twitter.py "Some random text with #hashtags, @mentions and http://t.co/kdjfkdjf (links). :)"
Script for preprocessing tweets by Romain Paulus
with small modifications by Jeffrey Pennington
with translation to Python by Motoki Wu
Translation of Ruby script to create features for GloVe vectors for Twitter data.
@jrnold
jrnold / f_filter.R
Created Apr 15, 2017
Filtering terms and variables in R model formulas
View f_filter.R
f_filter_vars <- function(formula., ...) {
f_filter_vars_(formula., .dots = lazyeval::lazy_dots(...))
}
f_filter_vars_ <- function(formula., ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
.terms <- terms(formula.)
.factors <- attr(.terms, "factors")
selectx <- dplyr::select_vars_(rownames(.terms), dots)
keepx <- as.logical(colSums(attr(ft, "factors")[selectx, , drop = FALSE]))
You can’t perform that action at this time.