Get access to all lessons in this course.
-
Setup packages
- devtools and usethis
- Package names and authors
- Choose a license
- Setup git + GitHub
-
Write code
- Writing functions
- Adding functions to our package
- Using other packages
- Packages so good they get their own functions
- Organizing .R files
-
Document
- roxygen2: In-Line Documentation for R
- Argument descriptions and examples
- Helper functions
- Joining documentation
-
Test
- Unit tests for R
- Organizing tests
- R CMD Check
- Types of Test Files
- Test Coverage
-
Teach
- Examples
- README
- Spellcheck
-
Add data and files
- Store data and files
- Include data for tests and create RMarkdown templates
- Create other templates
- Launch and store Shiny apps
- Citations, ignoring files, and including add-ins
Package Development with R
Writing functions
This lesson is locked
This lesson is called Writing functions, part of the Package Development with R course. This lesson is called Writing functions, part of the Package Development with R course.
If the video is not playing correctly, you can watch it in a new window
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.
You need to be signed-in to comment on this post. Login.