<- read_csv("data/pigs.csv") %>%
pigs mutate(
dose = factor(dose, levels = c("Low", "Med", "High")),
supp = factor(supp, levels = c("VC", "OJ"))
)
RAdelaide 2024
July 10, 2024
rmarkdown
rmarkdown
is a cohesive way to
knitr
is the engine behind this
Sweave
package about 8-10 years agoExtends the markdown
language to incorporate R code
rmarkdown
pandoc
README.md
#
symbols
#
is the highest level, ##
is next highest etc.*
) or underscore (_
)**
) or two underscores (__
)-
-
1.
Let’s quickly edit our file so there’s something informative
Enter this on the top line
# RAdelaide 2024
Two lines down add this
## Day 1
Leave another blank line then add
1. Introduction to R and R Studio
2. Importing Data
3. Data Exploration
4. Data Visualisation
Underneath the list enter:
**All material**
can be found at [the couse homepage](http://blackochrelabs.au/RAdelaide24/)
Preview Button
and an HTML document appearsrmarkdown
We can output our analysis directly as:
ioslides
or PowerPoint presentationsWe never need to use MS Word, Excel or PowerPoint again!
rmarkdown
.Rmd
R
code.Let’s create our first rmarkdown
document
File
drop-down menu in RStudioRMarkdownTutorial.Rmd
A header section is enclosed between the ---
lines at the top
.css
files, load LaTeX packages, set parameters etc.Lines 8 to 10 are a code chunk
R
code goes between these two delineation marksr
bash
, python
etc.)r
tells RMarkdown the chunk is an R
chunkLine 12 is a Subsection Heading, starting with ##
Tools
> Global Options
> R Markdown
##
#
Check the help for a guide to the syntax.
Help > Markdown Quick Reference
#
gives Section \(\rightarrow\) Subsection \(\rightarrow\) Subsubsection etc.Typewriter font
is set using a single back-tick `Typewriter`The default format is an html_document
& we can change this later. Generate the default document by clicking Knit
The Viewer Pane will appear with the compiled report (probably)
summary(cars)
temperature
Vs. pressure
has been embeddedecho = FALSE
.PDF
requires \(\LaTeX\)
Now we can modify the code to create our own analysis.
pigs
datasetWhat do we need for our report?
Alt+Ctrl+I
creates a new chunk on Windows/Linux
Cmd+Option+I
on OSXload-packages
next to the ```{r
library(tidyverse)
in the chunk body
Knit…
tidyverse
is a little too helpful sometimes
tidyverse
has loadeddplyr::filter
Vs. stats::filter
)setup
setup
and add include = FALSE
knitr::opts_chunk$set(message = FALSE)
Knit…
Below the load-packages
chunk:
load-data
pigs
using read_csv()
Now let’s add a section header for our analysis to start the report
## Data Description
after the header and after leaving a blank line60 guinea pigs were given vitamin C, either in their drinking water in via orange juice. 3 dose levels were given representing low, medium and high doses. Odontoblast length was measured in order to assess the impacts on tooth growth
nrow(pigs)
would give us the number of pigsReplace the number 60 in your description with `r
nrow(pigs)
`
Knit…
fig.cap
section of the chunk header
My example text:
Odontoblast length shown by supplement method and dose level
data-summary
# A tibble: 6 × 4
supp dose n len
<fct> <fct> <int> <chr>
1 VC Low 10 7.98 ±2.75
2 VC Med 10 16.77 ±2.52
3 VC High 10 26.14 ±4.8
4 OJ Low 10 13.23 ±4.46
5 OJ Med 10 22.7 ±3.91
6 OJ High 10 26.06 ±2.66
Knit…
pander
pigs %>%
summarise(
n = n(),
mn_len = mean(len),
sd_len = sd(len),
.by = c(supp, dose)
) %>%
mutate(
mn_len = round(mn_len, 2),
sd_len = round(sd_len, 2),
len = paste0(mn_len, " ±", sd_len)
) %>%
dplyr::select(supp, dose, n, len) %>%
rename_with(str_to_title) %>%
pander(
caption = "Odontoblast length for each group shown as mean±SD"
)
Supp | Dose | N | Len |
---|---|---|---|
VC | Low | 10 | 7.98 ±2.75 |
VC | Med | 10 | 16.77 ±2.52 |
VC | High | 10 | 26.14 ±4.8 |
OJ | Low | 10 | 13.23 ±4.46 |
OJ | Med | 10 | 22.7 ±3.91 |
OJ | High | 10 | 26.06 ±2.66 |
lm()
is used to perform linear regression
Call:
lm(formula = len ~ supp + dose + supp:dose, data = pigs)
Residuals:
Min 1Q Median 3Q Max
-8.20 -2.72 -0.27 2.65 8.27
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.980 1.148 6.949 4.98e-09 ***
suppOJ 5.250 1.624 3.233 0.00209 **
doseMed 8.790 1.624 5.413 1.46e-06 ***
doseHigh 18.160 1.624 11.182 1.13e-15 ***
suppOJ:doseMed 0.680 2.297 0.296 0.76831
suppOJ:doseHigh -5.330 2.297 -2.321 0.02411 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.631 on 54 degrees of freedom
Multiple R-squared: 0.7937, Adjusted R-squared: 0.7746
F-statistic: 41.56 on 5 and 54 DF, p-value: < 2.2e-16
Estimate | Std. Error | t value | Pr(>|t|) | ||
---|---|---|---|---|---|
(Intercept) | 7.98 | 1.148 | 6.949 | 4.984e-09 | * * * |
suppOJ | 5.25 | 1.624 | 3.233 | 0.002092 | * * |
doseMed | 8.79 | 1.624 | 5.413 | 1.463e-06 | * * * |
doseHigh | 18.16 | 1.624 | 11.18 | 1.131e-15 | * * * |
suppOJ:doseMed | 0.68 | 2.297 | 0.2961 | 0.7683 | |
suppOJ:doseHigh | -5.33 | 2.297 | -2.321 | 0.02411 | * |
Observations | Residual Std. Error | \(R^2\) | Adjusted \(R^2\) |
---|---|---|---|
60 | 3.631 | 0.7937 | 0.7746 |
pander
is a good all-rounder
broom::tidy()
lm()
output to a tibble
# A tibble: 6 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 7.98 1.15 6.95 4.98e- 9
2 suppOJ 5.25 1.62 3.23 2.09e- 3
3 doseMed 8.79 1.62 5.41 1.46e- 6
4 doseHigh 18.2 1.62 11.2 1.13e-15
5 suppOJ:doseMed 0.680 2.30 0.296 7.68e- 1
6 suppOJ:doseHigh -5.33 2.30 -2.32 2.41e- 2
reactable
creates amazing looking tablesDT
also creates fantastic tables
csv
, xls
etc.gt
is popular with somextable
is excellent for \(\LaTeX\) outputAfter you’re happy with the way your analysis looks
Session Info
sessionInfo()
So far we’ve been compiling everything as HTML, but let’s switch to an MS Word document. We could email this to our collaborators, or upload to Google docs
This basic process is incredibly useful