The first step when opening a new R studio environment or project is creating a script or notebook for working in. Scripts are basic text files where all code is executable. Writing non-code in a script requires the use of #’s (which can look messy and confusing) like so:
read.csv("datafile.csv")
# this code reads a csv (data) file into R. The command read.csv requires brackets with the filepath to the file in quotations.
# in this code, none of the #'s will run. so if I # the read.csv command, it will not run. like so:
# read.csv("datafile.csv")
A whole document of the above example can get messy and hard to understand.
In a notebook, we separate normal text from code by inserting “code chunks” (insert > R in the top right of the window). Chunks are specialised areas in the notebook for code only. Chunks separate code from text, making it easier to write notes and read. These tutorials have been written in a notebook.
# Only if you have not created a project
setwd("Drive:/Folder1/Folder2")
# insert your folders path in the brackets
# this will tell R to look here for files and "generally" save things here as well.
# e.g. C:/Users/Mitch/Documents/R/
After writing your code, you can click run, run selected line(s), run current chunk or press Ctrl + Enter on the line your cursor is on