Sharing files from host machine on virtualbox

I’ve really started getting into virtual machines recently. Firstly because I like to try different flavours of Linux and constantly reinstalling partitions is starting to get a bit of a drag, especially when it often takes ages to get the graphical desktop to work because of my silly graphics card. Secondly, and more importantly, because I’m finding if you want to do programming, or especially write about programming, you often need a “clean” system or a system set up a particular way to test things and work on multiple things at once. So the code I write for Shiny is based on the newest 0.12 version. However, my server is all the way back on 0.7 for various reasons to do with browser compatibility. So this means I need a local 0.7 installation to test programs that I’m updating on the server. Or sometimes it can be important to check what the dependencies for your programs are. You may have installed something a year ago that makes everything work beautifully that you’ve long since forgotten about, and if you tell someone else to do what you’re doing it won’t work on their machine. In the Linux world technologies like Docker which package up whole environments at the operating system level and help you to manage dependencies (amongst other things) are starting to spring up and in the R world things like packrat roll up packages into bundles in order that all package versions are the same and dependencies satisfied.

I haven’t learned those technologies yet so I’m going to stick with virtualisation for now (Docker is probably a bit of a sledgehammer to crack a nut for me, anyway, really).

I am mainly making this post because I keep forgetting how to access the host file system on the guest system, so I’ll be able to refer to it in future. I hope it helps you. It’s based on Ubuntu. Some of it is probably similar in other OS’s, I really have no idea I’m afraid.

Go to Devices (on the Virtualbox host machine, i.e. on the window that contains the graphical environment of the guest machine) and select Insert Guest Additions CD image. It will be mounted for you. You need to find out where it’s mounted so you can access it from the terminal. Mine mounted at /media/chris/VBOXADDITIONS_4.3.10_93012/.

Run the Linux script within the mounted folder, on my machine it was:

cd /media/chris/VBOXADDITIONS_4.3.10_93012/

sudo sh ./

Presuming this works okay, reboot the virtual machine, and now select Devices… Shared folder settings on the host machine. Click “Add shared folder” over on the right. Find the folder and click “Auto-mount”. Select “Read only” (a lot of people seem to do this, don’t know why, seems safer anyway).

If you restart the folder should now be in /media. If you get “Unknown filesystem”, “Cannot open folder”, or something like that then you need the advice from here.

From the VBox User Manual:

Access to auto-mounted shared folders is only granted to the user group vboxsf, which is created by the VirtualBox Guest Additions installer. Hence guest users have to be member of that group to have read/write access or to have read-only access in case the folder is not mapped writable.

Therefore run:

sudo adduser yourusername vboxsf

Restart and it should all work. I restarted the virtual machine after each stage, just in case. If it still doesn’t work, sorry, off to the forums with you.

Single file Shiny app to take GET parameters and return date range input boxes

I’ve spent quite a long time fiddling around with some code for the feedback website for which I am responsible for all the reporting so we can distribute custom URLs to individuals who have specific reports they want to run. We use Shiny to do custom reports, the application lives here, and many of the people who run the reports (our staff in Nottinghamshire Healthcare NHS Trust, mainly) would welcome the ability to have the date range preselected by means of a URL containing GET parameters which they could just click so that the date range is pre-selected.

The code is not very well commented and does not handle bad URLs very well. In time I may fix this or I may just tell people that if they can’t figure out how to write well formed GET querys that I will make some for them.

Nonetheless, I thought it would be useful to share in case anyone else is doing something similar and wants to get started or steal some of the code. It’s all hosted as a single file shiny app on Gist here which means that by the magic of Shiny you need only run:

runGist(gist = "79a592a83ccda153efae")

for a demo. You will need to paste the correct GET parameters in yourself, obviously. The app does one of three things, all selectable using a type=”xxx” GET parameter. There follows a list with a URL that does something for each one. The output to the right of the date widget is just some debugging stuff so I can see what’s happening as I go along, you may wish to keep something like that in anything you write while you’re building it.

You can run it on your own machine or I host it on my server, if you run on your own machine just add to the end of the address bar everything after and including the “?”, or click the link to see it on my server. It looks a bit funny because I have an old version of Shiny on the server, I can’t upgrade at the moment because of various issues with IE7 etc. but hope to soon. It works, anyway, which is the point, and looks fine on the newest version of Shiny (0.12 as I write this).


This is the simplest one, you just put the date (begin and/ or ending) straight in as dmy format (which UK people will prefer, more on which later) like this:

Missing out either is fine, if you miss the beginning one it will just go to a year ago and if you miss the end one it picks today.


This is for people who want to find the previous n months, rounding to months (e.g., today, in September, n = 3 would bring back the three months previous to September- June, July, and August).


And lastly and most complicated-ly, people who want to use just quarters and years. It took me ages to debug this because the quarter() function from the marvellous lubridate package returns January as 1, April as 2, etc., which apparently is the US way of doing things, whereas in the UK January is 4 (of the previous year, obviously), April is 1, July is 2, etc. I never had any idea there were different systems. In fact, there are different systems all over the world. Amazing what you learn some days.

Converting the output of quarter() to what I want entailed moving from a year that cycled 1, 2, 3, 4 to a year that cycled 4, 1, 2, 3 and so I used our old friend modulo arithmetic to convert between the two (%% in R). I won’t say anything further about that because I’m completely hopeless when it comes to modulo arithmetic and if I’m honest the code came about more through trial and error than anything else.

And that’s basically it. Browse the code, run the application, I hope it’s of some use to someone looking to do something similar.

Producing headings in Word files programmatically using knitr

I love knitr. Sometimes I have to send people documents in Word. It works beautifully except when I want to produce headings programmatically in code using cat(). This is fine in HTML:

sapply(headings, function(x){
  cat(paste0("<h1>", x, "</h1>"))
  cat(paste0("<p>", rep("Some text here", 10), "</p>"))

However, when writing Word .docx files using Rmarkdown the heading tags are not recognised. Well, they’re not printed, so they obviously are recognised, but they don’t work.

I’ve struggled for a long time to understand how to produce headings in Rmarkdown and to be honest in the past I’ve actually done it with a lot of guesswork and some silly hacky unnecessary code. Today I have finally figured out a foolproof way to do it very easily every time, so I present the whole document to you and to my future self when I inevitably forget how to do it:

title: "Minimal example"
author: "Chris Beeley"
date: "1 September 2015"
output: word_document

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)


```{r, results = 'asis', echo = FALSE}

headings = letters[1:4]

  sapply(headings, function(x){
    cat(paste0("#", x, "\n"))
    cat(paste0("<p>", rep("Some text here", 10), "</p>"))


That’s it! Don’t forget the invisible() because otherwise if you use lists and things like that you’ll get the names of the list at the end.