Skip to content

obj_size() formatting for large objects #57

@jthomasmock

Description

@jthomasmock

When I typically reach for obj_size I am using it on relatively large objects (MBs or GBs). I am curious if there is a recommended formatting method for converting from printing as Bytes to KB, MB, GB, etc. I can manually convert with a combination of division + fsprint, paste0, round etc.

There are a few r-lib packages that provide nice printing of bytes, but as far as I can tell do not have a method for lobstr_bytes objects. Does it make sense to have prettyunits operate natively on lobstr_bytes via a method or even having a function argument inside obj_size() for unit conversion/prettier printing?

Thank you for your time reviewing. 🖖

library(prettyunits)
library(lobstr)
library(magrittr)

car_size <- obj_size(mtcars)

# lobstr_bytes format
class(car_size)
#> [1] "lobstr_bytes"

car_size_num <- obj_size(mtcars) %>% 
  as.numeric()

# numeric
class(car_size_num)
#> [1] "numeric"

# scales::number_bytes() is deprecated
# and only works on numeric converted obj_size
scales::number_bytes(car_size)
#> Error in UseMethod("round_any"): no applicable method for 'round_any' applied to an object of class "lobstr_bytes"
scales::number_bytes(as.numeric(car_size))
#> [1] "7 KiB"
scales::number_bytes(unclass(car_size))
#> [1] "7 KiB"

# scales::label_bytes() works for numeric or unclassed objects
# but feels a bit awkward outside of ggplot2
scales::label_bytes()(unclass(car_size))
#> [1] "7 kB"

# prettyunits::pretty_bytes works well
# but again requires numeric or unclassed conversion
prettyunits::pretty_bytes(car_size)
#> Error in as.data.frame.default(x[[i]], optional = TRUE): cannot coerce class '"lobstr_bytes"' to a data.frame
prettyunits::pretty_bytes(unclass(car_size))
#> [1] "7.21 kB"

Created on 2021-08-27 by the reprex package (v2.0.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions