Skip to content

Commit

Permalink
Add a highlight matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
coatless committed Jan 11, 2024
1 parent 10a7cef commit eafd138
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(draw_matrix)
export(gdraw_matrix)
export(highlight_matrix)
importFrom(graphics,mtext)
importFrom(graphics,par)
importFrom(graphics,plot.new)
Expand Down
59 changes: 59 additions & 0 deletions R/highlight.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Highlight a matrix
#'
#' Generate a matrix with active areas.
#'
#' @param x A matrix.
#' @param rows An interger vector with valid row index locations.
#' @param columns An integer vector containing valid column indexlocations.
#' @param points An m by 2 matrix with points listed in x, y format.
#'
#' @return
#' A logical matrix with the required rows and/or columns or points set to
#' `TRUE`. All other values are given as `FALSE`.
#'
#' @export
#' @examples
#'
#' x = matrix(1:12, nrow = 4)
#'
#' # Highlight entries only in the 1st and 3rd rows.
#' highlight_matrix(x, rows = c(1, 3))
#'
#' # Highlight entries only in the first two rows:
#' highlight_matrix(x, rows = 1:2)
#'
#' # Highlight entries in the last row
#' highlight_matrix(x, rows = nrow(x))
#'
#' # Highlight entries in the first column
#' highlight_matrix(x, columns = 1)
#'
#' # Highlight entries in the first column or first row.
#' highlight_matrix(x, rows = 1, columns = 1)
highlight_matrix <- function(x, rows = NULL, columns = NULL, points = NULL) {

# Create a logical matrix with the same dimensions as 'x'
logical_matrix <- matrix(FALSE, nrow = nrow(x), ncol = ncol(x))

# Nothing to mark.
if (is.null(rows) && is.null(columns) && is.null(points)) {
return(logical_matrix)
}

if(!is.null(points)){
stopifnot("points must contain only two columns." = ncol(points) == 2)
logical_matrix[points] <- TRUE
}

# Enable rows
if(!is.null(rows)){
logical_matrix[rows, ] <- TRUE
}

# Enable columns
if(!is.null(columns)){
logical_matrix[, columns] <- TRUE
}

logical_matrix
}
43 changes: 43 additions & 0 deletions man/highlight_matrix.Rd

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

0 comments on commit eafd138

Please sign in to comment.