Skip to navigation

r-cran-rmysql

January 20th, 2010

Memo to self: I installed RMySQL using

sudo aptitude install r-cran-rmysql

because when I used

install.packages("RMySQL")

it asked my to specify the MySQL libraries and headers and whatnot and don’t know where they are.

Use R to help with lottery drawing

January 7th, 2010

When you start using R, it can be confusing that if you assign something to an object you do not get visual feedback:

x <- rep(1:3,5)

You have to then type x to see it:

> x
 [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3

You can exploit this fact for doing a quick-and-dirty lottery. Assume you have a file with email addresses, one per line, called "entries.txt". First, read them and assign them to an object:

entries <- readLines("entries.txt")

Next, create an index of numbers of the same length but in random order:

index <- sample(1:length(entries),length(entries))

Note that you don't see it unless you type index. Combine index and emails in a dataframe (also not directly visible):

dat <- data.frame(cbind(index,entries))

Now, go to random.org and draw a number from the index. Type dat to reveal who you drew. This method makes it a bit more "blind" and a bit less likely that you'll cheat.

Small hyperref error solved

November 9th, 2009

For the record. I got an error running hyperref:

Paragraph ended before \Hy@setref@link was complete

This was due to having a regulare closing parenthesis directly after the curly bracket that closed a \ref, like this: (see section \ref{section-1}). Putting in a space between the closing curly bracket and parenthesis solved the problem: ... section-1} )

Nice R example from r-help

November 5th, 2009

I don’t know the first thing about programming, but sometimes you see something and appreciate how elegant it is. This small solution from r-help is a good example. Someone wanted, given a vector c('p','p','t','t','t','p','k','t') to produce NA NA 1 2 3

test <- c('p','p','t','t','t','p','k','t')
v <- cumsum(ind <- test == 't')
v[!ind] <- NA

I didn't know you could assign something to an object and use it at the same time. Very neat. So if you wanted this all at once, you could also do:

v <- ifelse(cumsum(ind <- test=='t')==0,NA,cumsum(ind))

or

ifelse((v <- cumsum(test=='t'))==0,NA,v)

Maybe Dimitris' solution is in fact preferred for R, using indexing instead of if/ else, but I don't know enough about R to know whether that makes a difference.

Mutt: set dynamic email signature from shell command output

October 3rd, 2009

I want to set my signature when mailing to the r-help mailing list to contain information about the R version I have currently installed. Creating a bash script that directly echoes the several lines that I want to have in my signature and executing that in the muttrc using backticks gives a “broken pipe” error.

One solution (I’m sure there are others, maybe better ones) is to make the shell script create a temporary signature file and echo the path to that file, then execute the shell script using backticks.

Shell script (don’t forget to chmod 744):

#!/bin/bash                                                                                          

thesigfile=~/.mutt/rsig

echo  "Marianne Promberger PhD, King's College London
http://promberger.info" > $thesigfile
R --version | head -n 1 >> $thesigfile
cat /etc/issue | head -n 1 | cut -d " " -f 1-2 >> $thesigfile
echo $thesigfile

muttrc:

send-hook '~t r-help' 'set signature=`~/.mutt/signature_r-help`'