Mac R: https://cran.r-project.org/bin/macosx/
Windows R: https://cran.r-project.org/bin/windows/
R Studio (both Mac and Windows here): https://www.rstudio.com/products/rstudio/download/
In this section, we will cover some basic set up in R. We hope by the end of this, you will have R set up properly on your computer, as well as have a super preliminary understanding of how to write code in R.
Materials adapted from this resource: https://psych252.github.io/psych252book/visualization-1.html
Let’s first load the packages that we need for this lesson. You can click on the green arrow to execute the code chunk below.
You may need to install the package first. If so, you can type install.packages(“<the package’s name>”) into your console.
Example: install.packages(“tidyverse”)
library("knitr") # for rendering the RMarkdown file
library("tidyverse") # for plotting (and many more cool things we'll discover later). Not really necessary for this lesson, but will be for the next :)
You should always load packages at the top of each R Markdown you write. Base R is automatically loaded, but everything else needs to be imported. Check out this list of commonly used packages: https://support.rstudio.com/hc/en-us/articles/201057987-Quick-list-of-useful-R-packages
Go to general preferences and make sure:
Restore .RData into workspace at startup is unselected: This can otherwise cause problems with reproducibility and weird behavior between R sessions because certain things may still be saved in your workspace.
Save workspace to .RData on exit is set to Never: This can otherwise cause problems with reproducibility and weird behavior between R sessions because certain things may still be saved in your workspace.
Soft-wrap R source files is selected: This way you don’t have to scroll horizontally.
I also like to color my brackets :) Helps to see if you are missing an end bracket
RStudio makes it easy to write nice code. It figures out where to put the next line of code when you press ENTER. Legibility comes from the indentation - this is very important. If things ever get messy, just select the code of interest and hit cmd + i to re-indent the code.
If you want more information on writing nice code, check out this resource: - Advanced R style guide
There are three simple ways to get help in R. You can either put a ?
in front of the function you’d like to learn more about, or use the help()
function.
?printhelp("print")
R help files can sometimes look a little confusing. Most R help files have the following sections - copied from here):
Title: A one-sentence overview of the function.
Description: An introduction to the high-level objectives of the function, typically about one paragraph long.
Usage: A description of the syntax of the function (in other words, how the function is called). This is where you find all the arguments that you can supply to the function, as well as any default values of these arguments.
Arguments: A description of each argument. Usually this includes a specification of the class (for example, character, numeric, list, and so on). This section is an important one to understand, because arguments are frequently a cause of errors in R.
Details: Extended details about how the function works, provides longer descriptions of the various ways to call the function (if applicable), and a longer discussion of the arguments.
Value: A description of the class of the value returned by the function.
See also: Links to other relevant functions. In most of the R editors, you can click these links to read the Help files for these functions.
Examples: Worked examples of real R code that you can paste into your console and run.
cmd + enter
: run selected codecmd + shift + enter
: run code chunkcmd + i
: re-indent selected codecmd + shift + c
: comment/uncomment several lines of codecmd + shift + d
: duplicate line underneathctrl + option + i
: make a new code chunk