[1] 100
[1] 2187
[1] 0.6666667
[1] -34
BSTA 511/611 Fall 2024, OHSU
2024-10-02
For the history and details: Wikipedia
R is a programming language
RStudio is an integrated development environment (IDE)
= an interface to use R (with perks!)
Read more about RStudio’s layout in Section 3.4 of “Getting Used to R, RStudio, and R Markdown” (Ismay and Kennedy 2016)
When you first open R, the console should be empty.
Typing and executing code in the console
Variables are used to store data, figures, model output, etc.
mean()
is an example of a function()
?mean
in console will show help file for mean()
Incomplete commands
Object is not found
Example:
or, creating reproducible reports
.qmd file
html output
.qmd
file = Code + textknitr
is a package that converts .qmd
files containing code + markdown syntax to a plain text .md
markdown file, and then to other formats (html, pdf, Word, etc)
.qmd
)Two options:
Pop-up window selections:
HTML
output format (default)Knitr
Use visual markdown editor
Create
.qmd
)Create
, you should then see the following in your editor window:.qmd
)File -> Save
,We create the html file by rendering the .qmd file.
Two options:
Note
.qmd file
html output
bold, italics, superscripts & subscripts, strikethrough, verbatim
, etc.
Text is formatted through a markup language called Markdown
(Wikipedia)
Newer versions of RStudio include a Visual editor
as well that makes formatting text similar to using a word processor.
Visual editor
Visual editor
is similar to using a wordprocessor, such as Wordcode
format.Source
editor and examine the markdown code that was used for the formatting.Questions:
Markdown
Markdown: | Output: |
---|---|
|
This text is in italics, but so is this text. |
|
Bold also has 2 options |
|
|
|
Needsuper orsub scripts? |
|
Code is often formatted as verbatim |
|
|
#
at the beginning of the line to create headersText in editor:
Output:
Important
Make sure there is no space before the #
, and there IS a space after the #
in order for the header to work properly.
You can easily navigate through your .qmd file if you use headers to outline your text
.qmd file
html output
3 options to create a code chunk
Click on at top right of the editor window, or
Keyboard shortcut
Mac | Command + Option + I |
PC | Ctrl + Alt + I |
Visual editor
: Select Insert
-> Executable Cell
-> R
An empty code chunk looks like this:
Visual editor
Source editor
Important
Note that a code chunks start with ```{r}
and ends with ```
. Make sure there is no space before ```
.
Run Selected Line(s)
,Mac | ctrl + return |
PC | command + return |
Note
Full list of keyboard shortcuts
action | mac | windows/linux |
---|---|---|
Run code in qmd (or script) | cmd + enter | ctrl + enter |
<- |
option + - | alt + - |
interrupt currently running command | esc | esc |
in console, retrieve previously run code | up/down | up/down |
keyboard shortcut help | option + shift + k | alt + shift + k |
Try typing code below in your qmd (with shortcut) and evaluating it:
YAML
metadataMany output options can be set in the YAML metadata, which is the first set of code in the file starting and ending with ---
.
YAML
exampleYAML:
Output:
Important
---
.---
must be on the very first line.format:
optionFrom Garrett Grolemund’s Prologue of his book Hands-On Programming with R1:
As you learn to program, you are going to get frustrated. You are learning a new language, and it will take time to become fluent. But frustration is not just natural, it’s actually a positive sign that you should watch for. Frustration is your brain’s way of being lazy; it’s trying to get you to quit and go do something easy or fun. If you want to get physically fitter, you need to push your body even though it complains. If you want to get better at programming, you’ll need to push your brain. Recognize when you get frustrated and see it as a good thing: you’re now stretching yourself. Push yourself a little further every day, and you’ll soon be a confident programmer.