01: Introduction to R

Justin Dainer-Best
08-31-2020

The first steps involve setting up R and RStudio, getting acquainted with them, and making sure everything is working.

Install R

If you are using your own computer

  1. Install R from https://cran.r-project.org/—it is available for Mac OS, Windows, and Linux. Choose the appropriate link, download, and install.

  2. Install RStudio from https://www.rstudio.com/products/rstudio/download/—choose the free “RStudio Desktop” (all the way to the left). You can run R without RStudio, but I strongly recommend using RStudio (since that is what you’ll be seeing for all examples).

If you are using RStudio Cloud

  1. Make an account on RStudio.Cloud, and then skip to the next section.

Install this package

Once you’ve installed R, but before doing anything else, you can install this package. Doing so will also install a number of other imoportant packages.

Occasionally R may ask you questions like “These packages have more recent versions available. It is recommended to update all of them. Which would you like to update?” And you will need to select a response (e.g., type 1 and hit enter to download all updates).

install.packages("devtools")
devtools::install_github("jdbest/psychRstats")

Installing the package will also install all of the tutorials. However, I’ve also written exercises, which you’ll want to download somewhere on your machine. For that purpose, you should follow the instructions below on setting a working directory and then downloading exercises.

Set a working directory

You’ll need to make a decision about what will be your “working directory”. The working directory is the folder on your computer where you’ll be storing the exercise files, and where R assumes you’re working. (For example, you might use the “Downloads” folder on most computers is located in your user folder/Downloads, e.g., mine might be in /Users/jdbest/Downloads on a Mac or in C:/Users/jdbest/Downloads on a PC.)

There’s more info on this on the github wiki. You can always return there with questions.

(Read more about working directories in R and RStudio.)

To check that you’ve got the right working directory set up, run the following code in the RStudio console (just copy and paste it and then hit enter):

getwd()

Does it say what you expected it to? If not, either ask for help or read through the above again.

RStudio Cloud

Your files will download to “/home/rstudio-user” – if when you run getwd() you don’t get that, run the following code: setwd("/home/rstudio-user").

If you want to download files from the Cloud to your own computer, follow these instructions.

Download exercises

When you have decided on your working directory—even if you’re sticking with "~" (your user folder), you can download the lab exercises. They will all download from a zip file, and all be R Markdown files designed to run in RStudio. You should have downloaded the package before this will work:

psychRstats::download_exercises()

The lab documents will download to your computer in the directory (folder) you defined above—into your working directory. (If you didn’t define it, it’ll download into whatever working directory was the default, usually your user folder.) It may pop up a folder where the file lives.

Errors and Problems

If you run into any errors, consider looking to the wiki page on troubleshooting for solutions. (You can also scroll up and click “Wiki”.)

The first tutorial

All right! Now that you’ve gotten everything installed, run the first tutorial:

psychRstats::lab("01-intro-to-r")

Citation

For attribution, please cite this work as

Dainer-Best (2020, Aug. 31). psychRstats: Learning Statistics for Psychology in R: 01: Introduction to R. Retrieved from https://jdbest.github.io/psychRstats/labs/01-intro-to-r/

BibTeX citation

@misc{dainer-best202001:,
  author = {Dainer-Best, Justin},
  title = {psychRstats: Learning Statistics for Psychology in R: 01: Introduction to R},
  url = {https://jdbest.github.io/psychRstats/labs/01-intro-to-r/},
  year = {2020}
}