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:

install.packages("devtools")
library("devtools")
install.packages("roxygen2")
library("roxygen2")

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:

roxygenise("cookies/")

Lastly the package needs to be installed:

install_local("cookies")

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

library("cookies")

 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 github.com (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:

devtools::install_github("julieklessner/cookies")

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.

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.