Skip to content

Instantly share code, notes, and snippets.

View HenrikBengtsson's full-sized avatar

Henrik Bengtsson HenrikBengtsson

View GitHub Profile
@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 / 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 / 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 / ex-.Last.error.R
Last active May 6, 2016 01:17
Recording last stop() condition in .Last.error
## Requires options(error=recordStop)
## stop() at the prompt
stop("Hello")
## Error: Hello
print(.Last.error)
## <simpleError in stop("Hello"): Hello>
str(.Last.error)
# List of 3
# $ message: chr "woops"
--- R-3.2.1/src/main/memory.c.orig 2015-07-31 23:15:07.017151621 -0700
+++ R-3.2.1/src/main/memory.c 2015-07-31 23:17:10.185150073 -0700
@@ -3724,11 +3724,21 @@
static FILE *R_MemReportingOutfile;
static R_size_t R_MemReportingThreshold;
+static void printLineNum(FILE *file, SEXP srcref) {
+ if (srcref && !isNull(srcref)) {
+ int line = asInteger(srcref);
+ fprintf(file, "#%d ", line);
@HenrikBengtsson
HenrikBengtsson / future-remote-local-rendering.md
Last active September 24, 2016 22:03
Checking local rendering of remotely generated objects

Remote evaluation local display

Visualization

Works:

  • Base R Graphics
  • ggplot2
  • plotly
  • highcharter
  • rbokeh
@HenrikBengtsson
HenrikBengtsson / HomomorphicEncryption-example.R
Created November 7, 2016 01:43
Example of homomorphic encryption and operators in R
## Example adopted and clarified from
## http://www.louisaslett.com/HomomorphicEncryption/
library("HomomorphicEncryption")
p <- pars("FandV")
k <- keygen(p)
## Unencrypted data
x <- c(42, 34)
y <- c(7, 5)
@HenrikBengtsson
HenrikBengtsson / SVN-REVISION.md
Created January 11, 2017 00:32
A one-line version of the SVN-REVISION hack on https://github.com/wch/r-source/wiki

A one-line version of the SVN-REVISION hack on https://github.com/wch/r-source/wiki:

printf "Revision: %s\nLast Changed Date: %s\n" `git log --format=%B -n 1 | tr '\n' ' ' | sed -E 's/.*git-svn-id: https:\/\/svn.r-project.org\/R\/[^@]*@([0-9]+).*/\1/'` `git log -1 --pretty=format:"%ad" --date=iso | sed -E 's/ .*//'` > SVN-REVISION
@HenrikBengtsson
HenrikBengtsson / xerox_accounting_template.ppd
Last active January 6, 2022 19:39
Linux: Pass Accounting information to Xerox Printer via Postscript Printer Description (PPD)
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*% GENERAL ACCOUNTING
*%
*% Instructions:
*%
*% 1. Identify the accounting option or options used by the printer.
*%
*% 2. Replace the '{{...}}' placeholders with corresponding information
*% (without curly brackets). For example, '<22>XRX_USERID,{{userid}}<22>'
*% should become '<22>XRX_USERID,12345<22>'.
@HenrikBengtsson
HenrikBengtsson / ascii-mandelbrot.R
Last active July 10, 2017 18:05
ASCII Mandelbrot renderer (One-to-one R interpretation of C implementation)
## A one-to-one R interpretation of the minimalist C implementation
## of the ASCII Mandelbrot renderer in http://codepad.org/nINhbWVy:
##
## main(k){float i,j,r,x,y=-16;while(puts(""),y++<15)for(x
## =0;x++<84;putchar(" .:-;!/>)|&IH%*#"[k&15]))for(i=k=r=0;
## j=r*r-i*i-2+x/25,i=2*r*i+y/10,j*j+i*i<11&&k++<111;r=j);}
main <- function(k = 0) {
y <- -16
while({ cat("\n"); y <- y + 1L } < 16) {