Skip to content

Instantly share code, notes, and snippets.

View RobertMyles's full-sized avatar
👽

Robert Myles McDonnell RobertMyles

👽
View GitHub Profile
@jennybc
jennybc / vv_transformer.R
Last active August 9, 2019 18:32
A glue transformer inspired by the "variables & values" f-string syntax in Python 3.8
library(glue)
vv_transformer <- function(text, envir) {
regex <- "=$"
if (!grepl(regex, text)) {
return(identity_transformer(text, envir))
}
text <- sub(regex, "", text)
res <- identity_transformer(text, envir)
n <- length(res)
gh_migrate("YOUR_GITHUB_USERNAME", "GITHUB_REPO")
gitea_user <- function(gitea_username,
api_endpoint = Sys.getenv("GITEA_BASE_URL"),
gitea_token = Sys.getenv("GITEA_PAT")) {
require("httr", quietly = TRUE)
require("jsonlite", quietly = TRUE)
httr::GET(
@klmr
klmr / generator.md
Last active August 28, 2022 02:26
Python-like generators in R

A little experiment using restarts.

(And while we’re at it, let’s torture R’s syntax a little.)

![screenshot][]

In the following we will be using R’s “restarts” feature to implement the state machine that drives generators in languages such as Python. Generators allow lazily generating values on demand: a consumer invokes a generator, and consumes values as they are produced. A new value is only produced once the previous one has been consumed.

@hugobowne
hugobowne / tweet_listener.py
Last active October 6, 2023 18:48
NOTE: this code is for a previous version of the Twitter API and I will not be updating in the near future. If someone else would like to, I'd welcome that! Feel free to ping me. END NOTE. Here I define a Tweet listener that creates a file called 'tweets.txt', collects streaming tweets as .jsons and writes them to the file 'tweets.txt'; once 100…
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api=None):
super(MyStreamListener, self).__init__()
self.num_tweets = 0
self.file = open("tweets.txt", "w")
def on_status(self, status):
tweet = status._json
self.file.write( json.dumps(tweet) + '\n' )
self.num_tweets += 1
@karpathy
karpathy / pg-pong.py
Created May 30, 2016 22:50
Training a Neural Network ATARI Pong agent with Policy Gradients from raw pixels
""" Trains an agent with (stochastic) Policy Gradients on Pong. Uses OpenAI Gym. """
import numpy as np
import cPickle as pickle
import gym
# hyperparameters
H = 200 # number of hidden layer neurons
batch_size = 10 # every how many episodes to do a param update?
learning_rate = 1e-4
gamma = 0.99 # discount factor for reward
@srvanderplas
srvanderplas / Plot fragment hook
Created March 24, 2015 17:59
Knitr plot fragment hook for reveal.js
c0 <- knitr::knit_hooks$get('chunk')
knitr::knit_hooks$set(
list(
chunk=function(x,options){
if(is.null(options$class) & is.null(options$fragIndex)){
c0(x, options)
} else if(is.null(options$fragIndex)) {
classidx <- which(names(options)=="class")
@Pakillo
Pakillo / word-character-count-Rstudio.md
Last active June 25, 2020 21:24
Count words and characters in Rstudio

As far as I know Rstudio does not count words or characters at the moment, which would be useful particularly when writing Rmarkdown.

This is a quick shortcut using word_count and character_count functions from qdap package. See below for two wrapper functions that simplify their use.

library("qdap")

Just select and copy the text to the clipboard and then run in the console:

@zross
zross / knitr_hook.R
Last active May 4, 2016 16:20
Not completely finished hook to add fragment tags to RMarkdown code chunks (slidify, revealjs, R)
s0 <- knitr::knit_hooks$get('source')
o0 <- knitr::knit_hooks$get('output')
p0 <- knitr::knit_hooks$get('plot')
knitr::knit_hooks$set(
list(
source=function(x,options){
if (is.null(options$class)) s0(x, options)
#------------------------------------------------------------
# REVOLUTION ANALYTICS WEBINAR: INTRODUCTION TO R FOR DATA MINING
# February 14, 2013
# Joseph B. Rickert
# Technical Marketing Manager
#
# BIG DATA with RevoScaleR
#
# Copyright: Revolution Analytics
@graydon
graydon / country-bounding-boxes.py
Created April 23, 2014 00:03
country bounding boxes
# extracted from http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip
# under public domain terms
country_bounding_boxes = {
'AF': ('Afghanistan', (60.5284298033, 29.318572496, 75.1580277851, 38.4862816432)),
'AO': ('Angola', (11.6400960629, -17.9306364885, 24.0799052263, -4.43802336998)),
'AL': ('Albania', (19.3044861183, 39.624997667, 21.0200403175, 42.6882473822)),
'AE': ('United Arab Emirates', (51.5795186705, 22.4969475367, 56.3968473651, 26.055464179)),
'AR': ('Argentina', (-73.4154357571, -55.25, -53.628348965, -21.8323104794)),
'AM': ('Armenia', (43.5827458026, 38.7412014837, 46.5057198423, 41.2481285671)),