Skip to content

Instantly share code, notes, and snippets.

@gmbecker
gmbecker / sessionInfo.txt
Last active August 29, 2015 14:14
match.call and do.call
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
@gmbecker
gmbecker / fancystopifnot.Rmd
Created February 9, 2015 19:21
fancystopifnot
```{r}
fancystopifnot = function(obj, condfun) {
x = capture.output(str(obj))
if(!condfun(obj)) stop(paste(c("Condition not met, got", x),
collapse = "\n\t"))}
fancystopifnot(data.frame(x = sample(1:100, 10),
y = sample(c("a", "b"), 10, replace=TRUE)),
is.integer)
```
@gmbecker
gmbecker / vignCovSketch.R
Created February 20, 2015 23:38
Vignette coverage code sketch
library(devtools) # someday this will be switchr ;-)
install_github("duncantl/CodeDepends")
library(knitr)
library(CodeDepends)
## package you're dealing with (pkgname) MUST be loaded before calling this
vignetteCoverage = function(pkgdir, pkgname = basename(pkgdir), vignexts = c(".Rmd", ".Rnw")) {
vigns = list.files(file.path(pkgdir, "vignettes"), pattern = paste0("(", paste(vignexts, collapse="|"), ")"), full.names=TRUE)
tmpfils = sapply(seq(along=vigns), function(x) tempfile( fileext=".R"))
rfils = mapply(function(inp, out) knit(inp, out, tangle=TRUE), inp = vigns, out = tmpfils)
@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
@gmbecker
gmbecker / testgistfile.txt
Created May 15, 2015 13:54
my test of gistr
test gist contents
@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
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 / 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.
@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 / 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"