/pkg/R/tableplot_checkPals.R

https://code.google.com/ · R · 49 lines · 31 code · 10 blank · 8 comment · 9 complexity · c951f4b7c460565d7588f6285211b084 MD5 · raw file

  1. #' Function to check the tableplot argument: pals
  2. #'
  3. #' @aliases tableplot_checkPals
  4. #' @param pals pals
  5. #' @return object with palette information
  6. #' @export
  7. tableplot_checkPals <- function(pals) {
  8. if (class(pals)!="list") stop("<pals> is not a list")
  9. #tabplotPalettes <- NULL; rm(tabplotPalettes); #trick R CMD check
  10. #data("tabplotPalettes")
  11. palNames <- names(tabplotPalettes$qual)
  12. palLengths <- nchar(palNames)
  13. getPal <- function(palName, startCol=1) {
  14. originalPal <- tabplotPalettes$qual[[palName]]
  15. pal <- originalPal[startCol:length(originalPal)]
  16. if (startCol!=1) pal <- c(pal, originalPal[1:(startCol-1)])
  17. palList <- list(palette=pal, name=paste(palName, "(", startCol, ")", sep=""))
  18. return(palList)
  19. }
  20. palList <- lapply(pals, FUN=function(x, palN, palL){
  21. if (class(x)=="character" && length(x)==1) {
  22. checkPals <- mapply(palN, palL, FUN=function(palN, palL, x)substr(x, 1, palL)==palN, MoreArgs=list(x))
  23. if (sum(checkPals)==1) {
  24. whichPal <- which(checkPals)
  25. maxCol <- length(tabplotPalettes$qual[[whichPal]])
  26. startCol <- as.integer(substr(x, palL[whichPal]+2, nchar(x)-1))
  27. if (is.na(startCol) || startCol<1 || startCol>maxCol) startCol <- 1
  28. return(getPal(palN[whichPal], startCol))
  29. }
  30. }
  31. if (class(try(col2rgb(x), silent=TRUE))=="try-error") {
  32. stop("<pals> color palette(s) are not correct")
  33. }
  34. return(list(palette=x, name="custom"))
  35. }, palNames, palLengths)
  36. palN <- sapply(palList, FUN=function(x)x$name)
  37. palP <- lapply(palList, FUN=function(x)x$palette)
  38. return(list(name=palN, palette=palP))
  39. }