/
repgrid-constructs.r
452 lines (385 loc) · 13.4 KB
/
repgrid-constructs.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#----------------------------------------------#
### basic construct operations ###
#----------------------------------------------#
# Function that start with c. operate on the constructs only.
# These functions serve for basic operations on constructs.
# In case a function needs to operate on constructs and other
# slots (e.g. elements, ratings) higher-level functions
# that perform joints operations are used. The base operations
# are not needed when using openrepgrid by the user.
## constructs
# add constructs
# delete constructs
# rename pole(s)
# change construct order
# set pole status (emerged, preferred etc.)
# reverse poles
# pole--+
# +--name
# +--preferred
# +--emerged
############## FUNCTIONS TO RETRIEVE INFORMATION FROM REPGRID OBJECTS ##################
#' Get construct names
#'
#' @param x `repgrid` object.
#' @section Deprecated functions: `getConstructNames()`,
#' and `cNames()` have been deprecated.
#' Instead use `constructs()`.
#' @export
#' @keywords internal
#'
getConstructNames <- function(x) {
if (!inherits(x, "repgrid")) { # check if x is repgrid object
stop("Object x must be of class 'repgrid'")
}
l <- lapply(x@constructs, function(x) {
data.frame(
leftpole = x$leftpole$name,
rightpole = x$rightpole$name, stringsAsFactors = FALSE
)
})
list_to_dataframe(l)
}
cNames <- getConstructNames
#' Retrieves the construct names from a `repgrid`.
#'
#' Different features like trimming, indexing and choices of separators
#' allow to return the kind of format that is needed.
#'
#' @title Retrieve construct names in needed format.
#'
#' @param x `repgrid` object.
#' @param mode Type of output. 1= left and right pole
#' separated by `sep`), 2= only left pole,
#' 3 = only right pole.
#' @param trim Number of characters to trim the construct names to
#' (default `NA`). `NA` will suppress trimming.
#' The length of `index` is not included in the
#' trimming.
#' @param index Logical. Whether to add a index number before the construct
#' names (default `TRUE`).
#' @param sep Separator string between poles.
#' @param pre String before index number (default `(`).
#' @param post String after index number (default `) `).
#' @return Vector with construct names.
#' @export
#' @keywords internal
#' @examples \dontrun{
#'
#' getConstructNames2(bell2010)
#' getConstructNames2(bell2010, mode = 2)
#' getConstructNames2(bell2010, index = T)
#' getConstructNames2(bell2010, index = T, mode = 3)
#' }
#'
getConstructNames2 <- function(x, mode = 1, trim = 20, index = F,
sep = " - ", pre = "(", post = ") ") {
if (!inherits(x, "repgrid")) {
stop("Object x must be of class 'repgrid'")
}
cnames <- constructs(x)
cnames.l <- cnames[, 1]
cnames.r <- cnames[, 2]
# add numeric index in front of constructs
if (index) {
ind <- paste(pre, seq_along(cnames.l), post, sep = "")
} else {
ind <- ""
}
# trim names if prompted
if (!is.na(trim)) {
if (mode == 1) { # adjust trimming length if both sides are included
trim <- ceiling(trim / 2)
}
cnames.l <- substr(cnames.l, 1, trim)
cnames.r <- substr(cnames.r, 1, trim)
}
if (mode == 1) { # left and right poles
cnames.new <- paste(ind, cnames.l, sep, cnames.r, sep = "")
}
if (mode == 2) { # left pole only
cnames.new <- paste(ind, cnames.l, sep = "")
}
if (mode == 3) { # right pole only
cnames.new <- paste(ind, cnames.r, sep = "")
}
cnames.new
}
#' Get or replace construct poles
#'
#' Allows to get and set construct poles.
#' Replaces the older functions `getConstructNames`, `getConstructNames2`,
#' and `eNames` which are deprecated.
#'
#' @param x A repgrid object.
#' @param i,j Row and column Index of repgrid matrix.
#' @param value Character vector of poles.
#' @param collapse Return vector with both poles instead.
#' @param sep Separator if `collapse = TRUE`, default is `" - "`.
#' @rdname constructs
#' @export
#'
#' @examples
#'
#' # shorten object name
#' x <- boeker
#'
#' ## get construct poles
#' constructs(x) # both left and right poles
#' leftpoles(x) # left poles only
#' rightpoles(x)
#' constructs(x, collapse = TRUE)
#'
#' ## replace construct poles
#' constructs(x)[1, 1] <- "left pole 1"
#' constructs(x)[1, "leftpole"] <- "left pole 1" # alternative
#' constructs(x)[1:3, 2] <- paste("right pole", 1:3)
#' constructs(x)[1:3, "rightpole"] <- paste("right pole", 1:3) # alternative
#' constructs(x)[4, 1:2] <- c("left pole 4", "right pole 4")
#'
#' l <- leftpoles(x)
#' leftpoles(x) <- sample(l) # brind poles into random order
#' leftpoles(x)[1] <- "new left pole 1" # replace name of first left pole
#'
#' # replace left poles of constructs 1 and 3
#' leftpoles(x)[c(1, 3)] <- c("new left pole 1", "new left pole 3")
#'
constructs <- function(x, collapse = FALSE, sep = " - ") {
# check if x is a repgrid object
if (!inherits(x, "repgrid")) {
stop("Object x must be of class 'repgrid'.")
}
# get left and right pole name properties from each construct
df <- data.frame(
leftpole = leftpoles(x),
rightpole = rightpoles(x),
stringsAsFactors = FALSE
)
if (collapse) {
res <- paste0(df$leftpole, sep, df$rightpole)
return(res)
}
df
}
#' @rdname constructs
#' @export
`constructs<-` <- function(x, i, j, value) {
# check if x is a repgrid object
if (!inherits(x, "repgrid")) {
stop("Object x must be of class 'repgrid'.")
}
# get dataframe and replace using bracket operator
# this replacement type function somehow works.
# Other attempts falied.
d <- constructs(x)
d[i, j] <- value
# replace left and right poles
leftpoles(x) <- d$leftpole
rightpoles(x) <- d$rightpole
x
}
#' @rdname constructs
#' @export
leftpoles <- function(x) {
# check if x is a repgrid object
if (!inherits(x, "repgrid")) {
stop("Object x must be of class 'repgrid'.")
}
# get left pole name property from each construct
sapply(x@constructs, function(x) x$leftpole$name)
}
#' @param position Index where to insert construct
#' @param value Character vector of construct poles names.
#' @rdname constructs
#' @export
`leftpoles<-` <- function(x, position, value) {
# check if x is a repgrid object
if (!inherits(x, "repgrid")) {
stop("Object x must be of class 'repgrid'.")
}
# get element names and replace one or more
p <- leftpoles(x)
p[position] <- value
# replace leftpole name property of each construct
for (i in seq_along(p)) {
x@constructs[[i]]$leftpole$name <- p[i]
}
x
}
#' @rdname constructs
#' @export
rightpoles <- function(x) {
# check if x is a repgrid object
if (!inherits(x, "repgrid")) {
stop("Object x must be of class 'repgrid'.")
}
# get left pole name property from each construct
sapply(x@constructs, function(x) x$rightpole$name)
}
#' @rdname constructs
#' @export
`rightpoles<-` <- function(x, position, value) {
# check if x is a repgrid object
if (!inherits(x, "repgrid")) {
stop("Object x must be of class 'repgrid'.")
}
# get element names and replace one or more
p <- rightpoles(x)
p[position] <- value
# replace leftpole name property of each construct
for (i in seq_along(p)) {
x@constructs[[i]]$rightpole$name <- p[i]
}
x
}
constructInfo <- function(x, all = TRUE) {
if (!inherits(x, "repgrid")) { # check if x is repgrid object
stop("Object x must be of class 'repgrid'.")
}
l <- lapply(x@constructs, function(x) {
data.frame(
leftpole = x$leftpole$name,
l.preffered = x$leftpole$preferred,
l.emerged = x$leftpole$emerged,
rightpole = x$rightpole$name,
r.preffered = x$rightpole$preferred,
r.emerged = x$rightpole$emerged,
stringsAsFactors = FALSE
)
})
info <- list_to_dataframe(l) # old version using lapply above
if (all) {
info
} else {
info[c("leftpole", "rightpole")]
}
}
cInfo <- constructInfo
# constructInfo(x)
getNoOfConstructs <- function(x) {
if (!inherits(x, "repgrid")) { # check if x is repgrid object
stop("Object x must be of class 'repgrid'.")
}
length(x@constructs)
}
nc <- getNoOfConstructs
# internal. c_makeNewConstruct is the constructor for construct object (simple list)
c_makeNewConstruct <- function(x = NULL, l.name = NA, l.preferred = NA, l.emerged = NA,
r.name = NA, r.preferred = NA, r.emerged = NA, ...) {
list(
leftpole = list(
name = l.name,
preferred = l.preferred,
emerged = l.emerged
),
rightpole = list(
name = r.name,
preferred = r.preferred,
emerged = r.emerged
)
)
}
# str(c_makeNewConstruct())
# internal: c_setConstructs sets constructs by index
c_setConstructs <- function(x, l.name = NA, l.preferred = NA, l.emerged = NA,
r.name = NA, r.preferred = NA, r.emerged = NA,
index = NULL, ...) {
if (!inherits(x, "repgrid")) { # check if x is repgrid object
stop("Object x must be of class 'repgrid'.")
}
if (!is.atomic(l.name) | !is.atomic(l.name)) {
stop("arguments l.name and r.name must be a vector")
}
if (!is.logical(c(l.preferred, l.emerged, r.preferred, r.emerged))) {
stop("arguments l.preferred, r.preferred, l.emerged and r.emerged must be a vector")
}
if (is.null(index)) {
index <- seq_len(max(c(length(l.name)), c(length(r.name))))
}
if (!(is.na(l.name[1]) & is.na(r.name[1]))) {
newConstructs <- mapply(c_makeNewConstruct, NA,
l.name = l.name, l.preferred = l.preferred, l.emerged = l.emerged,
r.name = r.name, r.preferred = r.preferred, r.emerged = r.emerged,
SIMPLIFY = FALSE
) # generate new construct list
x@constructs[index] <- newConstructs
}
x
}
# x <- makeEmptyRepgrid()
# x <- c_setConstructs(x, l.name=c("construct left 1", "construct left 2"))
# x <- c_setConstructs(x, l.n=c("construct left 3", "construct left 4"), index=3:4)
# str(x@constructs, m=3)
# internal: c_addConstruct adds constructs at the bottom
c_addConstruct <- function(x, l.name = NA, l.preferred = NA, l.emerged = NA,
r.name = NA, r.preferred = NA, r.emerged = NA,
position = NA, side = "pre") {
if (!inherits(x, "repgrid")) { # check if x is repgrid object
stop("Object x must be of class 'repgrid'.")
}
if (!is.numeric(position) & !(length(position) == 1 & is.na(position[1]))) {
stop("position must be numeric.")
}
if (position < 0 | position > length(x@constructs) + 1) {
stop("USERINFO: position must be between 1 and number of constructs plus 1.")
}
if (length(position) == 1 & is.na(position[1])) position <- length(x@constructs) + 1
constructs.old <- x@constructs
constructs.new <- mapply(c_makeNewConstruct, NA, # generate construct list
l.name = l.name, l.preferred = l.preferred, l.emerged = l.emerged,
r.name = r.name, r.preferred = r.preferred, r.emerged = r.emerged,
SIMPLIFY = FALSE
)
index <- insertAt(seq_along(x@constructs), position, side = side)
x@constructs[index$index.base.new] <- constructs.old[index$index.base]
x@constructs[index$index.insert.new] <- constructs.new
x
}
# x <- makeEmptyRepgrid()
# x <- c.setConstruct(x, l.name=c("construct left 1"))
# x <- c_addConstruct(x, l.name="construct added at the end", r.name="test", pos=1)
# x <- c_addConstructs(x, l.name="construct left inserted at position 1", pos=1)
# x <- c_addConstructs(x, l.name="construct right inserted at position 1", pos=1)
# x <- c_addConstructs(x, l.name=c("construct 10", "element 11"), pos=10:11)
# str(x@constructs, m=3)
# internal: c_addConstructs. all elements that do not have a position specified are added at the end
c_addConstructs <- function(x, l.name = NA, l.preferred = NA, l.emerged = NA,
r.name = NA, r.preferred = NA, r.emerged = NA,
position = NA, side = "pre") {
if (!inherits(x, "repgrid")) { # check if x is repgrid object
stop("Object x must be of class 'repgrid'.")
}
if (!is.numeric(position) & !(length(position) == 1 & is.na(position[1]))) {
stop("position must be numeric.")
}
len <- max(c(length(l.name), length(r.name)))
if (length(position) == 1 & is.na(position[1])) {
position <- rep(NA, len)
}
position[is.na(position)] <- seq_along(position[is.na(position)]) + length(x@constructs)
constructs.old <- x@constructs
constructs.new <- mapply(c_makeNewConstruct, NA, # generate construct list
l.name = l.name, l.preferred = l.preferred, l.emerged = l.emerged,
r.name = r.name, r.preferred = r.preferred, r.emerged = r.emerged,
SIMPLIFY = FALSE
)
index <- insertAt(seq_along(x@constructs), position, side = side)
x@constructs[index$index.base.new] <- constructs.old[index$index.base]
x@constructs[index$index.insert.new] <- constructs.new
x
}
### NOT RUN
# x <- makeEmptyRepgrid()
# x <- c_setConstructs(x, l.name=c("construct left 1", "construct left 2"))
# x <- c_addConstructs(x, l.name="construct added at the end")
# x <- c_addConstructs(x, l.name="construct left inserted at position 1", pos=1)
# x <- c_addConstructs(x, l.name="construct right inserted at position 1", pos=1)
# x <- c_addConstructs(x, l.name=c("construct 10", "element 11"), pos=10:11)
# str(x@constructs, m=3)
### maybe unnecessary functions ###
# c.removeNullConstructs <- function(x){
# if(!inherits(x, "repgrid")) # check if x is repgrid object
# stop("Object x must be of class 'repgrid'.")
# x@constructs <- x@constructs[!sapply(x@constructs, is.null)]
# x
# }