Skip to content
R for the Rest of Us Logo

This lesson is locked

Get access to all lessons in this 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.

Your Turn

Working in exercises.Rmd:

  • Fix the code in R/themes.R to use ggplot2:: instead of library(ggplot2)

  • Run use_package(“ggplot2”) to add ggplot2 to Imports

  • Re-load the package (Cmd/Ctrl+Shift+L) and run this code to make sure it works

use_package("ggplot2")

### in R/themes.R:

theme_avalanche <- function() { 
     ggplot2::theme_minimal(base_size = 14) + 
     ggplot2::theme(panel.grid.minor = ggplot2::element_blank())
}

Learn More

You can learn more about using functions from other packages in your package in the Dependencies chapter of the R Packages book.

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

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

Mark Engelbert

Mark Engelbert

January 18, 2024

I'm finding that even after fixing the themes.R file, running use_package("ggplot2"), and re-loading the package, I get a "could not find function 'ggplot' " error when I try to run the next code block. I can only get that code block to work if I put ggplot2:: in front of all the ggplot functions in the block. But in the solution video, the block runs without ggplot2::. I took a screen capture of what I'm getting. Any idea what I'm doing wrong?

David Keyes

David Keyes Founder

January 19, 2024

So I think what is happening is that the code you are trying to run is totally separate from the function you're working on. While you can now use the ggplot() function in your theme_avalanche() function, the ggplot2 package is not loaded when you are trying to run your code in the R Markdown document. You still need to run library(ggplot2) or library(tidyverse) in order to use ggplot there. Does that make sense?

Mark Engelbert

Mark Engelbert

January 22, 2024

That makes sense, yeah. I guess when Malcolm says (in the Solution video) "Now our function will work without having loaded the ggplot2 package", I took that to mean he didn't have ggplot2 loaded in the global environment and I was trying to replicate that.

All good now, thanks for the help.