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

  • Change the default data frame printing method to use kable

  • Choose one of the table packages and make an attractive table that shows the top 10 districts with the highest percentage of Hispanic/Latino students (hint: use the slice_max() function to get the top 10 and the fmt_percent() function if you’re using gt or the percent() function otherwise to display the percentage of Hispanic/Latino students in each)

Please note that in order to use the slice_max() function, you need to have dplyr 1.0 or greater installed.

Learn More

If you want to read more about df_print options, you can find that here.

If you’re looking for an overview of all of the table packages discussed in this lesson, check out this comprehensive blog post I wrote.

That blog post includes links to some great tutorials on the various packages. Some additional tutorials have come out since then, especially on the gt package. See, for example, tutorials by:

In case it’s not already clear, the gt package is really popular, in large part because of this endorsement from Hadley Wickham.

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

I'm not sure at what step my years got off, but my enrollment_by_race_ethnicity dataset has always displayed '2018-2019' for one year and '2017-18' format for the other. I tried using col_labels to rename a numeric variable but it didn't work. Can I rename in gt, or do I have to go back to the dataset?

David Keyes

David Keyes

May 26, 2021

You can do it either way. If you want to rename in gt, you'd use the cols_label() function.

Abby Isaacson

Abby Isaacson

May 26, 2021

Thanks! I was missing the (back tick) around the original number. So cols_label(2017-18` = "2017-2018") worked.

David Keyes

David Keyes

May 26, 2021

Glad you got it figured out!

Jessica Sickler

Jessica Sickler

May 26, 2021

Is it possible to print a formatted table as an image to insert in report or document outside of R?

David Keyes

David Keyes

May 26, 2021

Yep! You can do this with the gtsave() function from the gt package. The save_as_image() function from flextable does the same thing. I'm not sure if the other packages do something similar because I haven't used them as much.

Jody Oconnor

Jody Oconnor

May 29, 2021

I'd like to create the 'your turn' table using flextable, but I'm not having much luck with the syntax. Could you post example code using flextable instead of gt please? Thanks!

David Keyes

David Keyes

June 1, 2021

Yup! I'll do this soon. I'm moving tomorrow but should be able to do it later this week.

David Keyes

David Keyes

June 4, 2021

I finally got around to doing this. You can see a video here and my code here. Please let me know if you have any questions!

Jody Oconnor

Jody Oconnor

June 7, 2021

Thanks! I think I like gt better (flextable seems to require more code to do the same thing and is less intuitive), but I want to be able to knit to word. The one thing I can't seem to do is format a title in flextable (it lets me set a caption at the top, but as far as I can tell the formatting can only be changed through the Word document's style with officer::styles_info). Is that true or am I missing something? If it is true, I'd be happy to see an example of how to make styles_info work if you have time... the documentation is a bit confusing.

Also, just so you know, your video doesn't show your R studio window after it spontaneously closed and you re-opened it. No need to re-record it though since you posted the code.

David Keyes

David Keyes

June 9, 2021

Hmm, I don't actually know the answer to your question on titles, but I think it probably does use the Word styles for titles. You could use Word reference docs to style those. Another option I see from a quick perusal of the docs is this: https://davidgohel.github.io/flextable/reference/add_header_lines.html. Looks like you could add either a title or a caption with it.

Atlang Mompe

Atlang Mompe

June 25, 2021

Hi David, it seems that I cannot load the gt library - I have tried to follow your video? Please advise. > library(gt) Error in library(gt) : there is no package called ‘gt’

Atlang Mompe

Atlang Mompe

June 25, 2021

OMG I figured it out - this may be a basic question David but sometimes when I try to load a library and it refuses, if I step away from the computer, I usually find an option to install, what prompts this, I know we most probably covered this in our earlier classes but moving forward, is it best to install the package - from packages before loading the library - sorry for the basic question - we have covered a lot in the past 3 months.

David Keyes

David Keyes

June 25, 2021

Hmm, hard to know exactly without seeing your computer. Do you get a specific error when you try to install a package and it fails to do so?

Andrew Paquin

Andrew Paquin

May 29, 2023

Hi David, As I worked through this stuff today, I received several warnings that "vars" has been deprecated and that I should used c() instead. Has vars gone the way of the pet rock? Is there a hard date after which it simple won't work at all?

David Keyes

David Keyes

May 30, 2023

I didn't actually know this change happened! I did, however, just look at the changelog. It does seem like vars() will be going away eventually so probably best to use c() instead.