Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include image in R packages

Tags:

package

r

I am trying to create a package for my company where there would be some custom color palettes and our company logo. I successfully built the package that contains all the required color palettes so far, however I am stuck when trying to include the logo as well.

I thought once the logo is loaded into R, it is just another data frame and I could simply use this function to store it:

logo <-  image_read("logo.png")
logo <- image_scale(logo, "50")    
usethis::use_data(logo,company_logo)

However, I encountered an error:

Error: Image pointer is dead. You cannot save or cache image objects between R sessions.

Now I am not sure if my objective would be possible?

like image 947
southwind Avatar asked Mar 05 '19 00:03

southwind


1 Answers

You can install additional files in subdirectories under inst in your package. For example create a directory inst/logos in you package and put your logos there. When the package is installed this creates a directory logos in your package directory. Then you can use system.file to access the images when the package is installed. You could create a function in your package that does this for you. For example:

company_logo <- function() {
  magick::image_read(system.file("logos/logo.png", "MyCompanyPackage"))
}

The reason that storing the result of image_read using save doesn't work, is that the result returned by image_read is a pointer to some memory allocated by the package. When saving the result only the pointer is saved not the data pointed to.

like image 179
Jan van der Laan Avatar answered Nov 15 '22 10:11

Jan van der Laan