End Interactive Session 9
ggplot(data = penguins, x = flipper_length, y = body_mass_g) +
geom_point()
{reprex}
, tables, and a ggtheme
package
eds221-m2021-day9-interactive
kableExtra
packagetidyverse
kableExtra
{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:
library(janitor)
in the reprex)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)See more:
reprex
Let’s create some code that throws an error. We’ll try to plot the palmerpenguins
, but forget to use the aes()
function.
We try to troubleshoot on our own, but can’t figure it out so we create a reprex!
Install and load the reprex package.
First, we want to create a small amount of data to reproduce our error to include in our reprex. We’ll use dplyr
functions to limit to just the columns we’re plotting and a few rows and then use datapasta
to create a little data frame with that data.
This should add the following to our script:
Now, we need to create a small chunk of code to reproduce our error.
Prove to yourself that it throws the same error. Then, copy that chunk of code into your clipboard (Cmd + C) and run reprex()
in your console. Your reproducible example is now loaded into your clipboard. Find your neighbor on Slack and send them your reprex as a message!
Now, practice a version on your own.
dplyr::across()
& janitor::get_dupes()
janitor::get_dupes()
to check for duplicatesdplyr::across()
- operations across columnsMutate across multiple columns:
You can use it within group_by() + summarize()
:
Another example:
{kable}
and {kableExtra}
We can produce finalized tables in R Markdown in a number of ways - see a bunch of them in David Keyes’ post How to make beautiful tables in R.
We’ll just use one tool: kable
+ kableExtra
to make nice html tables.
Try it out of the box (knit to see the table):
ggplot
themetheme()
to further customize your theme.my_theme <- function() {
theme(
panel.background = element_rect(fill = "yellow"),
panel.grid.major.x = element_line(colour = "purple", linetype = 3, size = 0.5),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour = "cyan4", linetype = 3, size = 0.5),
axis.text = element_text(colour = "red"),
axis.title = element_text(colour = "orange")
)
}
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). Now, save the .R
script (with the file name matching the function name). This now contains your function and the Roxygen information.
Source the .R
script into your .qmd
ggplot
graph that uses your ggplot theme. Done!End Interactive Session 9