Skip to content

Commit

Permalink
Merge pull request #11 from bschilder/main
Browse files Browse the repository at this point in the history
New function: `pals.maxcolors`
  • Loading branch information
kwstat committed Dec 29, 2023
2 parents c9feb47 + 84edc96 commit b83edd4
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: pals
Version: 1.8
Version: 1.9
Authors@R: person("Kevin","Wright", email="kw.stat@gmail.com", comment=c(ORCID = "0000-0002-0617-8673"), role=c("aut","cre","cph"))
Title: Color Palettes, Colormaps, and Tools to Evaluate Them
Description: A comprehensive collection of color palettes, colormaps, and tools to evaluate them.
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export(pal.sineramp)
export(pal.test)
export(pal.volcano)
export(pal.zcurve)
export(pals.maxcolors)
export(parula)
export(plasma)
export(polychrome)
Expand Down Expand Up @@ -181,4 +182,5 @@ importFrom(methods,as)
importFrom(stats,dist)
importFrom(stats,hclust)
importFrom(stats,runif)
importFrom(utils,getFromNamespace)
importFrom(utils,installed.packages)
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# pals 1.9

* Better warning if `pal.cube()` is called without `rgl` package installed (#10, Adam).

* New exported helper function: `pals.maxcolors()` (#5). By @bschilder
- Added unit tests.
* Bumped `pals` version from 1.8 --> 1.9 in *DESCRIPTION*.

# pals 1.8 (2023-08-20)

Expand Down
37 changes: 37 additions & 0 deletions R/pals.maxcolors.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' Pals max colors
#'
#' This function returns a data frame with the maximum number of colors for
#' each palette currently available within the \pkg{pals} package.
#' @returns A data frame with the maximum number of colors for each palette.
#' @author R code by Brian M Schilder.
#' @export
#' @importFrom utils getFromNamespace
#' @examples
#' dat <- pals.maxcolors()
pals.maxcolors <- function(){
## Copied function from rlang::is_missing
is_missing <- function (x) {
missing(x) || identical(x, quote(expr = ))
}
## Get all exported pals functions
ns <- getNamespaceExports("pals")
syspals <- utils::getFromNamespace("syspals", "pals")
## Filter out the functions that don't actually exist
syspals <- syspals[names(syspals) %in% ns]
dat <- lapply(names(syspals),
function(p){
f <- formals(utils::getFromNamespace(p,"pals"))
if(!"n" %in% names(f)) return(NA)
maxcolors <- if(is_missing(f$n)) Inf else eval(f$n)
return(
data.frame(palette=p,
maxcolors=maxcolors,
is_finite=is.finite(maxcolors),
stringsAsFactors=FALSE
)
)
}) |> Reduce(f=rbind)
## Sort by maxcolors
dat <- dat[order(dat$maxcolors),]
return(dat)
}
21 changes: 21 additions & 0 deletions man/pals.maxcolors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pals.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ LaTeX: pdfLaTeX
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageCheckArgs: --as-cran
PackageRoxygenize: rd,collate,namespace
7 changes: 7 additions & 0 deletions tests/testthat/test_pals.maxcolors.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("pals.maxcolors works", {
dat <- pals.maxcolors()
testthat::expect_true(all(
c("palette","maxcolors","is_finite") %in% names(dat)
))
testthat::expect_gte(nrow(dat), 127)
})

0 comments on commit b83edd4

Please sign in to comment.