Skip to content

Instantly share code, notes, and snippets.

View brodieG's full-sized avatar

Brodie Gaslam brodieG

View GitHub Profile
@brodieG
brodieG / substr-bug.md
Last active March 29, 2018 01:17
Description of possible R substr bug

In writing a string manipulation library I ran into what I think is a bug in how substr handles malformed UTF8 strings. I was testing corner cases, in particular the following case of a string that ends with the first byte of a UTF8 sequence:

string <- "abc\xEE"    # \xEE indicates the start of a 3 byte UTF-8 sequence
Encoding(string) <- "UTF-8"
substr(string, 1, 10)

When run under valgrind with level 2 instrumentation, we get:

> string <- "abc\xEE"    # \xEE indicates the start of a 3 byte UTF-8 sequence
@brodieG
brodieG / fansi-valgrind.md
Created March 26, 2018 20:14
strsplit valgrind warnings
@brodieG
brodieG / pwned2.R
Last active February 26, 2018 17:43
Use Troy Hunt's Have I Been Pwned Database from R
# Test your passwords against the "Pwned Passwords" V2 database
# as described in <https://www.troyhunt.com/ive-just-launched-pwned-passwords-version-2/>
#
# USE AT YOUR OWN RISK. I MAKE NO GUARANTEES THAT THIS CODE
# PROTECTS YOUR PASSWORDS.
#
# requires `digest` package (thanks @eddelbuettel)
#
# This code is published under the GPL-2 License
# <https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html>
@brodieG
brodieG / gdp.txt
Created January 17, 2018 23:22
GDP Example
State GDP
California 2622731835.09
Colorado 322644574.0735
Florida 926049669.5
Hawaii 84671360.816540
Mississippi 108495598.4982
Montana 46227205.9303
New Mexico 93594132.5471
New York 1500055297.281
Tennessee 331868896.1153
@brodieG
brodieG / ggplot-pipe.R
Last active November 29, 2017 20:19
Turn Ggplot into a Pipe Operated Machine
`%>%` <- function(lhs, rhs) UseMethod("%>%")
`%>%.default` <- magrittr::`%>%`
`%>%.gg` <- function(lhs, rhs) ggplot2:::`+.gg`(lhs, rhs)
# WARNING: IF YOU DO THE FOLLOWING YOU SHOULD PROBABLY EXIT AND
# RESTART YOUR R SESSION SO THAT MAGRITTR IS RESTORED TO ITS
# ORIGINAL STATE.
assignInNamespace("is_pipe",
function (pipe)
@brodieG
brodieG / Blinky-Verse.R
Last active November 9, 2017 23:36
A Blinking Tidyverse Logo
blinkyverse_logo <- function() {
logo <- c(tidyverse::tidyverse_logo(unicode=FALSE))
logoa <- gsub("*", "\u2b22", logo, fixed=TRUE)
logoa <- gsub("o", "\u2b21", logoa, fixed=TRUE)
logob <- gsub("o", "\u2b22", logo, fixed=TRUE)
logob <- gsub("*", "\u2b21", logob, fixed=TRUE)
logob <- gsub(".", "*", logob, fixed=TRUE)
@brodieG
brodieG / datebw-vetr.R
Created July 26, 2017 16:09
Illustration on using `vetr` with `date_between`
date.2 <- rep(Sys.Date(), 2) # length 2 date object
date.1 <- date.2[1]
valid_identifier_regex <- function() "^\\w+$"
date_between <- function(column_name, date_range) {
vetr(
character(1L) && grepl(valid_identifier_regex(), .),
(date.2 || date.1) && !anyNA(.)
)
TRUE
@brodieG
brodieG / valaddin-vetr-benchmrk.md
Created July 2, 2017 14:55
`valaddin` vs `vetr` performance.

Snippet used to compare valaddin to vetr performance. This is not actually generated as part of vignettes because it will then require valaddin as part of the build package, which brings in unwanted dependencies (via suggests) that make the build process slow on travis.

secant <- function(f, x, dx) (f(x + dx) - f(x)) / dx

secant_valaddin <- valaddin::firmly(secant, list(~x, ~dx) ~ is.numeric)
@brodieG
brodieG / grepso.R
Created April 18, 2017 22:08
Grep Stack Overflow
reg <- "(A|B)*" # overflow
reg <- "([AB])*" # works
string <- paste0(rep("A", 1e4), collapse="")
grep(reg, string, perl=TRUE)
@brodieG
brodieG / GIF-Screencast-OSX.md
Created March 18, 2017 00:58 — forked from dergachev/GIF-Screencast-OSX.md
OS X Screencast to animated GIF

OS X Screencast to animated GIF

This gist shows how to create a GIF screencast using only free OS X tools: QuickTime, ffmpeg, and gifsicle.

Screencapture GIF

Instructions

To capture the video (filesize: 19MB), using the free "QuickTime Player" application: