Skip to content
R for the Rest of Us Logo

Package Development with R

Writing functions

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

For this section of the course, you'll need the materials in this GitHub repo. You can download or clone them.

Your Turn

After you download or clone the materials from the GitHub repo for this section of the course, open the entire project by clicking 02_avalanchr.Rproj and then work in the exercises.Rmd file.

  • Re-write this ggplot2 theme as a function. Call it theme_avalanche_h().

  • Run this code to test that your function works

theme_avalanche_h <- function() { 
              theme_minimal(base_size = 14) +
              theme(panel.grid.minor = element_blank(), panel.grid.major.y = element_blank()) 
}

residents_per_sector <- data.frame(
              sector = as.factor(1:8),
              residents = c(1000, 2034, 4594, 2304, 8093, 1200, 300, 2398)
)

ggplot(residents_per_sector, aes(forcats::fct_reorder(sector, residents), residents)) + 
             geom_col() +
             coord_flip() +
             xlab("sector") +
             theme_avalanche_h()

Learn More

If you want a refresher on writing functions, there is a chapter in the 2nd edition of R for Data Science on the topic.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.