@@ -498,70 +498,70 @@ setMethod('typeof', signature(x = "FileArray"), function(x){
498498# ' @param ... optional arguments to \code{FUN}
499499# ' @param simplify a logical indicating whether results should be simplified if possible
500500# ' @return See Section 'Value' in \code{\link[base]{apply}};
501- # ' @noRd
502- # setGeneric("apply")
501+ # ' @export
502+ setGeneric ("apply ")
503503
504504# ' @rdname apply
505- # ' @noRd
506- # setMethod(
507- # 'apply', signature(X = "FileArray"),
508- # function(X, MARGIN, FUN, ..., simplify = TRUE){
509- # if(!X$valid()){
510- # stop("Invalid file array")
511- # }
512- # dim <- X$dimension()
513- #
514- # FUN <- match.fun(FUN)
515- # simplify <- isTRUE(simplify)
516- # d <- dim(X)
517- # dl <- length(d)
518- # dn <- dimnames(X)
519- # ds <- seq_len(dl)
520- # if (is.character(MARGIN)) {
521- # dnn <- names(dn)
522- # if (is.null(dnn))
523- # stop("'X' must have named dimnames")
524- # MARGIN <- match(MARGIN, dnn)
525- # if (anyNA(MARGIN))
526- # stop("not all elements of 'MARGIN' are names of dimensions")
527- # }
528- # d.call <- d[-MARGIN]
529- # d.ans <- d[MARGIN]
530- # if (anyNA(d.call) || anyNA(d.ans)) {
531- # stop("'MARGIN' does not match dim(X)")
532- # }
533- # s.call <- ds[-MARGIN]
534- # s.ans <- ds[MARGIN]
535- # if(length(s.ans) != 1){
536- # stop("`apply` on FileArray margin size can only be 1.")
537- # }
538- # dn.call <- dn[-MARGIN]
539- # dn.ans <- dn[MARGIN]
540- # d2 <- prod(d.ans)
541- # if (d2 == 0L) {
542- # newX <- array(vector(typeof(X), 1L),
543- # dim = c(prod(d.call), 1L))
544- # if (length(d.call) < 2L) {
545- # tmp <- newX[, 1]
546- # } else {
547- # tmp <- array(newX[, 1L], d.call, dn.call)
548- # }
549- # ans <- forceAndCall(1, FUN, tmp, ...)
550- # if(is.null(ans)){
551- # return(ans)
552- # } else if (length(d.ans) < 2L) {
553- # return(ans[1L][-1L])
554- # } else {
555- # return(array(ans, d.ans, dn.ans))
556- # }
557- # }
558- #
559- # tmp <- rep("", dl)
560- # tmp[[s.ans]] <- ".__i__."
561- # f <- sprintf("function(.__i__., ...){ FUN(X[%s], ...) }", paste(tmp, collapse = ","))
562- # f <- eval(parse(text = f))
563- #
564- # sapply(seq_len(d[[s.ans]]), f, ..., simplify = simplify)
565- # }
566- # )
505+ # ' @export
506+ setMethod(
507+ ' apply' , signature(X = " FileArray" ),
508+ function (X , MARGIN , FUN , ... , simplify = TRUE ){
509+ if (! X $ valid()){
510+ stop(" Invalid file array" )
511+ }
512+ dim <- X $ dimension()
513+
514+ FUN <- match.fun(FUN )
515+ simplify <- isTRUE(simplify )
516+ d <- dim(X )
517+ dl <- length(d )
518+ dn <- dimnames(X )
519+ ds <- seq_len(dl )
520+ if (is.character(MARGIN )) {
521+ dnn <- names(dn )
522+ if (is.null(dnn ))
523+ stop(" 'X' must have named dimnames" )
524+ MARGIN <- match(MARGIN , dnn )
525+ if (anyNA(MARGIN ))
526+ stop(" not all elements of 'MARGIN' are names of dimensions" )
527+ }
528+ d.call <- d [- MARGIN ]
529+ d.ans <- d [MARGIN ]
530+ if (anyNA(d.call ) || anyNA(d.ans )) {
531+ stop(" 'MARGIN' does not match dim(X)" )
532+ }
533+ s.call <- ds [- MARGIN ]
534+ s.ans <- ds [MARGIN ]
535+ if (length(s.ans ) != 1 ){
536+ stop(" `apply` on FileArray margin size can only be 1." )
537+ }
538+ dn.call <- dn [- MARGIN ]
539+ dn.ans <- dn [MARGIN ]
540+ d2 <- prod(d.ans )
541+ if (d2 == 0L ) {
542+ newX <- array (vector(typeof(X ), 1L ),
543+ dim = c(prod(d.call ), 1L ))
544+ if (length(d.call ) < 2L ) {
545+ tmp <- newX [, 1 ]
546+ } else {
547+ tmp <- array (newX [, 1L ], d.call , dn.call )
548+ }
549+ ans <- forceAndCall(1 , FUN , tmp , ... )
550+ if (is.null(ans )){
551+ return (ans )
552+ } else if (length(d.ans ) < 2L ) {
553+ return (ans [1L ][- 1L ])
554+ } else {
555+ return (array (ans , d.ans , dn.ans ))
556+ }
557+ }
558+
559+ tmp <- rep(" " , dl )
560+ tmp [[s.ans ]] <- " .__i__."
561+ f <- sprintf(" function(.__i__., ...){ FUN(X[%s], ...) }" , paste(tmp , collapse = " ," ))
562+ f <- eval(parse(text = f ))
563+
564+ sapply(seq_len(d [[s.ans ]]), f , ... , simplify = simplify )
565+ }
566+ )
567567
0 commit comments