Follow-up on feedback from CAA#55
Conversation
|
Where I am stuck:
|
|
Our old code is not compatible with this kind of strategy. The critical bit is this function in apply_bdl_strategy <- function(x, colname, bdl, bdl_strategy) {
bdl_values <- which(grepl(paste(bdl, collapse = "|"), x, perl = FALSE))
x[bdl_values] <- bdl_strategy()
return(x)
}At the moment the bdl strategy can essentially only be a static function - it does not have any inputs. We forsaw that eventually we could need other strategies so we laid the foundation for more flexibility, but we never fleshed it out. The comment above # colname only an argument in case we want to implement more specific handling
# eventuallySo how do we go beyond this? I see multiple possible solutions. The easiest would probably be to break the current interface and merge bdl_strategy_default(x, ...) {
bdl_strings <- c("b.d.", "bd", "b.d.l.", "bdl", "<LOD", "<")
bdl_indices <- which(grepl(paste(bdl_strings, collapse = "|"), x, perl = FALSE))
x[bdl_indices] <- NA_character_
return(x)
}bdl_strategy_negative(x, ...) {
y <- suppressWarnings(as.numeric(x))
bdl_indices <- which(y < 0)
x[bdl_indices] <- NA_character_
return(x)
}I think this would work 🤔. It is also not too complicated; even easier than what we have now. Of course it's a bit tedious to write a custom strategy function, but if we include these two examples then it should be reasonably clear how to do so. If you're OK with this conceptually I can offer to implement it. Maybe you write a quick (currently failing) test for this requirement. Then I would see right away if my implementation behaves as you would like it to. |
|
Ah - about your questions: I don't understand what you mean here, honestly. Could you explain what you mean? I guess I'm just missing something. |
|
Thank you, this sounds very good! I think merging them into a function would be great. It may look more "frightening" but at the same time more logical to have only one input for bdl. And with pre-made functions and good examples, this should actually not be that frightening. I agree that they should all have the same structure of input (and of course must have output). I envision here a single documentation page for this family. The functions could look like
Would it be possible to stack multiple functions, maybe something like this? This would allow to e.g. replace common labels for bdl as it is currently the case while handling e.g. negative values. For clarification to my questions: Thanks a lot for implementing it, that's a great help! I'm very happy to write a test. |
|
I implemented a draft of what I had in mind. It's not (yet) what you're describing, but your idea could be added easily. My idea was to literally write out the functions in Our pre-built functions and any the users may write can be used in bdl_strategy = bdl_strategy_defaultMultiple of these can be combined to be applied one after another with function composition: bdl_strategy = purrr::compose(bdl_strategy_default, bdl_strategy_negative)Your factories would be used like this, I guess: bdl_strategy = make_bdl_strategy(column_names, replacement_values, input_values)
|
|
This looks all good to me! Combining functions with is perfectly fine. It works if Moreover, even when working, it seems to skip the first column (d65Cu) - negative values there are not replaced. |
Function for
bdl_strategythat replaces negative values in selected columns with a uniform value (e.g.NAor 0).Follow-up on feedback gathered at the CAA (#43)