### Useful R snippets

18Mar12

In this post we collect several R one- or few-liners that we consider useful. As our minds tend to forget these little fragments we jot them down here so we will find them again. Continue reading ‘Useful R snippets’

### multi-platform real-time ‘intro’ in R using rdyncall

29Jul11

Below is a real-time audio-visual multimedia demonstration – or in short ‘an intro’ – written in 100% pure R. It requires no compilation and runs across major platforms via the package rdyncall and preinstalled precompiled standard libraries such as OpenGL and SDL libraries. This ‘happy-birthday’ production runs about 3 minutes and comprises typical effects of the home computer oldschool demoscene era such as a rotating cube, multi-layer star field, text scrollers, still images and flashes while playing a nice Amiga Soundtracker module tune. Check out the video screen-cast (with sound) or enjoy a smooth framerate using the R version at this website.

### Using R, Sweave and Latex to integrate animations into PDFs

18Apr11

The first week of April I attended an excellent workshop on biplots held by Michael Greenacre and Oleg Nenadić at the Gesis Institute in Cologne, Germany. Throughout his presentations, Michael used animations to visualize the concepts he was explaining. He also included  animations in some of his papers. This inspired me to do this post in which I will show how to use LaTex, R and Sweave to include animations in a PDF document. Here is the PDF document we will create (on MacOS the standard PDF viewer may not be able to play the animations, but Adobe Reader will). For this post some basic knowledge about Sweave is assumed. Continue reading ‘Using R, Sweave and Latex to integrate animations into PDFs’

### Animate .gif images in R / ImageMagick

21Nov10

Yesterday I surfed the web looking for 3D wireframe examples to explain linear models in class. I stumbled across this site where animated 3D wireframe plots are outputted by SAS.  Below I did something similar in R. This post shows the few steps of how to create an animated .gif file using R and ImageMagick. Here I assume that you have ImageMagick installed on your computer. As far as I know it is also possible to produce animated .gif files using R only, e.g. with write.gif() from the caTools package. But using ImageMagick is straighforward, gives you control over the conversion and .gif production and is the free standard program for conversion. Continue reading ‘Animate .gif images in R / ImageMagick’

### Playing with the ‘playwith’ package

23Mar10

Abilities of R for creating graphics is great, but one thing I always missed is the possibility of creating interactive plots and being able to look at graphs while changing one ore more parameters. I know that there is rggobi, but so far I always ran into problems with flexibility each time I wanted to use it. So I kept on searching until I found playwith which is “an R package, providing a GTK+ graphical user interface for editing and interacting with R plots” as its homepage says. The homepage includes a lot of screenshots with code snippets so this post doesn’t intend to give an extensive review about the possibilities of the playwith package to the reader. All I want to do now is present a small application of it. Continue reading ‘Playing with the ‘playwith’ package’

### R vs. Matlab – a small example

15Feb10

At the institute I’m working quite a lot of people prefer using Matlab and only a few of them know about R. Today one of my colleagues — who is also an eager user of Matlab — ran into the following problem:

• He had a vector $v$ in hand which consisted of $\frac{n(n+1)}{2}$ elements.
• He wanted to reshape this data into an n×n matrix $M$, where the element $M_{ij}$ is equal to $v_{k+j}I(j<=n-i+1)$ with $k=\frac{(2n-i+2)(i-1)}{2}$ and $I(j <= n-i+1)=1$ if the condition $j <= n-i+1$ is satisfied and $0$ otherwise. In other words, the first $(n-i+1)$th element of the $i$th row of $M$ is equal to the vector $(v_{k+1},v_{k+2},\ldots,v_{k+n-i+1})$ and the remaining elements are zero.

He struggled for long minutes of how he should design a loop for doing this task. Of course writing such a loop is not a highly difficult task, but why would we waste our time, if we can get the same result in a single line of R code?

### Progress bars in R (part II) – a wrapper for apply functions

11Jan10

Update (2014/08/24): Since October 2013 the pbapply package is available that has picked up the idea of integrating progress bars into the apply family of functions outlined in this post.

In a previous post I gave some examples of how to make a progress bar in R. In the examples the bars were created within loops. Very often though I have situations where I would like have a progress bar when using apply(). The plyr package provides several apply-like functions also including progress bars, so one could have a look here and use a plyr function instead of apply if possible. Anyway, here comes a wrapper for apply, lapply and sapply that has a progressbar. It seems to work although one known issue is the use of vectors (like c(1,2)with the MARGIN argument in apply_pb(). Also you can see in the performance comparison below that the wrapper causes overhead to a considerable extent, which is the main drawback of this approach. Continue reading ‘Progress bars in R (part II) – a wrapper for apply functions’