Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using pixel_metrics() within a function does not work? #781

Open
zoeschindler opened this issue Sep 20, 2024 · 1 comment
Open

Using pixel_metrics() within a function does not work? #781

zoeschindler opened this issue Sep 20, 2024 · 1 comment
Assignees
Labels
Enhancement Not actually a bug but a possible improvement

Comments

@zoeschindler
Copy link

zoeschindler commented Sep 20, 2024

Hello, I am not sure whether I found a bug, a feature or if I wrote bad code, but I have some code which is not working like I expected. I have an example here:

# load file
las <- readLAS(system.file("extdata", "Megaplot.laz", package="lidR"))

# set options
val <- 3

# get metrics without function
rast <- pixel_metrics(las, length(X) > val, res = 1)
# -> works

# get metrics with function
dummy <- function(las, minimum) {
  rast <- pixel_metrics(las, length(X) > minimum, res = 1)
}
dummy(las = las, minimum = val)
# -> does not work

When I use pixel_metrics inside a function, it does not work. I get an error that the variable "minimum" wasn't found. I get this traceback:

Fehler: Objekt 'minimum' nicht gefunden
10. `[.data.table`(las@data, , if (!anyNA(.BY)) length(X) > minimum, by = .(cells, echo)) at <text>#1
9. las@data[, if (!anyNA(.BY)) length(X) > minimum, by = .(cells, echo)] at <text>#1
8. eval(parse(text = cmd))
7. eval(parse(text = cmd))
6. metrics_classic(las, call, grp, filter)
5. template_metrics.LAS(las, func, template, ...)
4. template_metrics(las, func, template, ...)
3. pixel_metrics.LAS(las, length(X) > minimum, res = 1)
2. pixel_metrics(las, length(X) > minimum, res = 1)
1. dummy(las = las, minimum = val)

I am happy about any help!

@Jean-Romain
Copy link
Collaborator

This is an issue of scope. It searches X and minimum in the data.frame. minimum not being in in the data.frame it searched in the global environment. It works like that since the beginning. I don't know how to enhance it.

@Jean-Romain Jean-Romain self-assigned this Sep 24, 2024
@Jean-Romain Jean-Romain added the Enhancement Not actually a bug but a possible improvement label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Not actually a bug but a possible improvement
Projects
None yet
Development

No branches or pull requests

2 participants