Skip to content

Instantly share code, notes, and snippets.

View HenrikBengtsson's full-sized avatar

Henrik Bengtsson HenrikBengtsson

View GitHub Profile
@HenrikBengtsson
HenrikBengtsson / workflow.md
Last active January 10, 2017 15:34
WORK FLOW: Git and Bioconductor SVN (2016)

WORK FLOW: Git and Bioconductor SVN (2016)

This is how I currently work with Git and the Bioconductor SVN. I'll use the affxparser package as an example.

Clone existing Git repository

% git clone [email protected]:HenrikBengtsson/affxparser.git
% cd affxparser

Checkout existing Git Flow branches

I'm using the Git Flow workflow everywhere, including this package. In short,

@HenrikBengtsson
HenrikBengtsson / tmpfile.R
Last active May 10, 2016 16:21
Create a temporary file with content on the fly
#' Create a temporary file with content
#'
#' Create a temporary file with content that will auto delete
#' as soon as there is no longer any references to it.
#'
#' @param content A character string to be written to the file
#' @param ... Optional arguments to \code{\link[base]{tempfile}()}.
#'
#' @return The absolute pathname to the temporary file.
#'
@HenrikBengtsson
HenrikBengtsson / dim-ex.R
Last active December 22, 2015 16:02
Vectors can have dimnames and names at the same time
## Example showing that vectors/matrices/arrays can indeed have
## names attributes while still having dimensions/dimnames.
## I intentionally picked confusing names and dimnames.
x <- matrix(1:4, nrow=2, dimnames=list(c("a", "b"), c("A", "B")))
print(x)
### A B
### a 1 3
### b 2 4
@HenrikBengtsson
HenrikBengtsson / bad-pkgs-reinstall.R
Last active December 13, 2015 23:21
Identifying R packages that fail to load and try to re-install them
## Identify installed packages that fail to load
rscript <- function(...) {
system2(file.path(R.home("bin"), "Rscript"), ...)
}
works <- function(pkg) {
## Need to call in separate R session to not use up all DLLs
rcmd <- sprintf("cat(isTRUE(requireNamespace('%s')))", pkg)
as.logical(rscript(args=c("-e", dQuote(rcmd)), stdout=TRUE))
}
@HenrikBengtsson
HenrikBengtsson / readtable-ex.R
Last active August 29, 2015 14:24
read.table() with named colClasses
source("https://gist.githubusercontent.com/HenrikBengtsson/ed1eeb41a1b4d6c43b47/raw/ebe58f76e518dd014423bea466a5c93d2efd3c99/readtable-fix.R")
kkk <- c("a\tb",
"3.14\tx")
colClasses <- c(a="numeric", b="character")
data <- read.table(textConnection(kkk),
sep="\t",
header = TRUE,
colClasses = colClasses)

The overhead of method dispatching can be noticable, particularly if done on small objects and it adds up if doing many many times. For instance,

> add <- function(a,b) UseMethod("add")
> add.integer <- function(a,b) a+b

gives with method dispatching:

> system.time({ for (kk in 1:10e6) add(1L, 2L) })
  user  system elapsed
35.43 0.05 35.70
@HenrikBengtsson
HenrikBengtsson / Agilent_and_TABs.md
Last active September 23, 2015 19:32
Agilent adds random TABs to end of data rows?!?

Agilent adds random TABs to end of data rows?!?

Below are the first few lines(*) of two files exported from the Agilent CytoGenomics Software. These files are plain text files with a few "metadata" rows before the actual tab-delimited data begins. The tab-delimited data appears to have a header row with tab-delimited column names followed by corresponding tab-delimited data rows. But it also appears as if there are "random" TABs (ASCII 0x09 = '\t') added to the end of either to header row or the data rows. What's up with that?

PS. (*) I cut out the top few lines by reading the files as a binary file in order to remove the risk of it being me/my editor introducing these TAB outliers. Also, I've verified using binary diff that these example files are identicial to the top of the full files.

Function to count TABs in a string

> countTABs <- function(x) sum(charToRaw(x) == cha
@HenrikBengtsson
HenrikBengtsson / rpkg_svn2git.R
Last active October 4, 2016 19:09
Transfer R package from SVN to Git repository
#######################################################################
# rforge2git.R by Henrik Bengtsson
#
# USAGE:
#
# Rscript rpkg_svn2git.R <pkg>
#
# EXAMPLE:
# Rscript rpkg_svn2git.R r-forge:r-dots/R.menu
# Rscript rpkg_svn2git.R bioc-devel:illuminaio
@HenrikBengtsson
HenrikBengtsson / demo.R
Last active August 29, 2015 14:20
Mixed-ordering of string containing roman numerals
source("mixedsortRoman.R")
# Basics
yi <- c(1, 5, 10, 50, 100)
print(yi)
## [1] 1 5 10 50 100
yr <- as.character(as.roman(yi))
print(yr)
## [1] "I" "V" "X" "L" "C"
@HenrikBengtsson
HenrikBengtsson / README.md
Last active January 9, 2023 04:06
RSP: Including file content in Markdown

RSP: Including file into Markdown

This can be done using the <%@include file="..."%> RSP preprocessing construct.

From R:

> R.rsp::rfile('main.md.rsp', postprocess=FALSE)
RspFileProduct:
Pathname: C:/Users/hb/braju.com.R/R.rsp/main.md
File size: 66 bytes