R S4 objects and overloaded data.frame

Here is some code extracted from this answer http://stackoverflow.com/a/14607290 to the question “How to create a dataframe of user defined S4 classes in R“. The answer and question are well worth reading, but I wanted to have the code example in one place without the intervening comments:

# Create S4 class person(name,age)
  slots = c(

# Create subsetting operator
  function(x, i, j) {
    initialize(x, name=x@name[i], age=x@age[i])

# Create overload for format()
format.person <- function(x) {
  paste0(x@name, ", ", x@age)

# Create overload for as.data.frame()
as.data.frame.person <-
  function(x, row.names=NULL, optional=FALSE)
  if (is.null(row.names))
    row.names <- x@name
    value <- list(x)
    attr(value, "row.names") <- row.names
    class(value) <- "data.frame"

# Create overload for c()
c.person = function(...) {
  args = list(...)
      name=sapply(args, function(x) x@name),  
      age=sapply(args, function(x) x@age)

# Demonstrate the code above; writes "John, 20"
            new("person", name="Tom", age=30),
            new("person", name="John", age=20)


Conda Install R packages Tcl Error – solved

When I try to install R packages in conda, sometimes I get the following error:

. . .
Error: .onLoad failed in loadNamespace() for 'tcltk', details:
  call: fun(libname, pkgname)
  error: Can't find a usable init.tcl in the following directories: 
    /opt/anaconda1anaconda2anaconda3/lib/tcl8.5 ./lib/tcl8.5 ./lib/tcl8.5 ./library ./library ./tcl8.5.18/library ./tcl8.5.18/library

This is happening because install.packages is trying to paint the repository picker window – I don’t understand why it’s not using the command-line repository picker.

My workaround was to set the working directory to


so that


was on my path. Then it painted the repository-picker just fine, and I was able to install my update.

An alternative workaround might have been to ssh to the same box (without X forwarding).

bash get directory for script

First, some warnings from the BashFAQ:

  • Your script does not actually have a location! Wherever the bytes end up coming from, there is no “one canonical path” for it. Never.

  • $0 is NOT the answer to your problem. If you think it is, you can either stop reading and write more bugs, or you can accept this and read on.

The BashFAQ also describes BASH_SOURCE and the applicable caveats.  Here’s some workable, albeit fallible, code from http://stackoverflow.com/questions/59895/can-a-bash-script-tell-which-directory-it-is-stored-in:

# find directory where script may reside
# resolve $SOURCE till not a symlink
while [ -h "$SOURCE" ]; do 
  DIR="$( cd -P "$(dirname "$SOURCE")" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd && \
  echo x)"
echo Script $0 resides in directory $DIR

docker subverts ufw

The Problem

I found on Ubuntu that Docker modifies iptables such that ufw cannot effectively control incoming connections, as documented here:


A Workaround

This link also points out that it is possible to override this behavior by adding the --iptables=false option to the command line that starts the Docker daemon. For a systemd example, this option is appended to the ExecStart line in

ExecStart=/usr/bin/dockerd -H fd:// –iptables=false

A Side-Effect of the Workaround

On Debian, with dockerd lauched with the --iptables=false option, I tried to do “docker build” for a DockerFile that included:

RUN npm install -g ethercalc pm2

But, this failed with:

npm info retry will retry, error on last attempt: Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443

So, I had to

  • restart dockerd without the --iptables=false option
  • do the docker build
  • restart dockerd with the --iptables=false option

I would like to find a more elegant solution!!

Update 1:

On a Debian without any bridges, I don’t notice a difference in the output of ‘iptables -L’ with or without the option set.  So, I’m a bit stumped. Could this issue only affect Ubuntu? Could it only affect machines that have bridges?

Update 2:

I tried this again with my Ubuntu machine that does indeed have bridges, and I found that ‘iptables -L’ output is unaffected, yet, when I started dockerd without the --iptables=false option, my machine accepted connections even when ufw was set to reject all incoming connections.  So, I’m still stumped.

Percent complete R function

R function to print percent complete.

If you use
method = message
you may want to use
suffix = ” % complete”

pctComplete <- function(
  progress = 0, last.progress = 0,
  total = 100, increment = 10,
  suffix = " ", method = cat, ...) 
  if (increment < 1)
    increment <- 1
  if (total < 0)
    total <- 100
  pct <- round(progress * 100 / total)
  if ( pct >= increment 
               + last.progress
               - last.progress%% increment )
    method(paste("", pct, suffix, sep = ""),


aggregate free memory and swap

free -b reveals the total, used, free, shared, cache, and available memory.

Here’s the awk code to extract the total of the free physical memory and the free swap.

First, observe the free memory and swap, in bytes

# free -b | \
awk 'BEGIN{s=0}; NR<2{print $3}; NR>1{print $4}'

Now, total the free memory and swap

# free -b | \
awk 'BEGIN{s=0};NR>1{n=$4;s=s+n;};END{print s}'

The sum is slightly different because the second command was run at a different time.

Why I chose MDI during R installation

When installing R on a machine that is shared among several users, some of which are accustomed to the Mac OS, I had to choose between the Multiple Document Interface (MDI) and the Single Document Interface (SDI). The MDI article on Wikipedia pointed out that the MDI interface relates all documents to a common menu bar, which is what Mac users are accustomed to; Windows users probably have encountered both, since Office applications have used at times (depending on the generation) SDI and MDI. Therefore, to make the experience less alien for Mac users on a Windows machine, I installed R configured to use MDI.

Ubuntu temperature monitor


I am trying to figure out how to monitor temperature on my Ubuntu system, and I found this thread:
So I started installing the packages recommended in response #5, but response #4 may be good, too.

$ sudo apt-get install lm-sensors hddtemp
$ sudo sensors-detect
$ sudo /etc/init.d/kmod start
[ ok ] Starting kmod (via systemctl): kmod.service.
$ sensors
Adapter: Virtual device
temp1:        +54.0°C  (crit = +120.0°C)

Adapter: ISA adapter
Physical id 0:  +53.0°C  (high = +100.0°C, crit = +100.0°C)
Core 0:         +52.0°C  (high = +100.0°C, crit = +100.0°C)
Core 1:         +51.0°C  (high = +100.0°C, crit = +100.0°C)

sed script for key:value to csv

I had files with one key-value pair per line, e.g.:

Acquired Name: foo
Acquired Date: 1066-10-14T10:00:00Z

I wanted to extract all of the fields to one CSV line per file.

This is the sed script that I used. As it happens, this would also work if all of the input files were concatenated (because I used the ‘d’ command rather than ‘q’).

/Acquired Name:/ { 
  s/.*Acquired Name: \(.*\)./"\1"/ ; h ; }
/Acquired Date:/ {
  s/.*Acquired Date: \(.*\)./"\1"/ ; x ; G ; s/\n/,/M ; h ; }
/Acquired Time:/ {
  s/.*Acquired Time: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Job Code:/ {
  s/.*Job Code: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Instrument:/ {
  s/.*Instrument: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Bottle Number:/ {
  s/.*Bottle Number: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Inlet Method:/ {
  s/.*Inlet Method: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/MS Method:/ {
  s/.*MS Method: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; h ; }
/Tune Method:/ {
  s/.*Tune Method: \(.*\)./"\1"/ ; x ; G ;
  s/\n/,/M ; p ; d ; }

The manual for sed
explains the commands used:

  • d – Delete the pattern space; immediately start next cycle with next input line.
  • h – Copy the pattern space to the hold space
  • G – Append the hold space to the pattern space
  • p – Print the pattern space
  • s/pattern/replacement/ – Substitute pattern with replacement
  • s/pattern/replacement/M – Do multi-line substitution; see this
  • x – Exchange the pattern space and the hold space



From http://linux.die.net/man/1/colordiff

colordiff is a wrapper for diff and produces the same output as diff but with coloured syntax highlighting at the command line to improve readability.

The output is similar to how a diff-generated patch might appear in Vim or Emacs with the appropriate syntax highlighting options enabled.

The colour schemes can be read from a central configuration file or from a local user ~/.colordiffrc file.

To enjoy on Ubuntu:

sudo apt install colordiff