# NOT RUN {
s <- rast(system.file("ex/logo.tif", package="terra"))
ss <- s[[2:1]]
fvi <- function(x, y){ (x - y ) / (x + y) }
x <- lapp(ss, fun=fvi )
# which is the same as supplying the layers to "fun"
# in some cases this will be much faster
y <- fvi(s[[2]], s[[1]])
f2 <- function(x, y, z){ (z - y + 1) / (x + y + 1) }
p1 <- lapp(s, fun=f2 )
p2 <- lapp(s[[1:2]], f2, z=200)
# the usenames argument
fvi2 <- function(red, green){ (red - green ) / (red + green) }
names(s)
x1 <- lapp(s[[1:2]], fvi2, usenames=TRUE)
x2 <- lapp(s[[2:1]], fvi2, usenames=TRUE)
# x2 is different from x1 as the order of the layers has changed
# but x3 and x4 are the same as x1
x3 <- lapp(s[[2:1]], fvi2, usenames=FALSE)
x4 <- lapp(s, fvi2, usenames=TRUE)
# while this would give an error because
# there are too many layers in s
# x5 <- lapp(s, fvi2, usenames=FALSE)
pairs(c(x1, x2, x3, x4))
## SpatDataSet
x <- sds(s, s[[1]]+50)
lapp(x, function(x, y) x/y, recycle=TRUE)
# }
Run the code above in your browser using DataLab