2424# '
2525# ' @export
2626# '
27- # ' @examples \dontrun{click_pixels(3, 4, 4, TRUE)}
27+ # ' @examples \dontrun{
28+ # ' # Create a 16 x 16 pixel matrix with 3 possible states to cycle through
29+ # ' click_pixels(n_rows = 16, n_cols = 16, n_states = 3, grid = TRUE)
30+ # ' }
2831click_pixels <- function (
29- n_rows = 8L ,
30- n_cols = 16L ,
32+ n_rows = 8L ,
33+ n_cols = 16L ,
3134 n_states = 2L ,
32- grid = TRUE
35+ grid = TRUE
3336) {
3437
35- if (! is.numeric(c(n_rows , n_cols , n_states ))) {
36- stop(" Arguments 'n_rows', 'n_cols' and 'n_states' must be integer values." )
38+ if (! is.matrix(m ) | ! is.integer(m )) {
39+ stop(
40+ " Argument 'm' must be a matrix object composed of integers." ,
41+ call. = FALSE
42+ )
3743 }
3844
3945 if (! is.logical(grid )) {
40- stop(" Argument 'grid' must be TRUE or FALSE." )
46+ stop(" Argument 'grid' must be TRUE or FALSE." , call. = FALSE )
4147 }
4248
4349 n_rows <- as.integer(n_rows )
@@ -52,6 +58,89 @@ click_pixels <- function(
5258 .add_grid(m )
5359 }
5460
61+ message(" Click squares in the plot window. Press <Esc> to end." )
62+
63+ .repeat_loop(m , n_states , grid )
64+
65+ }
66+
67+ # ' Edit 'Pixels' in an Interactive Plot
68+ # '
69+ # ' Opens an interactive plotting canvas with a grid of clickable squares
70+ # ' ('pixels') that represent the cells of a matrix provided by the user,
71+ # ' ideally the output from \code{\link{click_pixels}}.
72+ # '
73+ # ' @param m A matrix of integers. The maximum value is assumed to be the number
74+ # ' of pixel states desired. Override by supplying a 'n_states' value larger
75+ # ' than the maximum in the matrix.
76+ # ' @param n_states Integer. The number of states that a pixel can be. Click a
77+ # ' pixel to cycle through the states. Numeric values are coerced to integer.
78+ # ' See details.
79+ # ' @param grid Logical. Should a boundary line be placed around the pixels to
80+ # ' make them easier to differentiate? Defaults to TRUE.
81+ # '
82+ # ' @details Click the pixels in the plotting window repeatedly to cycle through
83+ # ' a number of 'states'. Successive clicks increase the state value by 1
84+ # ' (wrapping back to 0, the default when the canvas is first plotted) and
85+ # ' make the pixel darker grey in colour. Press the ESCAPE key to exit the
86+ # ' mode and be returned a matrix that contains the state values of each
87+ # ' pixel.
88+ # '
89+ # ' @return A matrix.
90+ # '
91+ # ' @export
92+ # '
93+ # ' @examples \dontrun{
94+ # ' # Create a 3 x 4 pixel matrix with 3 possible states to cycle through
95+ # ' mat <- click_pixels(n_rows = 3, n_cols = 4, n_states = 3)
96+ # '
97+ # ' # Update the original matrix, allow for an extra state
98+ # ' mat_udpated <- edit_pixels(m = mat, n_states = 4)
99+ # ' }
100+ edit_pixels <- function (m , n_states = NULL , grid = TRUE ) {
101+
102+ if (! is.matrix(m ) | ! is.integer(m )) {
103+ stop(
104+ " Argument 'm' must be a matrix object composed of integers." ,
105+ call. = FALSE
106+ )
107+ }
108+
109+ if (! is.null(n_states )) {
110+ if (! is.numeric(n_states )) {
111+ stop(
112+ " Argument 'n_states' must be an integer value or NULL." ,
113+ call. = FALSE
114+ )
115+ }
116+ }
117+
118+ if (! is.null(n_states ) && n_states < max(m + 1L )) {
119+ stop(
120+ " The number of states, 'n_states', can't be less than " ,
121+ " the maximum value in the provided matrix, 'm'." ,
122+ call. = FALSE
123+ )
124+ }
125+
126+ if (! is.logical(grid )) {
127+ stop(" Argument 'grid' must be TRUE or FALSE." , call. = FALSE )
128+ }
129+
130+ if (is.null(n_states )) {
131+ n_states <- max(m ) + 1L
132+ } else if (is.null(n_states )) {
133+ n_states <- as.integer(n_states )
134+ }
135+
136+ .plot_canvas(m , n_states )
137+
138+ if (grid ) {
139+ .add_grid(m )
140+ }
141+
142+ message(" Click squares in the plot window. Press <Esc> to end." )
143+
55144 .repeat_loop(m , n_states , grid )
56145
57146}
0 commit comments