Reproducible Programming

What do you want to do?

  • Access data
  • Build models
  • Test hypotheses
  • Run analyses
  • Report the results
  • Share the results and the techniques with others

What's different about R?

What's different about R? {.build}

What are formulae?

?formula

What are factors?

?factor
?ordered
?levels

What are data frames? {.smaller}

head(mtcars)

```{R, eval = FALSE} ?data.frame ?read.csv ?read.table

## How do we subset vectors?

```{R}
vec <- c(1, 2, 3)
vec[vec > 2]
vec <- 1:3
vec > 2
vec <- seq(from = 1, to = 3)
subs <- c(T, F, T)
vec[subs]

How do we subset data frames?

df <- data.frame(val = c(1, 2, 3),
                 other = c("a", "b", "a"))
df$other
df[df$other == "b", ]
subset(df, val == 1)

How do we subset data frames?

But these things are constantly evolving: ```{R, message = FALSE} library(dplyr) df %>% filter(other == "b")

```{R}
mtcars %>% filter(mpg > 20) %>% filter(carb >= 4)

How do we plot?

plot(c(1, 2, 3), c(1, 2, 1))
lines(c(1, 2, 3), c(2, 1, 2))

How do we plot? {.smaller}

Again, there are much more sophisticated ways, as you know... e.g. ```{R, message = FALSE} library(ggplot2) mtcars %>% ggplot(aes(wt, mpg)) + geom_point() + geom_smooth()

## How do we save to disk?

```{R, eval = FALSE}
png("out.png")
plot(c(1, 2, 3), c(1, 2, 1), type = "l")
dev.off()

```{R, eval = FALSE} ?pdf ?png ?Devices

## How do we save to disk? 

Again, using ggplot2...
```{R, eval = FALSE}
g <- mtcars %>% ggplot(aes(wt, mpg)) + geom_point() + geom_smooth()
ggsave("out.png", g)

```{R, eval = FALSE} ?ggsave

## How do we use packages?

Install it first:

```{R, eval = FALSE}
install.packages(c("tibble"))

or from RStudio menus, and then use it:

```{R, eval = FALSE} library(tibble)

not forgetting:

```{R, eval = FALSE}
update.packages()

occasionally.

So how do you learn to program?

{ width=80% }

What do you want to do?

What are you going to do?

Learn how to:

  • Write code
  • Write documentation
  • So you can come back and understand it later
  • Identify and isolate reusable code
  • So you don’t have to write it again later
  • Generate documented results
  • Read and understand other people's code

Remember... {.flexbox .vcenter .smaller}

{ width=100% }

"I honestly didn't think you could even USE emoji in variable names. Or that there were so many different crying ones."

https://imgs.xkcd.com/comics/code_quality.png

What are you going to do?

  • Read and write well-documented and well-structured code
  • Use this to generate clear and well-documented results/reports

How are you going to do it?

  • Problems will be structured around population dynamics, epidemiology and biodiversity
  • They will start off easy and ramp up
  • Packages (libraries) will allow you to solve many problems easily, but we are focusing on problems they don’t help with!

Have fun! {.flexbox .vcenter .smaller}

{ width=100% }

http://phdcomics.com/comics/archive.php?comicid=946

Starting with...

and optionally:



IBAHCM/RPiR documentation built on Jan. 12, 2023, 7:41 p.m.