diff --git a/tests/apply.R b/tests/apply.R index f5fe660a77555c41c89b5c240c85e5c6e69f065c..84f571d1f43f68c513fd6eb1c0d16049bdd37039 100644 --- a/tests/apply.R +++ b/tests/apply.R @@ -28,6 +28,16 @@ print(apply(M,1,f1)) print(apply(M,2,f1)) print(apply(M,1,f2,101.23)) print(apply(M,2,f2,101.23)) +A <- array (1:12, c(2,2,3)) +print(A) +print(apply(A,1,f1)) +print(apply(A,2,f1)) +print(apply(A,3,f1)) +print(apply(A,c(1,3),f1)) +print(apply(A,1,f2,101.23)) +print(apply(A,2,f2,101.23)) +print(apply(A,3,f2,101.23)) +print(apply(A,c(1,3),f2,101.23)) # Check that delayed warnings refer to [[1L]] and [[3L]]. (They don't # if later calls modifiy earlier calls.) The first set of warnings are @@ -42,7 +52,10 @@ for (w in c(1,0)) { print(eapply(as.environment(list(a=-1,b=2,c=-1)),sqrt)) M <- matrix(-1,3,4) print(apply(M,1,sqrt)) + A <- array(-1,c(2,2,2)) + print(apply(A,2,sqrt)) } +warnings() # Test that indexed value is corectly retained when the applied function # returns a function that references it. @@ -65,3 +78,7 @@ fns <- apply (matrix(11:13,3,1), 1, function(x) function () x) print(fns) print(c(fns[[1]](),fns[[2]](),fns[[3]]())) +fns <- apply (array(1:8,c(2,2,2)), 1, function(x) function () x) +print(fns) +print(fns[[1]]()) +print(fns[[2]]()) diff --git a/tests/apply.Rout.save b/tests/apply.Rout.save new file mode 100644 index 0000000000000000000000000000000000000000..684ac5f97a99233a72c5a9ebadc9af93c9f85e48 --- /dev/null +++ b/tests/apply.Rout.save @@ -0,0 +1,252 @@ + +-- lapply: +\$a +[1] 4.23 + +\$b +[1] 9.23 + +\$c +[1] 17.23 + +\$a +[1] 104.23 + +\$b +[1] 109.23 + +\$c +[1] 117.23 + + +-- vapply: + a b c + 4.23 9.23 17.23 + a b c +104.23 109.23 117.23 + +-- eapply: +\$c +[1] 17.23 + +\$b +[1] 9.23 + +\$a +[1] 4.23 + +\$c +[1] 117.23 + +\$b +[1] 109.23 + +\$a +[1] 104.23 + + +-- apply: + [,1] [,2] [,3] [,4] +[1,] 1 4 7 10 +[2,] 2 5 8 11 +[3,] 3 6 9 12 +[1] 23.23 27.23 31.23 +[1] 7.23 16.23 25.23 34.23 +[1] 123.23 127.23 131.23 +[1] 107.23 116.23 125.23 134.23 +, , 1 + + [,1] [,2] +[1,] 1 3 +[2,] 2 4 + +, , 2 + + [,1] [,2] +[1,] 5 7 +[2,] 6 8 + +, , 3 + + [,1] [,2] +[1,] 9 11 +[2,] 10 12 + +[1] 37.23 43.23 +[1] 34.23 46.23 +[1] 11.23 27.23 43.23 + [,1] [,2] [,3] +[1,] 5.23 13.23 21.23 +[2,] 7.23 15.23 23.23 +[1] 137.23 143.23 +[1] 134.23 146.23 +[1] 111.23 127.23 143.23 + [,1] [,2] [,3] +[1,] 105.23 113.23 121.23 +[2,] 107.23 115.23 123.23 + +-- checking warnings: +Warning in FUN(c(-1, 2, -1)[[1L]], ...) : NaNs produced +Warning in FUN(c(-1, 2, -1)[[3L]], ...) : NaNs produced +[[1]] +[1] NaN + +[[2]] +[1] 1.414214 + +[[3]] +[1] NaN + +Warning in FUN(c(-1, 2, -1)[[1L]], ...) : NaNs produced +Warning in FUN(c(-1, 2, -1)[[3L]], ...) : NaNs produced +[1] NaN 1.414214 NaN +Warning in FUN(list(-1, 2, -1)[[1L]], ...) : NaNs produced +Warning in FUN(list(-1, 2, -1)[[3L]], ...) : NaNs produced +\$c +[1] NaN + +\$b +[1] 1.414214 + +\$a +[1] NaN + +Warning in FUN(newX[, i], ...) : NaNs produced +Warning in FUN(newX[, i], ...) : NaNs produced +Warning in FUN(newX[, i], ...) : NaNs produced + [,1] [,2] [,3] +[1,] NaN NaN NaN +[2,] NaN NaN NaN +[3,] NaN NaN NaN +[4,] NaN NaN NaN +Warning in FUN(array(newX[, i], d.call, dn.call), ...) : NaNs produced +Warning in FUN(array(newX[, i], d.call, dn.call), ...) : NaNs produced + [,1] [,2] +[1,] NaN NaN +[2,] NaN NaN +[3,] NaN NaN +[4,] NaN NaN +[[1]] +[1] NaN + +[[2]] +[1] 1.414214 + +[[3]] +[1] NaN + +[1] NaN 1.414214 NaN +\$c +[1] NaN + +\$b +[1] 1.414214 + +\$a +[1] NaN + + [,1] [,2] [,3] +[1,] NaN NaN NaN +[2,] NaN NaN NaN +[3,] NaN NaN NaN +[4,] NaN NaN NaN + [,1] [,2] +[1,] NaN NaN +[2,] NaN NaN +[3,] NaN NaN +[4,] NaN NaN +There were 11 warnings (use warnings() to see them) +Warning messages: +1: In FUN(c(-1, 2, -1)[[3L]], ...) : NaNs produced +2: In FUN(c(-1, 2, -1)[[3L]], ...) : NaNs produced +3: In FUN(c(-1, 2, -1)[[3L]], ...) : NaNs produced +4: In FUN(c(-1, 2, -1)[[3L]], ...) : NaNs produced +5: In FUN(list(-1, 2, -1)[[3L]], ...) : NaNs produced +6: In FUN(list(-1, 2, -1)[[3L]], ...) : NaNs produced +7: In FUN(newX[, i], ...) : NaNs produced +8: In FUN(newX[, i], ...) : NaNs produced +9: In FUN(newX[, i], ...) : NaNs produced +10: In FUN(array(newX[, i], d.call, dn.call), ...) : NaNs produced +11: In FUN(array(newX[, i], d.call, dn.call), ...) : NaNs produced + +-- checking function environments: +[[1]] +function () +x + + +[[2]] +function () +x + + +[[3]] +function () +x + + +[1] 13 13 13 +[[1]] +function () +x + + +[[2]] +function () +x + + +[[3]] +function () +x + + +[1] 13 13 13 +\$c +function () +x + + +\$b +function () +x + + +\$a +function () +x + + +[1] 11 11 11 +[[1]] +function () +x + + +[[2]] +function () +x + + +[[3]] +function () +x + + +[1] 13 13 13 +[[1]] +function () +x + + +[[2]] +function () +x + + + [,1] [,2] +[1,] 2 6 +[2,] 4 8 + [,1] [,2] +[1,] 2 6 +[2,] 4 8