Skip to content

Instantly share code, notes, and snippets.

View brodieG's full-sized avatar

Brodie Gaslam brodieG

View GitHub Profile
@brodieG
brodieG / all.equal.environment.R
Created December 27, 2020 01:32
Using serialize With all.equal.environment
## Serialization Does Not Trigger Promises
fx <- ff(stop('boom'))
fxa <- ff(stop('boom'))
fy <- ff(stop('boom boom'))
identical(serialize(fx, NULL), serialize(fxa, NULL))
## [1] TRUE
identical(serialize(fx, NULL), serialize(fy, NULL))
## [1] FALSE
@brodieG
brodieG / row-vs-col-sums.R
Last active May 20, 2020 16:27
Sum rows vs cols
library(inline)
frow <- cfunction(sig=c(x='list'), body='
R_xlen_t J = XLENGTH(x);
R_xlen_t n = XLENGTH(VECTOR_ELT(x, 0));
SEXP out = PROTECT(allocVector(INTSXP, n));
int *outp = INTEGER(out);
for (int j=0; j<J; j++) {
int *col = INTEGER(VECTOR_ELT(x, j));
for (int i=0; i<n; i++) {
if(j) {outp[i] += col[i];} else {outp[i] = col[i];}
@brodieG
brodieG / extruded-polygon-tests.R
Created May 2, 2020 21:15
Code used to test rayrender polygon extrusion patches
mk_name <- function(name=NULL) {
v <- as.character(packageVersion('rayrender'))
dir <- file.path('~', 'Downloads', 'rr-tests')
if(is.null(name)) {
name <- sprintf( 'a_%s.png', format(Sys.time(), "%Y-%m-%d_%H%M%S"))
} else {
name <- sprintf('%s.png', name)
}
file.path(dir, v, name)
}
@brodieG
brodieG / reclosures.R
Last active February 29, 2020 03:52
Code used in the reclosure tweet thread
## GPL-2
## Tweet 1
## From https://github.com/djnavarro/ohno
add_with <- function(z) function(x, y) x + y + z
add_with(10)(1, 2) # works
butcher <- function(x, y, f) {
env <- new.env() # create a new environment
environment(f) <- env
@brodieG
brodieG / coord-fixed_dims.R
Last active December 4, 2019 17:59
Device Size for coord_fixed Ggplot
# Compute Device Size To Fully Fill With Plot
#
# This is not tested extensively and I know very little about messing with
# grid.
#
# GPL-2
#
# @param gtable e.g. as produced by `ggplotGrob(ggplot() ...)`
# @param din device dimension in inches, ideally use defaults to avoid
# calculation issues with npc units.
@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)
@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 / 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 / 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 / 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"