Skip to content

Instantly share code, notes, and snippets.

View brodieG's full-sized avatar

Brodie Gaslam brodieG

View GitHub Profile
@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 / 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 / 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 / fansi-valgrind.md
Created March 26, 2018 20:14
strsplit valgrind warnings
@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 / testthat.Rout
Created April 1, 2018 00:07
`crayon` tests run with `fansi` string manip funs
── 1. Failure: col_substr keeps color (@test-operations.R#31) ─────────────────
col_substr("\033[31mred\033[39m", 1, 1) not equal to "\033[31mr\033[39m".
1/1 mismatches
x[1]: "\033[31mr\033[0m"
y[1]: "\033[31mr\033[39m"
── 2. Failure: col_substr keeps color (@test-operations.R#33) ─────────────────
col_substr("foo\033[31mred\033[39m", 4, 4) not equal to "\033[31mr\033[39m".
1/1 mismatches
x[1]: "\033[31mr\033[0m"
@brodieG
brodieG / cran-check.R
Last active June 18, 2019 21:02
Check CRAN Results
# started with `foghorn`, but that comes with dependencies and
# slows down R startup. `browseURL` just barely slows startup,
# even if actual page is slow to load.
check_cran_old <- function(email) {
utils::browseURL(
sprintf(
"https://cran.r-project.org/web/checks/check_results_%s.html",
gsub("[^A-Za-z0-9_:.-]", "_", sub("@", "_at_", email))
) ) }
@brodieG
brodieG / shader.R
Created October 19, 2018 16:56
Attempt to vectorize rayshader
faster_bilinear <- function (Z, x0, y0){
i = floor(x0)
j = floor(y0)
XT = (x0 - i)
YT = (y0 - j)
result = (1 - YT) * (1 - XT) * Z[i, j]
nx = nrow(Z)
ny = ncol(Z)
if(i + 1 <= nx){
result = result + (1-YT) * XT * Z[i + 1, j]
@brodieG
brodieG / let-lm.R
Created March 12, 2019 15:09
Test out let with LM
## Tests based on https://mailund.dk/posts/scoping-rules-and-nse/
# rm(list=ls())
if(length(ls())) message("you have variables defined in the environment")
library(wrapr)
lm_prop2a <- function(x, y, d, prop, eval) {
let(c(x = x, y = y, prop = prop, d=d),
summary(lm(y ~ x, data = d))$prop,
eval = eval, envir=parent.frame())
}
@brodieG
brodieG / paint.Rmd
Created April 25, 2019 13:17
Convert PNG to HTML in Rmarkdown
# PNG to HTML
Code adapted from [@gdagstn](https://gist.github.com/gdagstn/1917e0e6f7dc91a8dde7cb789326c3bb).
```{r, echo=FALSE, comment="", results="asis"}
old.hooks <- fansi::set_knit_hooks(knitr::knit_hooks)
```
```{r echo=FALSE}
library(crayon)