Hi everybody! This is an RMarkdown document. You can do italics and bold, and include a link for “Introduction to R Markdown”.
values <- rnorm(5)
values
## [1] -0.6862909 -0.6082460 0.2686655 0.7560391 -0.1002797
If you choose to only view the code and not the ourcome (OR not evaluate the code), place a comma after r in the curly brackets and write eval=FALSE
values <- rnorm(5)
values
We can also use the echo option (echo=FALSE)to show or not show the code and results/output. These options can be toggled using the setting icon within the grey box - for options on outputs… whether you want the output, code, both or neither.
THE SLIDERS - Particular for showing warnings and showing messages, when you knit your document, (by default) R Markdown document will include both warnings and messages, which you may not want in a professional document. A warning may show up after a ggplot when rows with values that are supposed to be there are not there; and, you might want to suppress that warning and talk about the missing values in the text of the document.
On line 34, “title chunk” is the name of that particular CODE CHUNK, which would not change what the knitted document looks like.
However, at the very bottom of teh source window, there is an “up and down arrow”. When you click on it, you can see outline with both the headings that you’ve set up and the chunks so you can navigate. With longer markdown documents, this will become important, therefore it is encouraged to NAME EACH CHUNK RIGHT AWAY- as a best practice.
Using the first chunk that R provided with the template document (at line 8):1. Set up DEFAULTS where echo=TRUE, by default, the codes will be shown for the rest of the document - unless otherwise stated… and 2. IMPORT LIBRARIES needed for example library(tidyverse) or library(palmerpenguins), however, as include=FALSE, all the codes will be evaluated but not used… More codes can be added as you proceed along the document.
Subheadings are created using two hashtags (##).
Unordered Lists:
first item
second item
Ordered Lists:
first item
second item
We can insert an image using the exclamation mark (!). Include
text/description in square brackets for someone who is not seeing the
image and the path to the file in parenthesis. The size of the picture
can be manipulated by adding a absolute value or percentage for the
width within curly brackets {}… Example: {width=30%}
We can citations or footnotes in multiple ways in R. The simplest you way is to add a superscript to the word citations. At the base of the document, the actual text in the square brackets will show by typing citations1, citations and superscript 1 will appear within the document.
We can use math mode by typing \(\alpha \beta\) for the greek alphabet to appear - and so on.
We can make nice tables in R Markdown, the most basic way is by using the kable() command in the knitr package. Library(knitr) would need to be added to the first code chunk. Then add a new code chunk below and use the kable() command to add a basic table using the first five rows of the palmerpenguin dataset - using the codes below:
penguins_sm <- head(penguins)
kable(penguins_sm)
| species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
|---|---|---|---|---|---|---|---|
| Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
| Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
| Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
| Adelie | Torgersen | NA | NA | NA | NA | NA | 2007 |
| Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
| Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |
The code is requesting that a small section of the penguins data set be created (penguins_sm), and a table showing only the head or first 5-6 rows. Then the kable() command requests a table of penguins_sm :- kable(penguins_sm). A table will appear when Knit.
The library ggplot2 will called in a new code chunk inserted below for the actual scatterplot.
library(ggplot2)
data(mpg, packag="ggplot2")
theme_set(theme_bw())
g <- ggplot(mpg, aes(cty, hwy))
g + geom_jitter(width = .5, size=1) +
labs(subtitle="mpg: city vs highway mileage",
y="hwy",
x="cty",
title="Jittered Points")
Under the HELP MENU, there are CHEAT SHEETS: the R Markdown Cheat Sheet and the R Markdown Reference Guide which are useful with commands for TEXT FORMATTING like italics, bold and links and some of the basic options for chunks and YAML.
footnotes↩︎