Crunch-io / rcrunch

R package for interacting with the Crunch API
https://crunch.io/r/crunch/
GNU Lesser General Public License v3.0
9 stars 15 forks source link

TabBookResult returns aliases and name as "total" for all NumericVariable type questions #509

Open 1beb opened 3 years ago

1beb commented 3 years ago

As the title specifies it appears that aliases returns as "total" for all NumericVariable type questions.

Here's a complete, but simple example:

library(crunch) 
# ds <- newExampleDataset()
ds <- loadDataset("Example dataset")
multitable <- newMultitable("~ `allpets`", ds)
r <- tabBook(multitable, dataset = ds[c("ndogs")])
aliases(r)
[1] "total"
# should be ndogs

In the example above the alias is not "total", it should be "ndogs"

gergness commented 3 years ago

Hey, I was able to trace this back to backend behavior. There are reasons that it works this way, but people agreed that it should be fixed, hopefully as we pay more attention to numeric variables with the incoming numeric arrays.

As a work-around, the "name" of the variable is included on the analysis page, so you can match it to the names of the dataset like so, to get back the aliases:

library(crunch) 
# ds <- newExampleDataset()
ds <- loadDataset("Example dataset")
multitable <- newMultitable("~ `allpets`", ds)
r <- tabBook(multitable, dataset = ds[c("ndogs")])

# Instead of: aliases(r)
get_tabbook_aliases <- function(tabbook, dataset) {
    var_info <- variables(dataset)
    vapply(tabbook$meta$analyses, function(analysis) {
        aliases(var_info[names(var_info) == analysis$name])
    }, "")
}

get_tabbook_aliases(r, ds)
#> [1] "ndogs"