Jennifer Lyon
2010-Jul-25 11:58 UTC
[R] Discrepancy in help for object.size() and question on how to round object sizes
I think there is a discrepancy In the help for object.size(). In the usage section there is the comment: ## S3 method for class 'object_size': while in the value section there is the text: An object of class ?"object.size"? ... And R itself says: > class(object.size(letters)) [1] "object_size" Also, I was trying to print out object sizes in Kb, but rounded to whole number of units, but I could not figure out how to do that without having to replicate the math in the print method for object.size.> tmp<-numeric(100000) > object.size(tmp)800024 bytes> print(object.size(tmp), units="Kb")781.3 Kb What I want is 781 Kb, but it wasn't obvious how to get there. The following seemed wrong, and didn't work:> print(signif(object.size(tmp),1), units="Kb")781.2 Kb> print(round(object.size(tmp)), units="Kb")781.3 Kb I know I can just get the result in bytes and divide by 1024, and then round, but it seemed like there should be a more straightforward way, so any suggestions are welcome. Thanks! Jen> sessionInfo()R version 2.11.1 (2010-05-31) i686-pc-linux-gnu locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics utils datasets grDevices methods base other attached packages: [1] RColorBrewer_1.0-2 loaded via a namespace (and not attached): [1] tools_2.11.1
Duncan Murdoch
2010-Jul-25 12:45 UTC
[R] Discrepancy in help for object.size() and question on how to round object sizes
On 25/07/2010 7:58 AM, Jennifer Lyon wrote:> I think there is a discrepancy In the help for object.size(). > > In the usage section there is the comment: > ## S3 method for class 'object_size': > > while in the value section there is the text: > An object of class ?"object.size"? ... > > And R itself says: > > > class(object.size(letters)) > [1] "object_size"Thanks, I'll fix the typo.> > Also, I was trying to print out object sizes in Kb, but rounded to > whole number of units, but I could not > figure out how to do that without having to replicate the math in the > print method for object.size.I don't think there's any simple way to do it. You can see the print method using utils:::print.object_size and it has one decimal point hard coded into the calls to round(). So you could copy that print method and change the rounding, or if you don't mind sloppy truncating rather than proper rounding, you could do a hack like this: wholesize <- function(x, units="b") { size <- capture.output(print(object.size(x), units=units)) cat( sub("\\.[[:digit:]]", "", size), "\n" ) } We could add a "decimals" argument to the version in R, but I'd rather not: it just introduces another point for people to confuse formatting with rounding, e.g. if the size is exactly 1000 Kb, should it be printed as 1000.0 Kb if you ask for 1 decimal? (Currently it will print as 1000 Kb.) Duncan Murdoch> >> tmp<-numeric(100000) >> object.size(tmp) > 800024 bytes > >> print(object.size(tmp), units="Kb") > 781.3 Kb > > What I want is 781 Kb, but it wasn't obvious how to get there. The > following seemed wrong, and > didn't work: > >> print(signif(object.size(tmp),1), units="Kb") > 781.2 Kb >> print(round(object.size(tmp)), units="Kb") > 781.3 Kb > > I know I can just get the result in bytes and divide by 1024, and then > round, but it seemed like there > should be a more straightforward way, so any suggestions are welcome. > > Thanks! > > Jen > >> sessionInfo() > R version 2.11.1 (2010-05-31) > i686-pc-linux-gnu > > locale: > [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 > [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 > [7] LC_PAPER=en_US.UTF-8 LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics utils datasets grDevices methods base > > other attached packages: > [1] RColorBrewer_1.0-2 > > loaded via a namespace (and not attached): > [1] tools_2.11.1 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Reasonably Related Threads
- function remains loaded in the search space after detaching the package
- An R vs. SAS Discrepancy: How do I determine which is correct?
- object.size vs lobstr::obj_size
- format(object.size(...), units): KB, MB, and GB instead of Kb, Mb, and Gb?
- Looks like a bug in subsetting of a complicated object