What to do if you're stuck in R

Look, you're going to get unexpected errors when running R code. We've talked about the benefits of using R -- it's free, open source, very flexible -- but it also tends to have more, and harder to understand, error messages than a paid-for statistics package like Stat or SPSS. I'd argue (and maybe you've heard me argue already) that in general it's actually better to have a few extra errors. In short, whether we're doing stats or data science it's usually better to have code  not work at all than to have it  not work as expected. R never wants your code to seem like it's running fine when in fact it's doing something wildly different than you intended. It also in general doesn't want it to seem like statistical models have finished fitting effectively when there were problems with convergence or error estimation. In those cases when Stata might just give you its best guess, R often gives a warning message and sometimes an error that breaks your code (don't worry, we'll fix it). Oh, also--and this is at least true for me--I often get errors because I've just made a typo or misunderstood how something works or forgotten what type a variable is or loaded a package that masked a function I'm using (I'm looking at you MASS::select).

Regardless of why you get an error, it doesn't mean that you have to give up! Instead, errors are R's way of saying "Hey, I want to do what you want me to do, but I don't have enough information about what you want yet, can you help me?"* As you use R more and more, you'll work out a way to deal with errors that works for you. As your doing that, here are some suggestions.

0. Be kind to yourself and take breaks

Errors can be really frustrating (also sometimes they can get fixed quickly!). Don't bang your head against your computer, it's bad for the computer (and not great for your head). Remember that getting errors is part of writing computer code and does not reflect your true value as a human being. You are awesome! When things aren't feeling awesome, that's a sign to take a break from the keyboard for a bit and touch grass or go to the movies or jump in the lake or something.

1. Read the error

The first thing to do is to read the error carefully. When you're starting out it might not make much sense, which is the fault of the developers, not you. But you still might find something helpful in the message. As you gain experience, the error messages will be more and more familiar. You'll realize, for instance, that a syntax erorrs often mean youv'e forgotton or mistyped a comma, a bracket, a parenthesis, a semicolor, accidently typed %%> instead of %>% or someother syntactical detail. 

2. Locate the source

If you read the error and still aren't sure what to do, see if you can isolate the specific point where the problem occured. If you got the erorr after running more than one line of code, try running one line at a time. If you only ran one line, can you run just part of the line? Zeroing in on the location of the problem might help you see what's wrong.

Once you know where the issue is, re-read your code there carefully to see if something seems off.

3. Check similar code

If you've copied and modified code from one of our labs or another source, go back to that code and see if that code runs on its own. If it runs, try and understand what you changed that might have caused the issue.

If the original code gives you the same error that you got with your code check for these common issues:

- do you have the r object (maybe a data file) that the code is using loaded into your environment?

- have you imported all of the packages you need for the code you're trying to run?

If the original code gives you a different error, then try re-running the original script from the top. If it's still a different error and you can't figure that one out, contact the teach ing team for help.

4. Walk through what you expect your code to do

This one seems straightforward, but can be really helpful. Re-read the code where the error is happening and think through what each function is doing, making sure that each function has all of the arguments it needs to work. If the error message mentioned one of your function names, pay special attention to that function.

If you’re unsure what a function, like table, does or how it works, you can use the help panel – or type ?table or ?<someotherfunction> into the console to bring up R’s built-in documentation. It’s not always the best and sometimes contains technical language, but worth checking out. The most helpful part of the documentation is usually at the top, where it has details about the function's arguments and what kinds of variables those arguments expect. Make sure you're giving a function what it wants!

5. Google the text of the error message, follow the StackExchange links

Somebody wrote the error message you're seeing, so somewhere there's an explanation of what it means. Most commonly there were also many people who've been in exactly (or nearly exactly) the problem spot that you're in now. A good internet search can often pull up a disscussion about what the error means and some things you can do about it. Those discussions often happen on the StackExchange platform, which is a message board explicitly for sharing coding help. If you find a post that seems to be about a similar problem, read the proposed solutions carefully. Then:

1. Try and get the code from their proposed solution to run.

2. If it does, make sure the code is doing what they say it will and that that's what you want

3. Try to modify their code to suit your purposes or to modify your code to incorporate their solution

6. BUT ALSO ANYTIME! Ask the teaching team or a friend for help.

If you've tried all of that stuff--or if you tried some or none of it--and you're flummoxed or stressed or anything else, then reach out to the teaching team. Often we'll have faced similar issues in the past and we can help you get going again quickly. Also often it will be a new problem for us and we'll work with you to find a way through or around it.

Finally - Remember, learning a new coding language is a little bit like learning a new langauge with a conversation partner who can do amazing things for you if you ask but who's a stickler for grammar. That can be frustrating, but keep at it and eventually you'll be communitication soon! And then you'll be able to do super cool research!

*In most cases. If you get memory fault - core dump then you're in different territory, and maybe you wrote code that messed up your computer a little. But only a little! It will be fine after you restart. Just mabye patch that leak.





Capital 'P' Project

One of the most common questions I (and probably anyone who’s finished a graduate degree) get is some variation on “what’s the one thing you’d tell someone who’s just starting out in grad school?” I’ve heard that question answered lots of times too and there are many great answers, like “relationships make or break a grad school experience” or “the staff members in your department are possibly the most important people there” or “make sure to talk to grad students before you join a department” or “you are a valuable person who deserves love and respect simply because you exist (even though at some point, or many points or all points the system of graduate school will tell you you don’t belong).” Those are all true and important and please remember them. They were also all super helpful for me too, but they aren’t quite the one single thing I wish I’d started with, and they aren’t what I think makes the biggest difference. Instead I think the most important thing is to have a “capital ‘P’ Project.”


I first heard about this idea in a grad class taught by the incomparable Kemi Adeyemi at the University of Washington. Kemi often began class with a free write, usually on the readings for that day. But one class she instead asked us to consider what brought us to grad school, to our particular program, and to the research that excited us. She suggested that even if we hadn’t thought about it in those terms before, there was likely some larger goal or intention, a Project (pronounced ‘capital P project’) towards which our most fulfilling efforts pushed. Like everyone else there I’d written a ‘statement of purpose’ (SOP) to get into my program, but that’s a document with very narrow specifications, used once for admission. Most importantly, an SOP was supposed to focus on research that you conceivably could actually do given the environment of the program you applied to. In contrast a Project doesn’t have to be about research, doesn’t have to be something feasible for you to do on your own (or at all!). Instead it’s a large-scale, long-term goal towards which you want to move not just your discipline, but ideally the world. A capital P project could be something like:


Finding ways to increase food security in areas affected by climate change


Understanding the subtle ways that systems of oppression reinforce themselves


Developing more efficient solar cells that use fewer rare resources


Discovering how meditation can contribute to effective therapy


Reducing the impact of waste disposal on the environment


Each of those projects is impossibly huge for a single graduate or academic career, or likely even for a single lifetime. At the same time, each of those projects has lots of places where individual researchers can contribute. If you’re already in graduate school, chances are you already have a project like this in the back of your head (though no worries if not) but you might not feel comfortable saying it out loud. We’re taught to set feasible goals which will contribute to our publication record and therefore our job-market chances, and Projects are not that. Advisors, mentors, or colleagues might not be as excited as you are about your project (though you might be surprised), and if you’re in a discipline that values ‘pure science’ there may be significant disciplinary and institutional barriers to having this kind of project. 


On the other hand, there are very strong benefits to developing and getting in touch with your capital P project:



A Project helps you say no

No matter where you are in your graduate school career (or beyond/outside such a career) time is one of your most precious resources. How you decide to spend it is critical to your success and to your wellbeing. Early on you have to decide what classes to take, later on you need to decide what projects to invest in. Unless you have the lucky situation of a fellowship, you also need a way to pay the bills. In my experience, graduate students usually burn their candles at as many ends as they can manage, and have trouble turning down opportunities. In my first year, for instance, I took 5 classes one term (terrible idea) and worked on a project where I wasn’t an author for free (also do not recommend). 


Having a well-defined capital P project helps you know when to say no: opportunities that contribute to the Project get your time and opportunities that don’t go in the bin. Obviously there’s space for interpretation and feasibility–a funding opportunity that doesn’t directly contribute to the Project could still be OK if it frees up time you had spent on a TA/RA position. Sometimes a new opportunity is so cool and impactful that it actually changes your Project. That’s fine, the project isn’t static and it isn’t legally binding. But the project is a really good reason to turn down a chance to work with someone famous who doesn’t share your values, or to skip a class that the rest of your cohort are all taking so that you can take a class outside of your department from that one professor who seems to share your Project if not your discipline.  


A Project provides intrinsic motivation

Writing an MA thesis or a Dissertation isn’t easy. It takes a long time and towards the end the novelty of the particular research question itself has usually worn off. By the time you’re writing you’ll probably already know the answer to the questions you were interested in. For me (and from what I’ve heard many of my colleagues) the struggle of finishing a thesis or dissertation isn’t primarily academic, but motivational. By the time we’re writing one of those papers we probably have the skills and data and (hopefully) resources we need to finish, but it might feel like we’d rather do anything else than write the next sentence.


At the beginning of the last year of my dissertation, my data was collected, my analysis was done–I felt like I was in good shape, but I had terrible trouble sitting down to write. I spent months where writing even for 5 or 10 minutes was something I counted as a success because it was so hard to convince my body to sit down and do it. However, I was able to keep coming back to my desk because I believed in my Project—I wanted to advance that project more than I wanted just a degree. Most of the first draft of my first chapter was written in those little short sessions. I’m not sure if I would have finished if I had only been in it for the credential. I hope that you don’t face the kind of writer's block that I did, but also that by thinking more explicitly about your project you do find the kind of steady motivation that I had.   


A Project is a framework for collaboration

The simplest version of a Project is just a clearly imagined end goal. By working backwards from there you can see more clearly what skills you need to develop and what knowledge you need to gain on your way to that goal. That can be invaluable when picking classes or deciding what to read next from your long list of articles. 


But the larger version of the project, as the impact you want your research (and by extension yourself) to have on the world, can also help you develop fruitful collaboration within and beyond academia. You want collaborators who ideally share your project, or at the very least are willing (and able!) to contribute to it. It’s very likely that looking for collaborators this way will bring you beyond your discipline, and perhaps beyond academia.


A Project can help on the job market

Now let me say explicitly what I’ve only hinted at before: I’m suggesting you let your Project guide these choices. Choose collaborators who can best help you grow your Project, not those who have the most status in the field or who have published in the highest-ranked journals. Now the primary benefit of this method is that it means your collaborations will fuel your project, and you’ll build a community of people who share, or at least support, your Project. But there’s an important secondary benefit when it comes to the job market.


One of the key things people say about your job market materials, typically at least a research statement and a teaching statement for academic jobs, is that they should have a unified narrative, you want to frame your C.V. as a series of smaller efforts that all contribute to a well-defined and important research agenda. Technically, a Project is not a full-blown research agenda–it’s just a long term goal, maybe even a pie in the sky. But if you’ve worked steadily towards that goal, it can be relatively easy to reframe the work you’ve done so far as part of a unified path.