/pkg/R/tableChange.R

https://code.google.com/ · R · 114 lines · 68 code · 20 blank · 26 comment · 13 complexity · 890896eefe85ba37cc52e46293d7f1fe MD5 · raw file

  1. #' Change a \link{tabplot-object}
  2. #'
  3. #' Change the order of columns, flip, and change the palettes of a \link{tabplot-object}.
  4. #'
  5. #' @aliases tableChange
  6. #' @param tab \link{tabplot-object}
  7. #' @param colNames vector of names of the desired columns
  8. #' @param flip logical, if TRUE then the plot is flipped vertically, i.e.\ the row bins are reversed
  9. #' @param pals list of color palettes. Each list item is on of the following:
  10. #' \itemize{
  11. #' \item a palette name in \code{\link{tablePalettes}}, optionally with the starting color between brackets.
  12. #' \item a palette vector
  13. #' }
  14. #' @param colorNA color for missing values
  15. #' @param numPals name(s) of the palette(s) that is(are) used for numeric variables ("Blues", "Greys", or "Greens"). Recycled if necessary.
  16. #' @return \link{tabplot-object}
  17. #' @export
  18. #' @example ../examples/tableChange.R
  19. tableChange <- function(tab, colNames=sapply(tab$columns, function(col)col$name), flip=FALSE, pals=list(), colorNA = NULL, numPals = NULL) {
  20. ## change order of columns
  21. currentColNames <- sapply(tab$columns, function(col)col$name)
  22. colID <- match(colNames, currentColNames)
  23. ## check if each column in colNames exist in tab
  24. if (any(is.na(colID))) stop(paste("Column(s) ", paste(colNames[is.na(colID)], collapse=", "), " does(do) not exist." , sep=""))
  25. tab2 <- list(dataset=tab$dataset,
  26. n=length(colNames),
  27. nBins=tab$nBins,
  28. binSizes=tab$binSizes,
  29. isNumber=tab$isNumber[colID],
  30. rows=tab$rows,
  31. columns=lapply(colID, function(id) tab$column[[id]])
  32. )
  33. ## flip tabplot
  34. if (flip) {
  35. tab2$rows$heights <- rev(tab$rows$heights)
  36. tab2$rows$heights <- -(tab$binSizes/tab$rows$m)
  37. tab2$rows$y <- 1- c(0,cumsum(tab$binSizes/tab$rows$m)[-tab$nBins])
  38. tab2$rows$marks <- rev(tab$rows$marks)
  39. flipCol <- function(col) {
  40. col$sort <- ifelse(col$sort=="", "", ifelse(col$sort=="decreasing", "increasing", "decreasing"))
  41. if (col$isnumeric) {
  42. col$mean <- rev(col$mean)
  43. col$compl <- rev(col$compl)
  44. col$lower <- rev(col$lower)
  45. col$upper <- rev(col$upper)
  46. col$mean.scaled <- rev(col$mean.scaled)
  47. col$mean.brokenX <- rev(col$mean.brokenX)
  48. col$widths <- rev(col$widths)
  49. } else {
  50. col$freq <- col$freq[nrow(col$freq):1,]
  51. col$x <- col$x[nrow(col$x):1,]
  52. col$widths <- col$widths[nrow(col$widths):1,]
  53. }
  54. return(col)
  55. }
  56. tab2$columns <- lapply(tab2$columns, flipCol)
  57. }
  58. ## change palettes
  59. if (length(pals)!=0) {
  60. pals <- tableplot_checkPals(pals)
  61. whichCategorical <- which(sapply(tab2$columns, FUN=function(col)!col$isnumeric))
  62. paletNr <- 1
  63. for (i in whichCategorical) {
  64. tab2$columns[[i]]$paletname <- pals$name[paletNr]
  65. tab2$columns[[i]]$palet <- pals$palette[[paletNr]]
  66. paletNr <- ifelse(paletNr==length(pals$name), 1, paletNr + 1)
  67. }
  68. }
  69. ## change colorNA
  70. if (!is.null(colorNA)) {
  71. ## Check colorNA
  72. if (class(try(col2rgb(colorNA), silent=TRUE))=="try-error") {
  73. stop("<colorNA> is not correct")
  74. }
  75. whichCategorical <- which(sapply(tab2$columns, FUN=function(col)!col$isnumeric))
  76. for (i in whichCategorical) {
  77. tab2$columns[[i]]$colorNA <- colorNA
  78. }
  79. }
  80. ## change numeric palettes
  81. if (!is.null(numPals)) {
  82. ## Check numPals
  83. if ((class(numPals)!="character") || !all(numPals %in% c("Blues", "Greens", "Greys"))) stop("<numPals> is not correct")
  84. whichNumeric <- which(sapply(tab2$columns, FUN=function(col)col$isnumeric))
  85. numPals <- rep(numPals, length.out=length(whichNumeric))
  86. paletNr <- 1
  87. for (i in whichNumeric) {
  88. tab2$columns[[i]]$paletname <- numPals[paletNr]
  89. paletNr <- paletNr + 1
  90. }
  91. }
  92. class(tab2) <- "tabplot"
  93. return(tab2)
  94. }