Create a new version-controlled R Project called eds221-m2021-day9-interactive
Install the kableExtra package
Create a new R Markdown document in the project
Attach the following packages in the setup chunk:
tidyverse
kableExtra
2. Making a reproducible example: {reprex}
Making a minimum viable example is often the best way to troubleshoot problematic code when you can’t figure out a solution quickly – and is definitely the best way to share an example of something you’re struggling with so you’re most likely to get help. If people can’t run or play with your code, it’s much less likely they’ll be able to offer a solution.
You probably already have {reprex} (part of the tidyverse). Copy code to clipboard and run reprex() to make one!
Some guidelines:
Ruthlessly simplify
Consider using or making a subset of data (possibly w/ datapasta, tribble)
Include library calls (e.g. library(janitor) in the reprex)
Make the minimum viable example of the thing that’s not working
Copy to clipboard
Run reprex() to create markdown-formatted (e.g. for GitHub issues) or reprex(venue = "slack") if posting to Slack (click “Add formatting” or Cmd-Shift-F for formatting)
3. A few new wrangling tools: dplyr::across() & janitor::get_dupes()
janitor::get_dupes() to check for duplicates
dupes <-get_dupes(starwars) # Across all variables (exact match across all columns?)
No variable names specified - using all columns.
No duplicate combinations found of: name, height, mass, hair_color, skin_color, eye_color, birth_year, sex, gender, ... and 5 other variables
# Check for duplicate values in the `2000` columndupes_2 <- starwars %>%get_dupes(homeworld)# Check for duplicates in the homeworld and species columndupes_3 <- starwars %>%get_dupes(homeworld, species)
Warning: There was 1 warning in `summarise()`.
ℹ In argument: `across(where(is.numeric), mean, na.rm = TRUE)`.
ℹ In group 1: `homeworld = "Alderaan"`.
Caused by warning:
! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
Supply arguments directly to `.fns` through an anonymous function instead.
# Previously
across(a:b, mean, na.rm = TRUE)
# Now
across(a:b, \(x) mean(x, na.rm = TRUE))
`summarise()` has grouped output by 'species'. You can override using the
`.groups` argument.
Species
Island
Body mass (g)
Adelie
Biscoe
3709.659
Adelie
Dream
3688.393
Adelie
Torgersen
3706.373
Chinstrap
Dream
3733.088
Gentoo
Biscoe
5076.016
5. A package for your ggplot theme
Creating a package
In RStudio, create a new R Project that is an R Package (New Project > New directory > R Package). Make your package name something specific to you, e.g. themes_yourname (like themes_teddy). Make it a version controlled repo by running usethis::use_git() and usethis::use_github() (or w/ CLI).
Check out the existing infrastructure for your R package, which currently contains a single function hello(), which prints “Hello, world!” Check out the R/hello.R file to see where that function is created.
In the Build tab, click Install and Restart. You should see that the package is automatically attached in the Console.
Run the hello() function in the Console, to see that it works.
Create a new R script (.R). Copy and paste the following placeholder code into your R script. Then update the colors (in quotations) to colors that R will recognize (or hexidecimals) and change the function name to theme_yourname (e.g. theme_allison). You can also add other options within theme() to further customize your theme.
Save the R script in to the R/ folder (with the file name matching the function name).
Put your cursor anywhere in the function code within your R script. In the top menu of RStudio, select Code > Insert Roxygen skeleton. The information added is important - it specifies the params (arguments of the function) and more that will appear in the documentation, which we’ll create next. Save the .R file, which now contains your function and the Roxygen information.
Document the function by running devtools::document() in the Console. This will create a new .Rd file in the man/ folder, containing important documentation information about your function.
Press Build > Install and Restart. In the Console, run ?function_name, replacing function_name there instead. It will bring up the documentation, and let you know your function exists! Now go ahead and try to use your function by making a graph in the Console.
Once you confirm it’s working, push your changes back to your repo on GitHub. Share your repo “username/reponame” with your neighbor so they can install your package from GitHub (recall: using remotes::install_github("username/reponame")). Install your neighbor’s theming package, check which functions exist by running help(package = "packagename") in the Console, then make a ggplot graph that uses your neighbor’s ggplot theme. Done!