Lab 01: Introduction to R

PSY 203: Lab 1

Justin Dainer-Best
08-31-2020

Today’s lab involves setting up R and RStudio, getting acquainted with them, and making sure everything is working. You won’t turn in anything today other than a brief response to how things went.

DO: Ask questions, work in your groups, and let me know if anything is not working. Now is the time to make sure you get it.

DON’T: Worry about something not working. We’ll sort out any errors.

These instructions are also on the course github page.

These are all of the instructions for Lab 1. Find other labs.

For each lab, you should expect to download the lab’s files locally to your computer, and run them there. If you’re using https://rstudio.cloud/ instead of running things locally, you’ll do the same thing there, with a few minor changes.

If you’d like, you can watch a video about setting this up—or just follow the instructions below. (Videos link to Brightspace.)

Set a working directory

If you are using your own computer, follow the syllabus details on installing R and RStudio, and then open RStudio. If using RStudio Cloud, make an account on RStudio.Cloud, and then skip to the next section. If using a lab computer in Albee 100, the computer should have R and RStudio installed. (Let me know if it does not.)

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 your 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.)

Use the Session > Set Working Directory menu. Navigate to whatever folder you’ll be using for this class. (You might want to use something like /yourusername/Bard/PSY 203/labs – but anything is fine so long as you know where it is.) This is the sort of folder that when you go to Finder (Mac) or Windows Explorer (PC), you can find all of the files associated with this class in one place. (If you’re on a shared computer, you may have limited choices. Documents folders are fine!)

Optional: You can permanently set your working directory using the Tools Menu. Go to Tools: Global Options, and then click the “Browse…” under Default working directory. Navigate to the same folder you just set.

If you’re familiar with some coding, you can also set your working directory using the setwd() command—just be sure to enclose the path in quotation marks, e.g., setwd("/yourusername/Bard/PSY 203/labs").

(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. I briefly point this out in the video linked above.

Download packages

Once you’re ready with your working directory correctly set, run the following commands in the Console in RStudio to download the correct packages, and then the correct document. (Copy and paste the code one line at a time into the R Studio console, hitting enter after each line.) Note that occasionally R will ask things 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("usethis")
install.packages("remotes")
remotes::install_github("rstudio/learnr")
remotes::install_github("rstudio-education/gradethis")
install.packages("flair")
install.packages("rmarkdown")
install.packages("tidyverse")

Once all packages are installed, run the following:


usethis::use_zip("https://github.com/jdbest/psy-203/raw/master/lab01.zip", cleanup = TRUE)

The lab document 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.) It may pop up a folder where the file lives—just come back to RStudio. Once it has finished downloading, copy and paste the following line to run in your RStudio console, which will open the tutorial.


rmarkdown::run("lab01/intro-to-r.Rmd")

Errors and Problems

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

Citation

For attribution, please cite this work as

Dainer-Best (2020, Aug. 31). PSY 203: Statistics for Psychology: Lab 01: Introduction to R. Retrieved from https://faculty.bard.edu/jdainerbest/psy-203/posts/2020-08-31-intro-to-r/

BibTeX citation

@misc{dainer-best2020lab,
  author = {Dainer-Best, Justin},
  title = {PSY 203: Statistics for Psychology: Lab 01: Introduction to R},
  url = {https://faculty.bard.edu/jdainerbest/psy-203/posts/2020-08-31-intro-to-r/},
  year = {2020}
}