Starting from scratch – building a package in R

For the first time, I am going to share something more related to my master thesis. When I started this thesis, I did not know how to use R. In order to learn R, I started using DataCamp, which is a series of interactive courses. You can start from scratch and build your skills step by step. My favorite course so far is called “Writing Functions in R”. During the course, you are told:

If you need to copy something three times or more – build a function.

As a rookie, it sounds complicated, but in fact it is really simple AND it will save you so much trouble later. Defining a function will allow you to produce code that is easy to read, easy to reuse and easy to share with colleagues.  A function can be defined by the following syntax:

my_function <- function(arguments){
  Function Body

my_function  can be any valid variable name, however, it is a good idea to avoid names used elsewhere in R. Arguments (also called formals) can be any R object that is needed for my_function to run,  for example numbers or data frames. Arguments can have a default value or not. If not, a value must be provided for the given function to run. Function body is the code between the { brackets }  and this is run every time the function is applied. Preferably, the function body should be short and a function should do just one thing. If a large function cannot be avoided, often they can be constructed using a series of small functions. An example of a simple function could be:

cookies <- function(who, number=10){
  print(paste(who, "ate", number, "cookies", sep = " "))

The cookie function has two arguments, the number argument defaults to 10 and the user does not necessarily need to provide a value. The who argument on the other hand has no default and a name must be provided. I had some cookies BUT I only had nine cookies so I better change the number argument:

cookies(who="Julie", number=9)
[1] "Julie ate 9 cookies"

So, now I have defined a function to keep track of my cookie consumption. What if I want to share this with the rest of Albertsen Lab? I could forward a script for them to save locally.  No no, I will build a personal R package. This might seem like overkill for the cookie function, but imagine a more complex function.  In my search for helpful tools for calculating correlations, I have come by several functions/sets of functions with no documentation. It is nearly impossible to piece together how, what and when to use arguments with no provided help.  So, now I will build a bare minimum package to help me share my function with the group, strongly inspired by Not So Standard Deviations. For more information check out the excellent book  “R-packages” by Hadley Wickham.

First, you will need the following packages:


After this we need to create a package directory:

create("cookies") #create package

 So now,  a package called cookies has been created (you can change the folder with: setwd("my_directory")).

It is a good idea to update the DESCRIPTION file, so that it contains the relevant information about the package (cookies) and the author (me). Next step is to add the cookie function to the package. For this I save a script containing the function in the R folder. If you want to add more functions to your package, you can either create a new file for each function (recommended) or define the functions sequentially in one file.

Now comes the important part – documentation. Good documentation is key if you want other people to benefit from your work. This can be done easily using the roxygen2 package also by Hadley Wickham. roxygen2 uses a custom syntax so that the text starting with #' will be compiled into the correct format for R documentation when processed. Make a new R script with the following code and save it as cookies.R in the folder cookies/R:

#' Cookies
#' This function will allow you to keep track of who your cookies.
#' @param who Who ate the cookies? (Requires input)
#' @param number How many cookies has been eaten? (Default: 10)
#' @keywords cookies
#' @export

cookies <- function(who, number=10){
  print(paste(who, "ate", number, "cookies", sep = " "))

After creating the script then roxygen2 can be used to create all the needed files and documentation:


Lastly the package needs to be installed:


You can now access your awesome functions by loading your brand new package:


 Now you have built a real R package! If you type ?cookies in the console a help page will actually pop up.

Finally, you can upload you package to (Guide). This will allow your others to try out your package, point out issues and suggest changes. Download and install directly from github is easy using install_github() from the devtools package. Try it out by typing this:


It really can be this easy! So next time you copy something more than three times or really want to share your work, consider defining a function and building your own personal package with associated documentation.

Traversing the fog… Soon graduating – then what??

The other day I watched my boyfriend playing the game Dark Souls III. At some point he had to step through this fog, entering a new area or a boss area or something else. You never know.

It was a boss, but I don’t remember how it went from that point on. Because it got me thinking, this summer I will graduate and, for the first time since I was a kid, I will not be a student. Wow, it is scary! My boyfriend is also graduating this summer. So, everyday life for our family will change. We live in a dormitory with our 1.5 year old son and we will have to move. But where? Closer to family? Closer to job opportunities? Somewhere in the middle? And where will that be?

The above are a small selection of the thoughts that crossed my mind. All of them circled one thing – can I find a job? I believe that the answer is yes. But can I do something before I graduate to help the process? Again, I believe that the answer is yes. So, as the to-do-list kinda person I am – I made a plan! Firstly, I must figure out what direction I want to go. The projects I did earlier are very different from my thesis. If I want to continue this path, the words I usually use to describe my professional profile, must change. This is a work in progress.

Second – LinkedIn. My profile is a mess at the moment and not accurately describing my capabilities. The next project is therefore to tailor my profile to the path I want to take. Together with my fellow Albertens Lab students, Kasper and Peter, I attended an afternoon course in LinkedIn, which was very for useful. For example, changing a simple thing as such as your headline can make a big difference.







It still needs some work I think and there a several great guides out there. I will find some inspiration in one available at the university called Pejling (Also notice the great interview about being parent and student at page 20).  As before, we are giving each other feedback. This way I hope to get a clear-cut profile before I start the job hunt.

Next step will be updating my basic résumé. This brings up the eternal question: How to write a good CV? There is a different answer depending on who you ask. Again, I plan to find some inspiration in Pejling. Afterwards I will kindly ask for feedback from my supervisors to help me improve it even further. After building my basic résumé, I plan to tailor my CV for each application, including only what is relevant for the particular job.

Lastly, I plan to apply for jobs before I graduate. It would be awesome to have the security of job before I graduate and the only way to do this is to start before I’m done. Furthermore, I think it demonstrates a capability to juggle several tasks at once. Finally, I plan to get feedback if/when some of my applications are turned down.

In the end, this turned out to be very personal. When this is published, people will know my intentions, also if I don’t succeed in finding a job right away. This makes me feel vulnerable. However, for me, being close to the graduation and the huge decisions that follow, has become a big part of being a master student.

Fast, easy and robust DNA extraction for on-site bacterial identification using MinION

My name is Peter Rendbæk, and I’m currently a master student in the Albertsen lab. The overarching aim of my master project, is as a pre-test for several of the new big projects in the group, which focus on applying the on-line bacterial identification for process control at wastewater treatment plants. Hence, last couple of months I have been working on the project “Developing methods for on-site DNA sequencing using the Oxford Nanopore MinION platform”. The MinION has improved a lot since its release three years ago, and it can now be used to make rapid determination of bacterial compositions.

The potential for this fast and mobile DNA-sequencing is mind-blowing. However, given that the technology is here now (!), there has been relatively little focus on portable, fast, easy and robust DNA extraction. Hence, I’ve spent the last months on trying to develop a fast, cheap, mobile, robust and easy to use DNA extraction method.

There is a significant amount of bias connected with DNA extraction, but the bias associated with wastewater treatment samples has been investigated in depth. However, the “optimized method” is not suited for on-site DNA-extraction. There are 3 principle steps in DNA extraction, cell lysis, debris removal and DNA isolation, which I will cover below and discuss how I simplified each step.

In general, complex samples require bead beating for cell lysis and homogenization. The problem is that our in-house bead-beating is done by a big table top tool weighing 17 kg, which makes it hard to transport. However, I came across a blog post from loman labs about sample preparation and DNA extraction in the field for Nanopore sequencing. In the blog post, the possibilities of a portable bead beater outlined, by the use of a remodeled power-tool. I thought this was interesting, so I went out and bought an Oscillating Multi-Tool cutter and tried this with lots of duct tape…

The amazing part was that it worked! But the problem was that the samples would get “beaten” differently depend on how you taped the sample to the power-tool, which could give rise to variation large variations in the observed microbial community.

I solved this by 3D printing an adapter to the power-tool that fits the bead-beater tube (Finally, a good excuse to use a 3D printer!). I used Solidworks to design the adapter and collaborated with our local department of mechanical and manufacturing engineering (m-tech) in 3D printing it. You can make your own by simply downloading my design from Thingiverse (It did take a few iterations to make it durable enough, and I still use a little duct tape..).


After the bead beating, the cell debris removal is done by centrifugation. Our “standard” protocol recommends centrifugation at 14000 x G for 10 minutes at 4 C. However, in our minds that seemed a little extensive and requires a huge non-transportable centrifuge… Alternatively, there are a lot of possibilities to use small, easy to transport and easy to use centrifuges if we do not have to centrifuge at 14.000 xG at 4 C. There is even the possibility to 3D print a hand-powered centrifuge. However, I did not follow this path, as it seems a bit dangerous… After several tests, we discovered that a simple table top centrifuge could do the job perfectly well, using 2000 xG for 1 min at room temperature if we combined it with the DNA isolation described below.

The last step is DNA isolation, I tried several different methods, but we got the idea to simply use Agencourt AMPure XP that is routinely used in e.g. PCR purification (we 10 diluted the AMPure XP beads 1:10 to save some money and it seems to work just as good). And… It works..

So, now you have an overview of the method I developed. The most amazing part is that it works! It takes 10-15 minutes from the sample is taken until you’ve got ready DNA for use, compared to 60+ minutes for our “standard” protocol. Furthermore, it requires inexpensive equipment that can be carried in a small suitcase. So, just to prove that this approach is fast, I filmed myself doing the DNA extraction with a GO-PRO camera, as you can see below.

The next part is to test the MinION in the lab. How, fast can we identify bacteria and is the extracted DNA compatible with the downstream library preparation, which we hope to do on the our new and shiny Voltrax (which is now moving liquids!).

Learning how to make a good presentation

As a student, you will have to present sometime during your education. Despite this, there is hardly any time allocated to learning the skills required in giving a good presentation .

As part of your Masters degree at Aalborg University you’ll have to participate in at least one status seminar presenting your thesis (20 minutes). Afterwards there is a 5 minute time slot for questions from the audience. The audience will be your fellow students, your supervisor(s) and other students or employees who may be interested in your project content.

My fellow (Albertsen Lab) master students and I, spend approximately two weeks preparing for this. During this period, it became clear that the amount of guidance we got was pretty unusual. Hence, I thought I would share how we prepared and the differences it made in general and specifically to our slides.

  • 19th: Meeting, brainstorming about content of presentation
  • 20th: Sending the first draft of the presentation and receiving feedback.
  • 26th: Rehearsal of presentations. Each student within our group presented and we were constructively critiqued by others in the group regarding slide content and presentation skills.
  • 27th: Improved slideshow was sent once again and feedback was given for the final time.
  • 31st: Status seminar

Although it seems to be rather extensive, I feel all of our presentations benefited from the extra effort.

Example from Peters presentation 

Before: Peter wanted to illustrate how he had optimized the method.

After: A line-up of conditions before and after optimization.

Example from Kaspers presentation

Before: Kasper wanted to illustrate how your ordination plot can change depending on your choice of distance metric.  

After: Kasper added a progress bar (with neutral colors), found an example to better illustrate his point, added the citation and underlined his point with big red statement.

Example from my presentation (1)

Before: I wanted to show the current status of my network function.

After: I changed some visual properties in my tools for better visualization. I also changed the specific OTU name to example names, as my audience could not relate to the MiDAS data base.

Example from my presentation (2)

Before: I wanted to make a quick introduction to correlation


After: Removing text for simplification and adding citation.


What you cannot see from the examples, is the improvement in the delivery of our presentations. As a student it can be nerve-racking to present science in front of an audience. If you haven’t had feedback, that is just one more thing to be nervous about. Getting feedback both on my slides and my way of presenting them gave me the safety of proper preparation.

After this experience, I can’t help but feeling thankful that learning to present is of high priority in our group. It is key to be able to communicate your message clearly, especially in a scientific community. It is not a part of our curriculum and maybe it is too much to expect, that students can learn to master this without any guidance.

The final presentation slides for Kasper, Peter and I can be found on SlideShare.

Small fry – master student at MEWE16

As a newly started master student it can be a bit intimidating to attend a conference. Nevertheless, I said yes when I was offered to attend MEWE16 in Copenhagen. I had just ended a one-year maternity leave and was eager to start my thesis and talk to adult people.

I quickly realized that I was surrounded by experts. I recognized names from articles, got a bit starstruck and started thinking: How in the … am I supposed to learn anything from this? They are so much smarter than me.

However, as the days past I realized there is a lot to take home from such an event – even for a small fry master student. So, I compiled a list:

  • Watch and learn. If you can follow the science – great! If you can’t, look for other things. In what way, did he/she present data? What worked? What did not? Did someone use other means to get their message through? One thing that I found could really keep my attention was humor. Not like comedy – a little goes a long way – but when my mind started to wander, humor redirected my attention back to the talk.
  • Poster sessions. I was surprised that the poster session was so crowded and it quickly became awkward because I was unprepared. So, take some time to browse the posters and note if anyone is of particular interest. Should there be no one of interest, use it as practice in “talking science” to people you don’t know. A suggestion could be: 1) go to a poster 2) ask: “Can you tell me about your poster?” At least for me, it is very good practice.
  • Networking can be many things. Don’t feel like you have to network with all the big guys, just because you’re in the same room. I am lucky enough to belong to a group that treasures social gatherings and I spent most of my time building my “local network”.
  • Be nice to yourself. I think you should be fair to yourself and realize that very few talks are directed toward you. It is a crowd of experts and if you want to have a relevant debate on your topic, maybe you have to narrow your talk and that’s not always for small fry. This is an opportunity to get some fresh air, see the city or browse the posters as mentioned earlier.