I want to start by talking about my struggles when beginning to work in R. My first exposure to R (and programming in general) was in a Phylogenetic Methods course. I should note that I consider myself barely computer literate. Never in a million years did I think that as a biologist, I would have to work through a command line/code/script/programing language/whatever the correct lingo is. When we first fired up R in class, I looked at that blank console with the blinking cursor and I thought “Now what??” There are an infinite number of things I could type into the blank space. Unfortunately, most of those entries will yield error messages. So how do I use R to produce something meaningful? I found that there were 2 key things that began to open the door for me. These may seem silly, but they are really not trivial.
- Know what a working directory is and how to use it.
- Believe it or not, I didn’t fully understand a working directory until about halfway through the semester. A working directory is essentially a file folder that you tell R you are going to work out of. When you try to load data, R will be looking for the file in your working directory. In addition, when you save code, plots, etc., they will be saved to your working directory. It can be helpful to create a file folder specifically for your work in R so that you do not constantly need to change your working directory or specify paths to different files.
- But how do you know what folder is your working directory? It’s simple, just use the function getwd() and R will return the path to your current working directory. This function is useful because if you don’t specify a working directory, R will set one for you as a default.
- How do you set your working directory? The function setwd() allows you to specify your working directory. In the parentheses, enter the path to your desired file folder.
- Learn how to read help files.
- This seems simple enough, but the help files also appear to be written in R. It takes practice, but once you start to become familiar with R syntax, the help files will begin to make more sense. It took me an entire semester of working in R before I could read and understand help files. Now, I use them all the time!
A few other useful R basics to learn:
- How to install and use packages (yay, we went over it in class!)
- GOOGLE: If you are trying to do something in R and the help files are not doing it for you, google! There are a ton of resources out there and lots of people trying to troubleshoot their code. Even copying and pasting an error message into google can be very helpful. Most likely, you are not the first person to get that error and you may find a page where the error message is explained.
Now I would like to open it up to discussion. What specific challenges have you faced in learning a programming language? Do you have any tricks that make it easier? What do you think are the most important things for a beginner in R to know?