1

I have a manually created color palette which my package is using. I have included my .rds color palette file into my package folder and I refer to it within my functions by load(paste0(getwd(),'/my_palette.rds')). However I find it very unpractical for example if I want to share my package, and someone what to change his working directory - then the reference won't work. I'm looking for a solution which would build this color palette within R automatically while installing package so that I don't need to have exact file in package folder to which I have to refer. Is this possible ?

EDIT

I created data/ folder in my package folder using commands below :

x<-load(paste0(getwd(),'/my_palette.rds'))
usethis::usedata(x)

Then I clicked on this .rda in data/ folder to load this into my working directory. However I don't know how should I refer to it. load('x') doesn't work.

EDIT vol 2

I will describe full algorithm of adding my_palette.rds to my package which I'm applying :

  1. Create manual color palette
  2. Refer to it by command x<-load(dir/my_palette.rds)
  3. Then I'm using command usethis::use_data(x) (then data/ folder is created with .rda file within it)
  4. After that I'm using ggplot2 function with scale_fill_manual(my_package::x). The warning I get is : Error: Unknown colour name: my_palette

EDIT vol 3

I want to describe process of creating color palette so that we can see where is the basis of the error. So the main difference between what you did and what I did is that you created colour palette as a vector of color to future reference. (This is very important, your colour palette is stored in a vector type). what I did is to create functions which create a colour palette and after that I saved it into .rds file. And I believe that it might be the main difference that my colour palette is stored within file. Do you think that it's significant difference ? Or it can be omitted somehow ?

John
  • 1,849
  • 2
  • 13
  • 23
  • I think you should [add data to your package](https://stackoverflow.com/questions/12391195/include-data-examples-in-developing-r-packages) or create a function that returns the palette. – Edo Oct 29 '20 at 12:24
  • Let's assume that I have my function which creates manually color palette in R folder. How can I refer to that palette ? – John Oct 29 '20 at 13:49
  • have you tried `name_of_your_package::name_of_your_data`? – Edo Oct 29 '20 at 14:00
  • I get the error following when trying to use it : Error: Insufficient values in manual scale. 2 needed but only 1 provided. Run `rlang::last_error()` to see where the error occurred. – John Oct 29 '20 at 14:11
  • I need more details to help you. – Edo Oct 29 '20 at 14:14
  • I will update my question shortly – John Oct 29 '20 at 14:22
  • I added brief description what I'm doing to add this palette – John Oct 29 '20 at 15:07

1 Answers1

0

The process for saving data within R packages is well described here: https://r-pkgs.org/data.html

You might prefer to use the Internal data approach, unless you want other users of your package to have access to the color palette.

Through the usethis::use_data(), you add an object in your Global environment to the data folder, or into R/sysdata.rda if you set internal = TRUE. When the package is loaded (e.g. devtools::load_all()) these data objects are loaded into the package environment.

The error you mention - Error: Unknown colour name: my_palette - suggests a problem in the way you've generated your color palette. It would be helpful to provide the code that generates your color palette to check this.

An example:

# define color palette
color_palette <- c("#c6d4e1", "#2f2016", "#fcfaea", "#456789")

# save to internal package data - saves data in R/sysdata.rda
usethis::use_data(color_palette, internal = TRUE)

# load package
devtools::load_all()

# refer to color palette
mypackage::color_palette[1]     # "#c6d4e"
Jay Achar
  • 1,156
  • 9
  • 16
  • Yes, I did completely the same as you did and I get same error so I believe you are right that the problem is in generating color palette. Please look at my answer to see how my color palette is generated. – John Oct 30 '20 at 09:02
  • @John - the fact that my color palette above is a vector isn't so important - the color palette simply needs to be an object (e.g. vector, list, data frame) that can be passed to `usethis::use_data()`. To better understand the error, can you show the structure of `my_package::x` from your original question, or the code you've used to generate my_palette.rds? – Jay Achar Oct 30 '20 at 09:32