EmilHvitfeldt / paletteer

🎨🎨🎨 Collection of most color palettes in a single R package
https://emilhvitfeldt.github.io/paletteer/
Other
925 stars 45 forks source link

Can't pass object representations of package (or pallete?) names to paletteer_d() #13

Closed wetlandscapes closed 6 years ago

wetlandscapes commented 6 years ago

Hi, Really cool package. I appreciate what you've done here.

I'm running into a bit of trouble passing the names of packages and palettes to paletteer_d(), however.

Here's my scenario: I wanted to create a function for your package that would allow me to visualize the colors from a specific package, palette, etc. so that i could rapidly review the palettes, and see which one might best fit my needs, without having to manually entire each name. Focusing on just the fixed width discrete palettes, I made a function:

library(tidyverse)
library(paletteer)
library(pal)

color_plot_d <- function(package, name, n){
  palette <- paletteer::palettes_d_names %>%
  as_data_frame %>%
  dplyr::filter(package == package, palette == name)

  pack <- palette$package
  pal <- palette$palette
  n <- palette$length

  pals::pal.bands(
    paletteer_d(pack, pal),
    main = paste(pack, pal, sep = ": ")
  )
}

However, I get this error:

 Error in match.arg(package, unique(paletteer::palettes_d_names$package)) : 
  'arg' should be one of “awtools”, “dichromat”, “dutchmasters”, “ggsci”, “ggpomological”, “ggthemes”, “ghibli”, “grDevices”, “jcolors”, “LaCroixColoR”, “NineteenEightyR”, “nord”, “ochRe”, “palettetown”, “pals”, “Polychrome”, “quickpalette”, “rcartocolor”, “RColorBrewer”, “Redmonder”, “RSkittleBrewer”, “wesanderson”, “yarrr” 

It seems that this behavior is the results of the follwing two lines of code in paletteer_d():

package <- rlang::quo_name(rlang::enquo(package))
palette <- rlang::quo_name(rlang::enquo(palette))

It appears that I can only pass quoted or unquoted versions of the name, but not object representations of the name. I'd hoped to provide a solution to this problem, but this is a level of R I'm not yet familiar with.

Instead, my work around was to simply create a duplicate of the paletteer_d() function, but to comment out the two lines causing me problems. However, this means that if one passes an unquoted representation of a package or palette (e.g. awtools) then a different error occurs.

#The re-worked version of `paletteer_d()`, commenting out the problematic lines.
p_d <- function(package, palette, n, direction = 1, type = c("discrete", 
    "continuous")) {
  if (abs(direction) != 1) {
      stop("direction must be 1 or -1")
  }
  #package <- rlang::quo_name(rlang::enquo(package))
  #palette <- rlang::quo_name(rlang::enquo(palette))
  package <- match.arg(package, unique(paletteer::palettes_d_names$package))
  type <- match.arg(type)
  pal <- paletteer::palettes_d[[c(package, palette)]]
  if (is.null(pal)) 
      stop("Palette not found. Make sure the palette name are spelled correct.")
  if (missing(n)) {
      n <- length(pal)
  }
  if (type == "discrete" && n > length(pal)) {
      stop(paste("Number of requested colors greater than this palette can offer which is ", 
          length(pal), ".", sep = ""))
  }
  out <- switch(type, continuous = (grDevices::colorRampPalette(pal))(n), 
      discrete = pal[1:n])
  if (direction == -1) {
      rev(out)
  }
  else {
      out
  }
}

#The palette generation function I made:
color_plot_d <- function(package, name){
  palette <- paletteer::palettes_d_names %>%
  as_data_frame %>%
  dplyr::filter(package == package, palette == name)

  pack <- palette$package
  pal <- palette$palette

  pals::pal.bands(
    p_d(pack, pal),
    main = paste(pack, pal, sep = ": ")
  )
}

#These all work:
color_plot_d("awstools", "a_palette")
color_plot_d(paletteer::palettes_d_names$package[1],
  paletteer::palettes_d_names$palette[1])
pack <- paletteer::palettes_d_names$package[1]
pal <- paletteer::palettes_d_names$pal[1]
color_plot_d(pack, pal)

#This does not work:
color_plot_d(awstools, a_palette)

Not sure if this matters to you, but just thought I'd pass the information along, in case it was useful.

Again, I appreciate your effort on this package. Keep up the great work!

EmilHvitfeldt commented 6 years ago

Hello jmerc13!

Thanks for your interest, it appears that you forgot to unquote when passing pack and pal into paletteer_d.

library(tidyverse)
library(paletteer)
library(pals)

color_plot_d <- function(package, name, n){
  palette <- paletteer::palettes_d_names %>%
  as_data_frame %>%
  dplyr::filter(package == package, palette == name)

  pack <- palette$package
  pal <- palette$palette
  n <- palette$length

  pals::pal.bands(
    paletteer_d(!!pack, !!pal),
    main = paste(pack, pal, sep = ": ")
  )
}

color_plot_d(package = "awtools", name = "a_palette")

If you want to see all the palettes in this package I have compiled a visual list here: https://github.com/EmilHvitfeldt/r-color-palettes which also have them sorted by style.

wetlandscapes commented 6 years ago

Holy crap, you just blew my mind! I've never seen that syntax before. Thanks for letting me know and responding so quickly!

And your r-color-palettes page is super useful.

Cheers!

EmilHvitfeldt commented 6 years ago

you are very welcome!

further look reveals some redundency in your function. Similar functionality comes with

library(paletteer)
library(pals)

color_plot_d <- function(package, name, n){
  pals::pal.bands(
    paletteer_d(!!package, !!name, n = n),
    main = paste(package, name, sep = ": ")
  )
}

color_plot_d(package = "awtools", name = "a_palette")