Skip to content

Instantly share code, notes, and snippets.

View HenrikBengtsson's full-sized avatar

Henrik Bengtsson HenrikBengtsson

View GitHub Profile
@HenrikBengtsson
HenrikBengtsson / speedtest.html
Last active August 29, 2015 14:04
Example of a Speedtest HTML report based on speedtest-cli, R, ggplot and R.rsp.
<html>
<style>
body {
font-size: 80%;
}
</style>
<body>
<head>
<title>Speedtest benchmarks (2014-02-23 -- 2014-04-19)</title>
</head>
@HenrikBengtsson
HenrikBengtsson / table-pandoc.md.rsp
Last active August 29, 2015 14:11
RSP example: A Table
<%--------------------------------------------------------------
Usage:
md <- R.rsp::rfile('table-pandoc.md.rsp', postprocess=FALSE)
pdf <- gsub("md$", "pdf", md)
system2("pandoc", args=c(normalizePath(md), "-o", pdf))
!pdf # Opens PDF in viewer
Requires Pandoc [http://johnmacfarlane.net/pandoc/] and
LaTeX, because Pandoc uses LaTeX to generate PDFs.
--------------------------------------------------------------%>
@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
@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 / 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 / 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

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 / 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)
@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))
}