Skip to content
R for the Rest of Us Logo

This lesson is locked

Get access to all lessons in this course.

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.

Your Turn

  1. Add a table of contents and make it floating

  2. Adjust default figure height, width, and captions

  3. Add a parameter to the YAML and use it in the body of your report to dynamically create a table of the top 10 districts by various race/ethnicity categories

Learn More

As I was making this course, I was confused about when you would define things like figure height and width in the YAML versus in the setup code chunk. Yihui Xie, developer of RMarkdown, helpfully laid out the differences for me in this RStudio Community thread.

I mention parameterized reporting in the video. This is the idea of making multiple reports at once. The Urban Institute has a nice walkthrough of how this works.

If you're looking for more YAML options, check out RMarkdown: The Definitive Guide as well as the ymlthis package.

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

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

Abby Isaacson

Abby Isaacson

May 25, 2021

FYI I got this error AFTER I had successfully changed the variable names, so I updated: columns = vars(...)` has been deprecated in gt 0.3.0:

  • please use columns = c(...) insteadcolumns = vars(...) has been deprecated in gt 0.3.0:
  • please use columns = c(...) instead

Abby Isaacson

Abby Isaacson

May 25, 2021

I meant after I applied % format!

David Keyes

David Keyes

May 26, 2021

Thanks for the heads up! Looks like they changed the name of the argument after I recorded this video.

Abby Isaacson

Abby Isaacson

May 25, 2021

Also, any ideas why my test caption doesn't show up? YAML: output: html_document: toc: TRUE toc_depth: 2 toc_float: TRUE fig_width: 8 fig_height: 10 fig_caption: TRUE

CODE:

#options(scipen = 999)

enrollment_by_race_ethnicity %>% 
  filter(race_ethnicity == params$race_ethnicity_cat) %>% 
  pivot_wider(id_cols = c(district, district_id),
              names_from = year,
              values_from = percent_of_school) %>% 
  mutate(growth = `2018-2019` - `2017-18`) %>% 
  arrange(desc(growth)) %>% 
  select(-district_id) %>% 
  slice_max(growth, n = 10) %>% 
  gt() %>% 
  cols_label(
    district = "District",
    growth = "Growth") %>% 
fmt_percent(
    columns = c(`2017-18`, `2018-2019`, growth),
    decimals = 1
  )

David Keyes

David Keyes

May 26, 2021

I think captions may only work on plots (though I'm not sure). It looks like for gt, you can use the tab_source_note() function to add a caption.

the floating TOC in this course and elsewhere (e.g., https://bookdown.org/yihui/rmarkdown/html-document.html) keep the location within the TOC when I click to a different portion. But I’ve noticed that the TOC for R Graphics Cookbook always goes back to the top upon click (which I find annoying). What setting dictates whether the TOC “stays put” vs “returns to top”?

Charlie Hadley

Charlie Hadley

December 13, 2021

To try and understand why this is happening I tried to build the R Graphics Cookbook book on my own machine and can't give you a specific explanation, but here are possibilities:

  1. I couldn't get all the chapters to successfully knit within 10mins of attempted fixes, so there may be code in these chapters that results in the TOC breaking. However, this is unlikely.

  2. When the author (Winston Chang) built the book and pushed it to GitHub they might have been using an old/development build of the {bookdown} (or other dependent) package. This is quite likely.

  3. The book also uses a non-standard approach to write the book. It's fairly technical to explain how. The {bookdown} book describes the standard way to build a {bookdown} book. Whereas the author uses a make file and further complicates the build process with additional scripts. It's possible that the author also uses a non-standard approach to building the book which results in the TOC not behaving properly. This is probable, it might also combine together with option 2.

In principle, you could open an issue on the GitHub repo to ask the author to look into this. However, it would be completely up to the author to decide if they consider it an issue or not.

Thanks Charlie! I really like how the floating TOC keeps its place on most sites (like this course) and wanted to make sure that I knew how to do so. If that is the norm, then I think I should be all set!

How do I know where I can put params$parameter_of_choice into the report and have it knit correctly? I tried to put it into the cols_label() function within quotes and it just showed up as "params$race_ethnicity" in the html doc: enrollment_race_ethnicity_with_percentages %>% filter(race_ethnicity == params$race_ethnicity) %>% filter(schoolyear == "2018_2019") %>% slice_max(percent_ethnicity, n = 10) %>% select(district, percent_ethnicity) %>% gt() %>% cols_label( district = "District", percent_ethnicity = "2018-2019 params$race_ethnicity Population Share" )

Try changing

percent_ethnicity = “2018-2019 params$race_ethnicity Population Share”

to

percent_ethnicity = str_glue(“2018-2019 {params$race_ethnicity} Population Share”)

What's happening is that R doesn't know that params$race_ethnicity should be treated as code, not text. By putting it inside str_glue() and using the {} it is treated as code.

Sara Cifuentes

Sara Cifuentes

June 8, 2022

Hi, Although I have included this information in my YAML: output: html_document: toc: TRUE toc_depth: 2 toc_float: TRUE

I can't see the table of contents. Should I activate any specific library? Thank you in advance.

Charlie Hadley

Charlie Hadley

June 9, 2022

Hi Sara!

In R we're quite lucky because the language doesn't really care about indentation, but annoyingly YAML does.

We need to indent "html_document" with respect to output and then the toc options with respect to html_documment, here's an example:

<script src="https://gist.github.com/charliejhadley/6eb9c98f03625df495b1d3e373c74008.js"></script>

Let me know if that fixes your issue!

Sara Cifuentes

Sara Cifuentes

June 9, 2022

Yes, thank you Charlie

Emma Spielfogel

Emma Spielfogel

December 23, 2022

How could you dynamically pick the district with the most growth for the highlight_district data frame in the ggplot chart? We previously explicitly typed "Douglas ESD" as the one with the most growth for the Hispanic/Latino population then highlighted it in orange in the chart. Is there a way to automatically highlight the district with the most growth, based on the race_ethnicity parameter?

Yup! You can access the data in the parameter with params$race_ethnicity_group (assuming your parameter name is race_ethnicity_group). So you could filter your data to say race_ethnicity == params$race_ethnicity_group. Does that make sense?