Hola, novice package builder in need of advice.
I need to parse a batch of spreadsheets that have the same data, but in five or six distinct formats. I can handle this when the processing steps are small:
obj1 <- list(a = 'A', b = NULL)
obj2 <- list(a = NULL, b = 'B')
parser <- function(obj) {
if(is.null(obj$a))
return(obj$b)
if(is.null(obj$b))
return(obj$a)
}
parser(obj1)
#> [1] "A"
parser(obj2)
#> [1] "B"
Created on 2019-10-30 by the reprex package (v0.3.0)
But anything involving more than a few lines quickly becomes unwieldy.
Question: How can I have multiple versions of the same function, without creating some monolith full of if/else or switch statements?
If this were an API, I'd write: parser/v1/obj and parser/v2/obj and have versions of code with different addresses.
Stretch goal - how I can I refactor and organise this code, so that it's clear which code maps to which format?
Many thanks!