how to get help

This week I have been thinking a lot about how to ask for help with R code in a way that makes it easy for people to help you.

It is tempting to take a screenshot or make a video of your code not working and share it with people who know more than you do. But without being able to run your code, or to reproduce the environment you are trying to run your code in (what packages you have loaded, what data you are using), it is really hard for people to help.

I saw a really great talk by Sharla Gelfand at the Toronto Reproducibility Conference this week about reproducible examples and it inspired me to try the reprex package.

my goal

This week I wanted to make R give me an error and then learn how to create a reproducible example that I might be able to share with people who could help me solve my error.

how to make a reprex

first isolate the problem

When you want help with a problem, you want to give your helper the information they need to help you and nothing more. Your helper is going to need to know…

  • the packages that are needed to run your code
  • the code itself
  • the data that your code runs on

the packages: just the relevant ones

When we are doing complex analysis, the load packages chunk at the top of our document can include LOTS of packages. But the error we are having a problem with is most like to relate to just 1 (or maybe a few) of those packages. Work out which function is causing your code to break (see below) and then only include the packages that are directly relevant to your problem in your reprex.

the code: where is the break?

We often write code in long pipes and sometimes it is tough to work out which part of the pipe is causing the break. When you are debugging code, Sharla recommends adding one line at a time until you find which line WON’T run, or subtracting lines until you find the point at which the code WILL run.

the data: create a minimal dataset

The data we are working on is often big and complicated and we probably don’t want to send the whole .csv file out into the world in order to get some help. But the problem usually relates to a small aspect of the data, which is where minimal datasets are really useful. Try and create a small dataset that can be used to illustrate the problem in the simplest possible terms.

The tibble function is really helpful for creating a little dataframe that can be used to illustrate your code problem. My problem is going to relate to the pivot_longer() function and I saw some funny tweets this morning about pivoting sandwiches, so am going to make a little dataframe about sandwich ingredients.

library(tidyverse)

wide_lunch <- tibble(
  recipe = c("sandwich", "burger", "burrito"), 
  bread = c("sourdough", "brioche", "tortilla"), 
 meat = c("ham", "patty", "chicken"), 
  cheese = c("swiss", "blue", "cheddar"), 
 vege = c("tomato", "pickles", "guacamole"), 
  sauce = c("mayo", "mustard", "hot"))

wide_lunch
## # A tibble: 3 x 6
##   recipe   bread     meat    cheese  vege      sauce  
##   <chr>    <chr>     <chr>   <chr>   <chr>     <chr>  
## 1 sandwich sourdough ham     swiss   tomato    mayo   
## 2 burger   brioche   patty   blue    pickles   mustard
## 3 burrito  tortilla  chicken cheddar guacamole hot

Now I am going to try and use the pivot_longer() function to make the wide_lunch data into long format… but I’ve got an error.

long_lunch <- wide_lunch %>% pivot_longer(names_to = type, 
                            values_to = ingredient, 
                          bread:sauce)

then bundle your problem into a reprex

I have an error and I don’t know what is wrong, so I want to create a reprex to share with someone who might help me.

I need to make sure that my reprex includes…

  • the packages that are needed to run my code
  • the code itself
  • the data that your code runs on

… so I have put all of that into 1 chunk.

# packages

library(tidyverse)

# data

wide_lunch <- tibble(
  recipe = c("sandwich", "burger", "burrito"), 
  bread = c("sourdough", "brioche", "tortilla"), 
 meat = c("ham", "patty", "chicken"), 
  cheese = c("swiss", "blue", "cheddar"), 
 vege = c("tomato", "pickles", "guacamole"), 
  sauce = c("mayo", "mustard", "hot"))

# problem code

long_lunch <- wide_lunch %>% pivot_longer(names_to = type, 
                            values_to = ingredient, 
                          bread:sauce)

Then I can load library(reprex) in the console, use Command-C to copy the code to the clipboard and use the Addins menu “Render reprex”.

A helpful popup appears and I can tell R that my code is on the clipboard and that I want to share it on Slack.

When I render the reprex, this appears in the Viewer panel, and I can copy and paste the code into Slack (or github issues or some other community help forum).

Whoever is helping me, can copy from Slack into the console on THEIR MACHINE and the code WILL RUN for them, giving them the SAME error.

so why is my pivot_longer not working??

It is because I forgot to put the names_to = and values_to = variables in quotes. The error tells me that the object type (aka the variable I am trying to create with names_to) is not found. I need to tell it that I want it to create NEW variables via names_to and values_to variables by putting them in quotes.

long_lunch <- wide_lunch %>% pivot_longer(names_to = "type", 
                            values_to = "ingredient", 
                          bread:sauce)

So if you need help, make a reprex!