You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wavelength subsetting fails if formula is passed as a variable #212
When I pass wavelength range as a variable, functions [ and [[ fail. If I pass the formula directly, no issue is present.
Is it a bug or I do something wrong?
suppressPackageStartupMessages(library(hyperSpec))
# Worksflu[, 1:2, min~320]
#> hyperSpec object#> 6 spectra#> 2 data columns#> 0 data points / spectrum#> wavelength: lambda/nm [numeric]#> data: (6 rows x 2 columns)#> 1. spc: I[fl]/"a.u." [matrix, array0]#> 2. filename: filename [character] rawdata/flu1.txt rawdata/flu2.txt ... rawdata/flu6.txt# Failswl_range<-min~320flu[, 1:2, wl_range]
#> Error in Re(from): non-numeric argument to function
I know this behavior does not seem logical and was surprising for me as well, but this is rooted deeply into the strangeness of the R language. min ~ 320 is an expression in R, and not a numeric range or array. If you save an expression into wl_range, it works:
The high priority label is only because this has aspects that I think we should consider in the next video call. After that, it may turn out to be low priority or high priority to be done before the 1.0 milestone - depending on our decision.
Without looking into details, I think what happens is that a formula can have its own environment attached, and thus if the envirnoment is from .GlobalEnv, min and max will evaluate to functions min() and max() (or to the values of variables min and max if defined).
Possible alternatives:
as Roman says, explicitly using an expression rather than a formula helps
we could (slowly) move to using other names (e.g. low and high as in ChemoSpec) that are less likely to exist in the user's workspace.
Iff such a variable exists, there will be maybe even more of a surprise, though.
Currently, we don't extrapolate outside the existing wavelength range of the hyperSpec object. I.e. for an object with wavelength range 500 : 1500, calling wl2i(object, 499.999) will return NA.
If we drop this behaviour, we could access lowest and highest possible wavelength with -Inf and +Inf, respectively. This would completely remove the surprises with formulas.
Since the "no extrapolation" is useful, though, this should at least be switched by a flag/parameter.
It may be possible to detect +Inf/-Inf and have the extrapolation behaviour at least for these.
Any change to the user interface here should be decided before Milestone 1.0
Activity
ximeg commentedon Jul 14, 2020
I know this behavior does not seem logical and was surprising for me as well, but this is rooted deeply into the strangeness of the R language.
min ~ 320is anexpressionin R, and not a numeric range or array. If you save an expression intowl_range, it works:I'm not sure if anything should or can be done about this. I'd prefer to ask @cbeleites , she knows better.
cbeleites commentedon Jul 24, 2020
The high priority label is only because this has aspects that I think we should consider in the next video call. After that, it may turn out to be low priority or high priority to be done before the 1.0 milestone - depending on our decision.
cbeleites commentedon Jul 27, 2020
Without looking into details, I think what happens is that a
formulacan have its own environment attached, and thus if the envirnoment is from.GlobalEnv,minandmaxwill evaluate to functionsmin()andmax()(or to the values of variablesminandmaxif defined).Possible alternatives:
as Roman says, explicitly using an expression rather than a formula helps
we could (slowly) move to using other names (e.g.
lowandhighas in ChemoSpec) that are less likely to exist in the user's workspace.Iff such a variable exists, there will be maybe even more of a surprise, though.
Currently, we don't extrapolate outside the existing wavelength range of the hyperSpec object. I.e. for an object with wavelength range 500 : 1500, calling
wl2i(object, 499.999)will returnNA.If we drop this behaviour, we could access lowest and highest possible wavelength with
-Infand+Inf, respectively. This would completely remove the surprises with formulas.Since the "no extrapolation" is useful, though, this should at least be switched by a flag/parameter.
+Inf/-Infand have the extrapolation behaviour at least for these.Any change to the user interface here should be decided before Milestone 1.0
GegznaV commentedon Aug 12, 2021
This discussion was moved and reopen as: