Skip to content

Instantly share code, notes, and snippets.

@gmbecker
gmbecker / manifest.rman963c2e2474f2
Created January 31, 2022 19:22
An R seeding manifest
# R manifest
# Manifest type: session
# Dependency repositories: 6
# repo: https://cloud.r-project.org
# repo: https://bioconductor.org/packages/3.14/bioc
# repo: https://bioconductor.org/packages/3.14/data/annotation
# repo: https://bioconductor.org/packages/3.14/data/experiment
# repo: https://bioconductor.org/packages/3.14/workflows
# repo: https://bioconductor.org/packages/3.14/books
"name","url","type","branch","subdir","extra","version"
@gmbecker
gmbecker / argify.R
Created April 4, 2018 18:21
Add argument specification to a function call
argify = function(expr, recursive = FALSE) {
if(is.character(expr))
expr = parse(text = expr)
else if(!is.call(expr))
expr = substitute(expr)
stopifnot(is.call(expr))
if(recursive && length(expr) > 1) {
for(i in 2:length(expr)) {
if(is.call(expr[[i]]))
expr[[i]] = argify(expr[[i]], TRUE)
@gmbecker
gmbecker / needlehaystack.R
Created April 4, 2018 15:22
Finding vector in larger vector #rstats
findneedle = function(need, hay) {
inds = which(hay == need[1L])
i = 2L
while(i <= length(need) && length(inds) > 0L) {
nval = need[i]
## adding the scalars together first reduces the allocs
## by 1 saves time if inds is really long
inds = inds[hay[(i - 1L) + inds] == nval]
@gmbecker
gmbecker / manifest.rman875972defe0a
Created November 6, 2017 17:07
An R seeding manifest
# R manifest
# Manifest type: session
# Dependency repositories: 5
# repo: https://bioconductor.org/packages/3.5/bioc
# repo: https://bioconductor.org/packages/3.5/data/annotation
# repo: https://bioconductor.org/packages/3.5/data/experiment
# repo: https://bioconductor.org/packages/3.5/extra
# repo: https://cran.rstudio.com
"name","url","type","branch","subdir","extra","version"
"switchrGist","https://cran.rstudio.com/src/contrib","CRAN","trunk",".",NA,"0.2.1"
@gmbecker
gmbecker / legend_title.R
Created November 15, 2016 20:16
legend title ggplot
## this worked for me
df = data.frame("omg" = factor(sample(1:2, 100, replace=TRUE)), x = rnorm(100), y = rnorm(100))
ggplot(data=df) + aes(x = x, y = y, color = omg) + geom_point() + scale_colour_discrete(guide_legend(title="whaaat?\nok!"))
@gmbecker
gmbecker / broken.Rmd
Last active September 26, 2018 04:25
DebuggingInRmd
We set up knitr so it doesn't catch errors, then set
`options(error=recover)` to set up R's debug-on-error machinery.
We have to do one additional thing, before the options call though:
trace the recover function with`sink(NULL)` to turn off the output
capturing so the R console is useful when the debugging framework
dumps us back into it. This has to happen before the options call
because that call grabs the `recover` object and stores it somewhere
so setting a trace on recover after the call won't affect the cached
version that gets called upon an error.
happyfun = function(pat, txt) {
m = gregexpr(pat, txt, perl=TRUE)[[1]];
t = attr(m, "capture.start");
mapply(function(st, end) substr(txt, st, end),
st = t,
end = t+attr(m, "capture.length")-1)
}
@gmbecker
gmbecker / manifest.rman10162b9f4746
Created June 3, 2015 16:15
An R package manifest
# R manifest
# Manifest type: package
# Dependency repositories: 5
# repo: http://bioconductor.org/packages/3.1/bioc
# repo: http://bioconductor.org/packages/3.1/data/annotation
# repo: http://bioconductor.org/packages/3.1/data/experiment
# repo: http://bioconductor.org/packages/3.1/extra
# repo: http://cran.fhcrc.org
"name","url","type","branch","subdir","extra"
"http://github.com/gmbecker/fastdigest","fastdigest","http://github.com/gmbecker/fastdigest","git","master",".",NA
@gmbecker
gmbecker / testgistfile.txt
Created May 15, 2015 13:54
my test of gistr
test gist contents
@gmbecker
gmbecker / fastRcategories.R
Created March 30, 2015 19:26
"Fast" binary category column generation using only R code. Response to http://rstatistics.net/strategies-to-speed-up-r-code/
col1 <- runif (12^6, 0, 2)
col2 <- rnorm (12^6, 0, 2)
col3 <- rpois (12^6, 3)
col4 <- rchisq (12^6, 2)
df <- data.frame (col1, col2, col3, col4)
## process the rows
## slightly less than 3 times slower than Rcpp example here http://rstatistics.net/strategies-to-speed-up-r-code/ (on my machine)
## but written only in R