Concatenating text nicely with commas and “and”s

Wow, this blog is quiet at the moment. I can’t even remember if I’ve written this anywhere but I had a liver transplant in May and I’ve been repeatedly hospitalised with complications ever since. So work and life are a little slow. I’m hoping to be back to full health in a few weeks.

Anyway, I was just writing some Shiny code and I have a dynamic text output consisting of one, two, or many pieces of text which needs to have commas and “and”s added to make natural English, like so:

Orange. Orange and apple. Orange, apple, and banana. Orange, apple, banana, and grapefruit.

I really toyed with the idea of just using commas and not worrying about “and”s but I decided that it was only a quick job and it would make the world more attractive. So if you’ve just Googled this, here’s my solution:

theWords = list(LETTERS[1], LETTERS[2:3], LETTERS[4:6])

vapply(theWords, function(x) {

if(length(x) == 1){

x = x # Do nothing!

} else if(length(x) == 2){

x = paste0(x[1], " and ", x[2])
} else if(length(x) > 2){

x = paste0(paste0(x[-length(x)], ", ", collapse = ""),
"and ", x[length(x)])

}, "A")

Notice the use of vapply, not lapply, because Thou Shalt Use Vapply.

Working as a coder at a technology company versus working as a coder in other contexts

I was just skimming a book about Python and I found a rather interesting quote:

“Programming as a profession is only moderately interesting. It can be a good job, but you could make about the same money and be happier running a fast food joint. You’re much better off using code as your secret weapon in another profession.

People who can code in the world of technology companies are a dime a dozen and get no respect. People who can code in biology, medicine, government, sociology, physics, history, and mathematics are respected and can do amazing things to advance those disciplines.”

Looking at salaries for developers as well as reading the experiences of coders in technology companies this really seems to ring true. I’m the other kind, I’ve picked up a bit of code in order to do my day job which is really focused on collecting and analysing user experience data. And actually I do get respect and I have been able to do a lot of new things that nobody was doing before I got started.

Quite inspiring words, really. I feel a renewed urge to develop my programming skills within the current role that I have to try to push things even further and will stop daydreaming about what it would be like to be a “real” programmer.



The NHS, hammers, and spanners, part 27

The scene- a group of NHS managers stand around a bolt fixed to the wall, repeatedly hitting it with a hammer in a futile attempt to remove it, and a valuable painting, from the wall. Every manager is holding a new, shiny, and identical, hammer.
A colleague and I walk in and, wielding a spanner, easily remove it from the wall, and hand the painting to the most senior person in the room. We turn to leave.
*gesturing to a huge bank of paintings on the wall, each affixed with a single bolt* “Can you port what you just did to hammers?”
Without a word, we trudge wearily out.

Open document formats and why we need them

The whole world is moving to open document formats for the obvious reason that in order to read and write documents one shouldn’t need to waste money on Microsoft’s increasingly ungainly Office application when perfectly good free and open source alternatives exist (thinking particularly of the mighty LibreOffice). The UK Government has formally adopted an open standard for all of its documents.

Despite this many public bodies in the UK, including the NHS, councils, and many other organisations, continue to use Microsoft’s proprietary formats for documents. It may seem to the casual user that the arguments for proprietary versus open document formats are rather arcane. I would like to tell a story from my own life that illustrates why in actual fact every citizen and certainly every public body should be interested in open document formats.

My wife has just applied for a job. The application process involved filling out a Microsoft Word document. Naturally, she wanted to appear efficient and professional and so was concerned that if she used LibreOffice the tables within the document would be mangled. We don’t own a copy of Microsoft Word. I consider it to be a waste of money and besides object to funding Microsoft since they have done so much to oppose open standards.

As a consequence, I have spent three hours, which I could have spent working, or looking after my family, or just relaxing from doing either, using a huge pile of technology (a Citrix enabled Word program that I can access through my job, Google’s own document preview function in their email, a .pdf to .docx converter, and LibreOffice) to ensure that the document renders nicely.

This was further complicated by the fact that the document itself, in its original state, was badly formatted, and so I had to ensure that the bad formatting left in the document really was in the original or if we had introduced it using LibreOffice.

I’m a computer programmer fluent in R, moderately proficient in PHP/ MySQL, and manage two Linux servers. Not only that but I have extensive experience with Microsoft Office from my PhD. I actually made the document better.

Not everybody will want to or be able to pay for Microsoft Office. Not everybody will know somebody who can fiddle around with the huge pile of technology necessary to make the form look reasonable. But I think it’s pretty obvious that we don’t want to make job decisions based on an application form’s looking unprofessional because the applicant didn’t have Office and wasn’t married to a geek.

And that, ladies and gentlemen, is why we need open document standards.

Backing up a remote MySQL database on a remote server using cron

This is more a collection of links, really, than a proper blog post, but I just spent quite a while figuring out how all of these different technologies all fit together to do the job I wanted so if you want to do what it says on the tin of this blog post then you should find it useful.

I have a remote server running a MySQL database. I wish to backup this server daily. I would rather do this on my other remote server than on my local machine because, obviously, my server is always switched on whereas my local machine isn’t. So the task is to dump a MySQL database to a file and upload it to a remote server.

To begin, you’re going to make sure that the server with the MySQL database on it can access the other server. This is easiest using ssh key login. I use this for all my servers and all of my local machines. There is a summary here. The beauty of using a key based login is it eliminates the need for passwords, which is not only secure in general but also means you do not need to store passwords in cron scripts or anywhere else on the computer which is highly insecure. Unless you really know what you’re doing you should never do this.

Next you’ll need to create a user on your MySQL and give it SELECT privileges so it can read the data to make the backup. This is very simple

GRANT SELECT, LOCK TABLES ON mydatabase.* to ‘backup’@’localhost’ IDENTIFIED BY ‘password’;

Now you need to set up a cron job to back up the database every day. There’s some stuff about it here, but you won’t need the bit about passwords because we’re using key authentication. As I mentioned above, it’s easier and safer. We’re also going to add some terminal magic to automatically change the file name every day to the current date so you can have daily backups on the server. Here’s the finished cron entry.

0 1 * * * mysqldump -u backup SUCE | ssh chrisbeeley@ “cat > ~/backupDB/$(date +\%F)_SUCE_DB.sql”

This runs the backup operation every day at 1am.

You’ll need to clean up the old files occasionally, otherwise your server will get full of very old copies of the database. The following command will remove all files from the directory that are older than 31 days old:

find ~/backupDB -type f -mtime +31 -exec rm {} +

BE VERY CAREFUL with this command. If you run it with the wrong directory or have any strange errors with it you may find yourself deleting a lot of files that you did not want to delete. ALWAYS be safe and test by running find ~/backupDB -type f -mtime +31 first and reviewing the files which come back. Only when you’re happy that it’s finding the right files should you add -exec rm{} +

I haven’t added this to a cron job, partly because it doesn’t seem worth the risk (the command is safe in my environment today, but always be wary of PEBKAC, even for yourself), and also because it means I can choose when to flush the old database files, just in case I find some current problem in the database which I can fix with an older version.

Make your own HTML tables in R

I’ve avoid writing HTML tables by hand in R, reasoning that it would be far too complicated. But the problem with using packages like pander and rmarkdown, much as I love those packages, is you can’t fine tune the outputs. So if a colleague asks you to add little extra touches for a report you can’t really do it.

So it was a nice surprise to find that it’s actually not that hard. Here’s a table with 3 columns which looks very nice with bootstrap (don’t know what the styling options are on WordPress so it just looks unformatted here)

Service quality 95 90
Promoter 90 85
SUCE returns 1000 1200


paste0(c("<table class='table table-striped'>", 
  "<th>Score</th><th>Current quarter</th><th>Previous quarter</th>",
  "<tr>", paste0("<td>", c("Service quality", 95, 90), "</td>"), "</tr>",
  "<tr>", paste0("<td>", c("Promoter", 90, 85), "</td>"), "</tr>",
  "<tr>", paste0("<td>", c("SUCE returns", 1000, 1200), "</td>"), "</tr>",
  "</table>"), collapse = "")

Obviously you can make it dynamic pretty easily, the table above is from a Shiny app I’m currently developing, I’ve just replaced the numbers with static values for the blog post. So the next time someone asks me to style a table it should be pretty easy.

G++ error compiling on R

I was installing R packages (the httpuv package, but I imagine others would generate the same problem) on my Ubuntu server today and I got a very strange error message:

g++: error: Welcome: No such file or directory
g++: error: to: No such file or directory
g++: error: R!: No such file or directory

It was a problem with my file. That’s why it looks funny, because R is saying “Welcome to R”, but for some reason this is messing with g++. In my case it seemed to be generated by running R as sudo, but I need to do that to make the package library writable. Just invoke R at the terminal with:

R --no-site-file

and it will be fixed.