Skip to content

Instantly share code, notes, and snippets.

View brodieG's full-sized avatar

Brodie Gaslam brodieG

View GitHub Profile
DT[
id %in% 200:300,
.(reg.val = sum(value)),
by=region
][
reg.val > 0,
range(reg.val)
]
@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 / data.table.standard.eval.R
Last active April 8, 2020 11:02
Corner Cases With Non-Standard Evaluation in data.table
# Because there is no way to tell data.table
# "interpret this variable as a column name", it's possible to come up
# with corner cases. I'll grant these are unlikely to occur in day
# to day use, but any function that uses `data.table` must account for
# them
# Low odds, and yes, there are workarounds, but this is
# what I mean by you have to think carefully to avoid
# corner cases
@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 / 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 / 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())
}