rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
541 stars 90 forks source link

`subset(SpatVector, NSE = TRUE)` fails when the logical expression includes an object `x` #1600

Closed AramburuMerlos closed 1 month ago

AramburuMerlos commented 2 months ago

Maybe I should just avoid using the same column names or the NSE in these cases, but:

library(terra) # v .1.7.78
v <- vect(system.file("ex/lux.shp", package="terra"))

subset(v, NAME_1 == "Diekirch", NSE=TRUE) |> nrow()
# 5 

x <- data.frame(NAME_1 = "Diekirch")
subset(v, NAME_1 == x$NAME_1, NSE=TRUE) |> nrow()
# 12
subset(v, NAME_1 %in% x$NAME_1, NSE=TRUE) |> nrow()
# 12

# It doesn't happen with other classes
xl <- list(NAME_1 = "Diekirch")
subset(v, NAME_1 %in% xl$NAME_1, NSE=TRUE) |> nrow()
# 5
xv <- subset(v, NAME_1 == "Diekirch", NSE=TRUE)
subset(v, NAME_1 %in%  xv$NAME_1, NSE=TRUE) |> nrow()
# 5
rhijmans commented 2 months ago

The underlying problem is that you use the variable name x. That leads to a mix up because the first argument of subset is also x. With NSE you then effectively get

subset(x, x$NAME_1 == x$NAME_1)

While it works as expected with another variable name, like this

y <- data.frame(NAME_1 = "Diekirch")
subset(v, NAME_1 == y$NAME_1, NSE=TRUE) |> nrow() 
[1] 5
rhijmans commented 1 month ago

This now works for your examples; so I am hopeful it does now newly break other cases. Thank you for reporting this.