[R-gregmisc-users] SF.net SVN: r-gregmisc:[1936] trunk/gdata
Brought to you by:
warnes
From: <wa...@us...> - 2015-04-22 23:15:01
|
Revision: 1936 http://sourceforge.net/p/r-gregmisc/code/1936 Author: warnes Date: 2015-04-22 23:14:54 +0000 (Wed, 22 Apr 2015) Log Message: ----------- Fix 'units' argument of humanReadable() Modified Paths: -------------- trunk/gdata/DESCRIPTION trunk/gdata/R/object.size.R trunk/gdata/man/humanReadable.Rd Added Paths: ----------- trunk/gdata/R/humanReadable.R Modified: trunk/gdata/DESCRIPTION =================================================================== --- trunk/gdata/DESCRIPTION 2015-04-22 22:44:46 UTC (rev 1935) +++ trunk/gdata/DESCRIPTION 2015-04-22 23:14:54 UTC (rev 1936) @@ -4,8 +4,8 @@ Depends: R (>= 2.13.0) SystemRequirements: perl Imports: gtools -Version: 2.15.0 -Date: 2015-04-06 +Version: 2.16.0 +Date: 2015-04-21 Author: Gregory R. Warnes, Ben Bolker, Gregor Gorjanc, Gabor Grothendieck, Ales Korosec, Thomas Lumley, Don MacQueen, Arni Magnusson, Jim Rogers, and others Added: trunk/gdata/R/humanReadable.R =================================================================== --- trunk/gdata/R/humanReadable.R (rev 0) +++ trunk/gdata/R/humanReadable.R 2015-04-22 23:14:54 UTC (rev 1936) @@ -0,0 +1,61 @@ +humanReadable <- function(x, standard=c("SI", "IEC"), units, digits=1, width=3, sep=" ") +{ + ## --- Setup --- + + suffix.decimal <- c("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") + suffix.binary <- c("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB") + + standard <- match.arg(standard) + + ## --- Functions --- + + .applyHuman <- function(x, base, suffix, digits, width, sep) + { + ## Which suffix should we use? + n <- length(suffix) + for(i in 1:n) { + if(x >= base) { + if(i < n) x <- x / base + } else { + break + } + } + ## Formatting + if(is.null(width)) { ## the same formatting for all + x <- format(round(x=x, digits=digits), nsmall=digits) + } else { ## similar to ls, du, and df + lenX <- nchar(x) + if(lenX > width) { + digitsMy <- width - (lenX - (lenX - (nchar(round(x)) + 1))) + digits <- ifelse(digitsMy > digits, digits, digitsMy) + } + if(i == 1) digits <- 0 + x <- round(x, digits=digits) + } + paste(x, suffix[i], sep=sep) + } + + ## -- Work + + if(any(x < 0)) stop("'x' must be positive") + if(standard == "SI") { + suffix <- suffix.decimal + base <- 10^3 + } else { + suffix <- suffix.binary + base <- 2^10 + } + + if(!missing(units)) + { + units <- match.arg( units, suffix ) + power <- match(units, suffix ) -1 + X <- x/(base^power) + X <- format.default(round(x=X, digits=digits), nsmall=digits) + X <- paste(X, units) + X + } + else + sapply(X=x, FUN=".applyHuman", base=base, suffix=suffix, digits=digits, + width=width, sep=sep) +} Modified: trunk/gdata/R/object.size.R =================================================================== --- trunk/gdata/R/object.size.R 2015-04-22 22:44:46 UTC (rev 1935) +++ trunk/gdata/R/object.size.R 2015-04-22 23:14:54 UTC (rev 1936) @@ -104,5 +104,6 @@ width=width, sep=sep) } + ###------------------------------------------------------------------------ ### object.size.R ends here Modified: trunk/gdata/man/humanReadable.Rd =================================================================== --- trunk/gdata/man/humanReadable.Rd 2015-04-22 22:44:46 UTC (rev 1935) +++ trunk/gdata/man/humanReadable.Rd 2015-04-22 23:14:54 UTC (rev 1936) @@ -19,15 +19,14 @@ } \usage{ - -humanReadable(x, standard="SI", digits=1, width=3, sep=" ") - +humanReadable(x, standard=c("SI","IEC"), units, digits=1, width=3, sep=" ") } \arguments{ \item{x}{integer, byte size} - \item{standard}{character, "SI" for powers of 1000 or anything else for + \item{standard}{character, either "SI" for powers of 1000 or "IEC" for powers of 1024, see details} + \item{units}{character, unit to use for all values (optional)} \item{digits}{integer, number of digits after decimal point} \item{width}{integer, width of number string} \item{sep}{character, separator between number and unit} @@ -104,6 +103,14 @@ \examples{ +# Simple example: maximum addressible size of 32 bit pointer +humanReadable(2^32-1, standard="SI") +humanReadable(2^32-1, standard="IEC") + +humanReadable(2^32-1, standard="SI", unit="MB") +humanReadable(2^32-1, standard="IEC", unit="MiB") + + baseSI <- 10 powerSI <- seq(from=3, to=27, by=3) SI0 <- (baseSI)^powerSI @@ -117,12 +124,22 @@ IEC1 <- IEC0 - IEC0 / c(2, runif(n=k, min=1.01, max=5.99)) IEC2 <- IEC0 + IEC0 / c(2, runif(n=k, min=1.01, max=5.99)) +# Auto units cbind(humanReadable(x=SI1, width=NULL, digits=3), humanReadable(x=SI0, width=NULL, digits=2), humanReadable(x=SI2, width=NULL, digits=1), humanReadable(x=IEC1, standard="IEC", width=7, digits=3), humanReadable(x=IEC0, standard="IEC", width=7, digits=2), humanReadable(x=IEC2, standard="IEC", width=7, digits=1)) + +# Single unit +cbind(humanReadable(x=SI1, units="GB", width=NULL, digits=3), + humanReadable(x=SI0, units="GB", width=NULL, digits=2), + humanReadable(x=SI2, units="GB", width=NULL, digits=1), + humanReadable(x=IEC1, units="GiB", standard="IEC", width=7, digits=3), + humanReadable(x=IEC0, units="GiB", standard="IEC", width=7, digits=2), + humanReadable(x=IEC2, units="GiB", standard="IEC", width=7, digits=1)) + } \keyword{misc} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |