Skip to content

Instantly share code, notes, and snippets.

View brodieG's full-sized avatar

Brodie Gaslam brodieG

View GitHub Profile
@brodieG
brodieG / setOldClassProbs.R
Last active August 29, 2015 14:04
Test setOldClass Issues
library(devtools)
install_github("pckgA", "brodieg") # defines old class "file" and class union "fileOrNULL" == c("file", "NULL")
install_github("pckgB", "brodieg") # defines old class "file", and nothing else
library(pckgA)
makeDummy() # from pckgA, instantiates a class with a "fileOrNULL" slot, placing a temp file S3 object in the slot
library(pckgB)
makeDummy() # this fails now
@brodieG
brodieG / ensure_alike.R
Last active August 29, 2015 14:13
Using `alike` with `ensurer`
# Packages
library(devtools)
install_github("smbache/ensurer")
install_github("brodieg/alike")
library(ensurer)
library(alike)
library(magrittr)
set.seed(1)
list(system=c("browser",
"device",
"dvipscmd",
"mailer",
"pager",
"pdfviewer",
"pkgType",
"printcmd",
"HTTPUserAgent",
"texi2dvi",
@brodieG
brodieG / sample0110.R
Last active August 29, 2015 14:23
Code for SO Q: Random sample of character vector, without elements prefixing one another
sample0110 <- function(size, n, complete.only=FALSE) {
size <- as.integer(size)
n <- as.integer(n)
if(size > 25 || size < 3L) stop(
"Currently size min is 3 and max is 25, though should be possible to allow ",
"smaller and larger with some changes"
)
# Generate integer pool and weights
@brodieG
brodieG / bench0101.R
Last active August 29, 2015 14:23
Benchmarks for Random sampling from 01010 etc.
make_pool <- function(size)
sort(
unlist(
lapply(
seq_len(size),
function(x) do.call(paste0, expand.grid(rep(list(c('0', '1')), x)))
) ) )
system.time(pool4 <- make_pool(4))
@brodieG
brodieG / sample01101.R
Last active August 29, 2015 14:23
Random sampling from 01010 with some c implementations
sample01101 <- function(size, n, complete.only=FALSE) {
size <- as.integer(size)
n <- as.integer(n)
if(size > 25 || size < 3L) stop(
"Currently size min is 3 and max is 25, though should be possible to allow ",
"smaller and larger with some changes"
)
# Generate integer pool and weights
@brodieG
brodieG / diff.R
Last active January 15, 2016 23:43
Playing with Diff
diff_rdiff <- function(target, current) {
stopifnot(is.character(target), is.character(current))
a <- tempfile("unitizerRdiffa")
writeLines(target, a)
b <- tempfile("unitizerRdiffb")
writeLines(current, b)
diff <- capture.output(system(paste("diff -bw", shQuote(a), shQuote(b))))
}
differ <- function(A, B) {
@brodieG
brodieG / membug.R
Created February 27, 2016 18:48
Demonstrate Crayon Binary Package Memoise Bug
ls(all=T)
# character(0)
install.packages("crayon")
# trying URL 'http://cran.r-project.org/bin/macosx/mavericks/contrib/3.2/crayon_1.3.1.tgz'
# Content type 'application/x-gzip' length 604079 bytes (589 KB)
# ==================================================
# downloaded 589 KB
#
#
# The downloaded binary packages are in
@brodieG
brodieG / optim.R
Created March 8, 2016 02:41
Demonstrate Some Optimizations to Crayon Substring
## Lightly edited for clarity
R version 3.2.2 (2015-08-14) -- "Fire Safety"
> install.packages("crayon")
trying URL 'http://cran.r-project.org/bin/macosx/mavericks/contrib/3.2/crayon_1.3.1.tgz'
Content type 'application/x-gzip' length 604079 bytes (589 KB)
==================================================
downloaded 589 KB
@brodieG
brodieG / gist:8eaccf0e1bb7cb2f60f1ecfcd26b473c
Last active August 22, 2016 01:44
Illustrate Connection Issues
f1 <- tempfile()
f2 <- tempfile()
cat("hello\n", file=f1)
cat("goodbye\n", file=f2)
con1 <- file(f1)
open(con1)
getAllConnections()
close(con1)