Common mistakes and troubleshooting in RStudio

1. Not installing and loading packages

We need specific packages to conduct our analyses. Running the "install and run packages" is always the first step.

We can check the installed packages under “Packages.” Checkmark means that the specific packages are loaded for the session. If there is no checkmark, we have the package but it was not loaded.

For troubleshooting, make sure to run "install and run packages" codes, wait until the “STOP” sign in the console disappears, no more code is running in the console, and see "Package already installed: package name" (under the console in red font, see the first figure).

2. Not loading the data

Every time we open RStudio, we must load the data. Even if we see "gss" under “Environment,” we should run "load data" code again. If we do not see "gss" under "Environment," our codes will not work.

If the data is not loaded, we will get the following error (under the console):

For troubleshooting, make sure to run "install and run packages" codes and "load data" codes in order.

3. Not working in the "Working space"

We do not edit or change anything on R script files except under “working space”(see highlighted part #1). Anything above the “working space” is teaching material!

The codes for assignments will be put under the “working space”

For easy navigation click “Outline” (see highlighted part #2) to see the headings and subheadings.

If we accidentally delete something from the R script file (above the working space), we can undo the changes using ctrl + Z for Windows or command + Z for macOS. If we lose track of the changes or cannot undo them, we simply re-upload the original R script file.

If this happens, we should first rename the current R script file because re-uploading the file will overwrite it and we lose our previous progress. Based on the sample below, we can rename the file to 'script_scripting_previous.R' (make sure not to delete the ".R" file extension at the end). Click "check box" (see highlighted part #1) and click "Rename" (see highlighted part #2)

4. Not using a model code

Whenever we run an analysis with a different variable, we create a model code and a working code.

If we don’t use model code for comparison, it is likely that we accidentally delete something extra. In this example, the comma was deleted.

Instead, we keep the use model code and compare it with our working code. Here we clearly see that the comma is missing in line 79. Note that RStudio warns us that something is wrong with that red cross.

After identifying what is missing, we can fix the working code. Once the working code functions correctly, we can delete the model code.

5. Typing variable names

We NEVER type variable names! It is very common to miswrite codes, forget commas, etc. We always copy the variable names (from the code templates page or assignments), and paste into our codes.

There is no variable called “maritaal”, but “marital.” RStudio warns us that “maritaal” is “unknown.” We copy and paste variable names to avoid this possibility.

6. Not highlighting all the lines when running codes

We need to highlight all the lines and click “Run.”

For single lines, we triple click (clicking three times really fast).

For multiple lines, we highlight the codes with mouse.

Here's what happens if we don't:

It simply shows what we highlighted and run (check the console part), not the analysis (check the plots part).

Instead, we should have highlighted all the line:

7. Not putting hashtags (#) for our notes

Typing notes on our R script files is encouraged. When we type a note, we must put a hashtag (#) first (we can put # anywhere we want). Not putting a hashtag (#) will confuse RStudio.

While line 29 will work, line 27 won’t. Here RStudio warns us that there is something wrong. Look at the red cross on line 27. When there is a red cross on the left side of the line number, there is something wrong with our codes.

8. Changing the original values of the dataset

We never save our data, but our R script files. From time to time, we may accidentally change the values of original variables (especially when we recode variables).

When this happens, we go to the very top of the R script file and load the data again. If we created new variables previously, we will need to run those codes under our working space again in order since it will be a fresh data.

Last updated