Hi @kevinushey, I wonder if you could help with a follow-up on this? I am working on adding this new S3 method to the units package and am not able to get past one point:
units contains an S3 class mixed_units which is a modified list. When a mixed_units object is constructed the list class is overwritten, but R still identifies it as a list:
library(units)
mix <- units::mixed_units(1:5, c("s", "m", "cd", "kg", "lb"))
class(mix)
## "mixed_units"
inherits(mix, "list")
## FALSE
is.list(mix)
## TRUE
The new S3 str.mixed_units() method gives the output I want in the terminal, but not in the RStudio environment pane (see screenshot). In the screenshot below I would like the environment pane to display Mixed units: cd (1), kg (1), lb (1), m (1), s (1) instead of List of 5. The display for column b of the dataframe gives you an idea of what I'm trying to achieve.
I assume this is something to do with the fact that the object is still a list, but it would be good to understand why things are treated differently when the list is its own object vs when it is a list col in a data frame. Any pointers would be much appreciated.
Thanks, Lewin
P.S. This is the current function definition:
str.mixed_units = function(x, ...) {
tbl <- table(as.character(units(x)))
tbl <- paste(names(tbl), " (", as.numeric(tbl), ")", sep = "")
tbl_str <- paste(tbl, collapse = ", ")
file <- textConnection("rval", "w", local = TRUE)
sink(file)
on.exit(sink())
NextMethod()
sink()
on.exit()
cat(" Mixed units:", tbl_str, "\n")
cat(rval[2:length(rval)], sep = "\n")
}