2 General troubleshooting

2.0.1 Common coding errors

  • Error: "object not found". This usually means you don’t have any objects in your workspace with the name of the object that is “not found”. Try rerunning the code that should be creating the object (remember, objects are often created with <-). Alternatively, run the object name alone to inspect it. Is the output what you expect? This error might also mean that you think you are calling a column name when in fact because of the way that the code is written, R is looking for an object. Double check the function syntax and data structure. One way to quickly check the column names in a dataframe is to use names() or str().

  • Error: "could not find function". This usually means the package containing a specific function is not loaded. Try looking up which package the function is a part of and double check that you’ve loaded the package.

  • Double check the variable class and object type. Sometimes objects get converted into tibbles when they need to be dataframes or are matrices but need to be igraph objects. Variables might need to be numeric or binary or something else. You can find the class of a variable with class() and the object type with str(). Common conversion functions include: as.data.frame() and as.numeric()

  • Warning message: In *function(data)* : argument is not numeric or logical: returning NA. The data class of the vector being read into a function doesn’t match the data class that the function is looking for. For example, you might be loading text data into a function that can only summarize numeric data.

  • Error: unexpected symbol in: This means that the function syntax has an issue. Often this is an extra comma or a missing quotation mark. Double check the line of code to make sure the punctuation is correct.

  • Error in UseMethod(“anti-join”) : no applicable method for ‘anti_join’ applied to an object of class “character”. In this case, you might be loading a vector into a function that is looking for a dataframe. Be sure to doublecheck that the objects being loaded into the function match what the function is looking for. Think about what you expect the output to be, as this can help debug the input.

  • Error in if (!(class(mydata) == "data.frame")) { : the condition has length > 1. This error is likely to do with the input objects not matching what the function is looking for. Make sure that the data classes of the columns are as expected. Also make double check whether the function is looking for a dataframe or a vector as input.

  • Error in file(file, "*filename*") : cannot open the connection In addition: Warning message: In file(file, "*filename*") : cannot open file '*filename*': No such file or directory. This means that the file you are trying to read in isn’t located at the specified location. This could be due to a typo in the filepath, a change in your working directory, or a typo in the file name itself.

2.0.2 General troubleshooting articles

We all learn differently. In case my explanations aren’t clicking, try reading some of these other articles about troubleshooting in R. These ones helped me figure out how to write this guide.

2.0.3 Why isn’t my R Code running?

  • Read the error code. This is the first step to figuring out why your output was unexpected.

  • Google the error code. Find the part of the error code that does not include your specific variable or column names. Google the error and look for solutions on websites like stackoverflow.

  • Double check spelling, capitalization, commas, parentheses, quotation marks and general syntax. If any one of these is off it can cause the R code to not run properly.

  • Debug the code line by line. Go back to the last line that did work in your code. Then check the error code of the first line that doesn’t work properly. This can help you figure out the first issue and potentially avoid the later issues in the code.

2.0.4 Where is my dataset? I loaded it but it’s not showing up.

  • Familiarize yourself with how file paths work on your computer. This will differ across operating systems. On a mac, a handy shortcut is to right click the file, hold down option, and select “copy as Pathname”. You might also use a web url to load in data.

  • Make sure you are reading in the file with the right function for the file type. A .csv, .xlsx, and .png file will be read differently.

  • Here’s a helpful article about reading excel files into R