knitr::opts_chunk$set(comment=NA)
Iterate lists getting a window argument list to your callback.
[octostep]
[mergeLists]
octostep::octostep(x, func, # x and func are required pad=1L, use.names=TRUE, transform.previous=FALSE)
x List requiredfunc Function with arity 2L * pad + 1L requiredpad Integer controlling the number of items to be padded around (on each side of) the current item, must be within 1L:((length(x) - 1L) / 2L) for the ordinary use case with length(x) >= 2L * pad + 1L, otherwise 1L optionaluse.names Copy names? optionaltransform.previous Should the previous arguments to func hold the values of previous callbacks rather than the plain values of the initial input list? optionalList
# see arguments evolve octo <- octostep::octostep(as.list(1L:3L), function(pre, cur, nxt) { c(pre=if (is.null(pre)) NA else pre, cur=cur, nxt=if (is.null(nxt)) NA else nxt) }) print(octo)
# increased padding paddle <- octostep::octostep(as.list(1L:5L), function(pre1, pre2, cur, nxt1, nxt2) { c(pre1=if (is.null(pre1)) NA else pre1, pre2=if (is.null(pre2)) NA else pre2, cur=cur, nxt1=if (is.null(nxt1)) NA else nxt1, nxt2=if (is.null(nxt2)) NA else nxt2) }, pad=2L) print(paddle)
# cool input fable <- list(x=4L, y=1L, z=9L, a=0L, b=0L) # transform previous items while iterating transformer <- octostep::octostep(fable, function(pre, cur, nxt) { if (!octostep::any.null(pre, nxt)) sum(pre, cur, nxt) else cur }, pad=1L, use.names=TRUE, transform.previous=TRUE) # transformers print(transformer)
octostep::mergeLists(x, func, # x and func are required which.names=NULL, from=c('left', 'right')[1], allow.ragged=FALSE, warn=if (allow.ragged) TRUE else FALSE)
x List of lists requiredfunc Function with arity length(x) requiredwhich.names Integer index of the list in x from which to copy names, default NULL indicates not to copy any names optionalfrom Whether to reduce from left or right, defaults to left, alternative right optionalallow.ragged Whether to allow input lists of unequal length, defaults to FALSE optionalwarn Logical indicating whether to signal a warning if the name vector specified by which.names does not have the same length as the longest list of x optionalList
# new input listoflists <- list(list('A', 'B', 'C'), list(1L, 2L, 3L)) # allows reducing from left ... octostep::mergeLists(listoflists, function(a, b) { paste0(a, as.character(b)) }, from='left')
# or from right ... octostep::mergeLists(listoflists, function(a, b) { paste0(a, as.character(b)) }, from='right')
# and even lists of unequal length octostep::mergeLists(list(list(), list(1L)), function(a, b) { if (is.null(a)) b else a }, allow.ragged=TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.