Office hours will always be held at the same Zoom link.
Office hours will always be held at the same Zoom link.
RStudio is an Integrated Development Environment (IDE) for R
[source]
RStudio used to be the name of a company that is now called Posit.
Easier working with R
More information
First it is important to be familiar with the layout. When you first open RStudio, you will see 3 panes.
In a .R file (we call a script), code is saved on your disk
Super useful “cheat sheet”: LINK
It will look like this with text in it, unlike a script.
Although people will use scripts often, and they are good for more programmatic purposes, we generally don’t recommend them for Public Health Researchers.
For data analyses, R Markdown files are generally superior because they allow you to check your code and write more info about your code.
History
If RStudio doesn’t look the way you want (or like our RStudio), then:
Click on the pane button, which looks like a waffle with 4 indentations. Scroll down to “Pane Layout”.
To do this lab we need to:
These videos can help if you aren’t sure where your downloads are:
If you have a PC: https://youtu.be/we6vwB7DsNU
If you have a Mac: https://www.youtube.com/watch?v=Ao9e0cDzMrE
You can find these on the resource page of the class website.
R Markdown files (.Rmd) help generate reports that include your code and output. Think of them as fancier scripts.
Go to File → New File → R Markdown or click the green add file button.
Within R Markdown files are code “chunks”.
This is where you can type R code and run it!
Clicking the run (play) button runs the code in the chunk.
Ctrl + Enter
on Windows or Command + Enter
on Mac in your script evaluates that line of code
See the Help page of the website. You can adjust this and change your RStudio settings:
Tools > Global Options > Appearance
Running all chunks - this will create a report from the R Markdown document!
This generates a nice report that you can share with others who can open in any browser.
To create a new R code chunk:
If you like keyboard shortcuts:
I is for insert.
You can run all chunks above a specific chunk using this button:
You can specify if a chunk will be seen in the report or not.
R studio can help you find issues in your code. Note that sometimes the error occurs earlier than RStudio thinks.
Ctrl + Enter
on Windows or Command + Enter
on Mac in your script evaluates that line of code
Ctrl+1
on Windows or Command + 1
on Mac takes you to the script pageCtrl+2
on Windows or Command + 2
on Mac takes you to the consoleWhen you type in a function name, a pop up will preview documentation to help you. It also helps you remember the name of the function if you don’t remember all of it!
?
If you know the name of a package or function:
Type ?package_name
or ?function_name
in the console to get information about packages and functions.
For example: ?readr
or ?read_csv
.
If you haven’t loaded a package yet into R than you may get a response that there is no documentation.
Typing in ??package_name
can show you packages that you haven’t loaded yet.
💻 Lab
Image by Gerd Altmann from Pixabay