From 92ab0188cc061095755abff7e80fabea136cb64d Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Fri, 4 Mar 2016 10:30:46 -0500 Subject: [PATCH] byte_format --- NAMESPACE | 5 ++++ NEWS | 4 +++ R/formatters.r | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.Rmd | 3 +- man/byte_format.Rd | 50 ++++++++++++++++++++++++++++++++ man/geom_bkde.Rd | 34 ++++++++++++---------- man/geom_bkde2d.Rd | 34 ++++++++++++---------- man/geom_xspline.Rd | 34 ++++++++++++---------- man/stat_ash.Rd | 34 ++++++++++++---------- 9 files changed, 219 insertions(+), 61 deletions(-) create mode 100644 NEWS create mode 100644 R/formatters.r create mode 100644 man/byte_format.Rd diff --git a/NAMESPACE b/NAMESPACE index d59ada8..3aeb5d2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,13 +6,18 @@ S3method(grobWidth,absoluteGrob) S3method(grobX,absoluteGrob) S3method(grobY,absoluteGrob) export(CoordProj) +export(Gb) export(GeomBkde) export(GeomBkde2d) export(GeomXspline) +export(Kb) +export(Mb) export(StatAsh) export(StatBkde) export(StatBkde2d) export(StatXspline) +export(byte_format) +export(bytes) export(coord_proj) export(geom_bkde) export(geom_bkde2d) diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..550cae0 --- /dev/null +++ b/NEWS @@ -0,0 +1,4 @@ +0.1.5 +===================== +* Pokemon discrete color scales! +* `byte_format` (et al) scales (e.g. 10000 => 10 Kb) diff --git a/R/formatters.r b/R/formatters.r new file mode 100644 index 0000000..7a1f71b --- /dev/null +++ b/R/formatters.r @@ -0,0 +1,82 @@ +#' Bytes formatter: convert to byte measurement and display symbol. +#' +#' @return a function with three parameters, \code{x}, a numeric vector that +#' returns a character vector, \code{symbol} the byte symbol (e.g. "\code{Kb}") +#' desired and the measurement \code{units} (traditional \code{binary} or +#' \code{si} for ISI metric units). +#' @param x a numeric vector to format +#' @param symbol byte symbol to use. If "\code{auto}" the symbol used will be +#' determined by the maximum value of \code{x}. Valid symbols are +#' "\code{b}", "\code{K}", "\code{Mb}", "\code{Gb}", "\code{Tb}", "\code{Pb}", +#' "\code{Eb}", "\code{Zb}", and "\code{Yb}", along with their upper case +#' equivalents and "\code{iB}" equivalents. +#' @param units which unit base to use, "\code{binary}" (1024 base) or +#' "\code{si}" (1000 base) for ISI units. +#' @references Units of Information (Wikipedia) : +#' \url{http://en.wikipedia.org/wiki/Units_of_information} +#' @export +#' @examples +#' byte_format()(sample(3000000000, 10)) +#' bytes(sample(3000000000, 10)) +byte_format <- function(symbol="auto", units="binary") { + function(x) bytes(x, symbol, units) +} + +#' @export +#' @rdname byte_format +Kb <- byte_format("Kb", "binary") + +#' @export +#' @rdname byte_format +Mb <- byte_format("Mb", "binary") + +#' @export +#' @rdname byte_format +Gb <- byte_format("Gb", "binary") + +#' @export +#' @rdname byte_format +bytes <- function(x, symbol="auto", units=c("binary", "si")) { + + symbol <- match.arg(symbol, c("auto", + "b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb", + "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", + "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")) + + units <- match.arg(units, c("binary", "si")) + + base <- switch(units, `binary`=1024, `si`=1000) + + if (symbol == "auto") { + symbol <- + if (max(x) >= (base^5)) { "Pb" } + else if (max(x) >= (base^4)) { "Tb" } + else if (max(x) >= (base^3)) { "Gb" } + else if (max(x) >= (base^2)) { "Kb" } + else if (max(x) >= (base^1)) { "Mb" } + else { "b" } + } + + switch(symbol, + "b" =, "B" = paste(x, "bytes"), + + "Kb" =, "KB" = paste(round(x/(base^1), 1L), "Kb"), + "Mb" =, "MB" = paste(round(x/(base^2), 1L), "Mb"), + "Gb" =, "GB" = paste(round(x/(base^3), 1L), "Gb"), + "Tb" =, "TB" = paste(round(x/(base^4), 1L), "Tb"), + "Pb" =, "PB" = paste(round(x/(base^5), 1L), "Pb"), + "Eb" =, "EB" = paste(round(x/(base^6), 1L), "Eb"), + "Zb" =, "ZB" = paste(round(x/(base^7), 1L), "Zb"), + "Yb" =, "YB" = paste(round(x/(base^8), 1L), "Yb"), + + "KiB" = paste(round(x/(base^1), 1L), "KiB"), + "MiB" = paste(round(x/(base^2), 1L), "MiB"), + "GiB" = paste(round(x/(base^3), 1L), "GiB"), + "TiB" = paste(round(x/(base^4), 1L), "TiB"), + "PiB" = paste(round(x/(base^5), 1L), "PiB"), + "EiB" = paste(round(x/(base^6), 1L), "EiB"), + "ZiB" = paste(round(x/(base^7), 1L), "ZiB"), + "YiB" = paste(round(x/(base^8), 1L), "YiB") + ) + +} diff --git a/README.Rmd b/README.Rmd index 85481f1..9665d10 100644 --- a/README.Rmd +++ b/README.Rmd @@ -39,12 +39,13 @@ The following functions are implemented: - `stat_ash` : Compute and display a univariate averaged shifted histogram (polynomial kernel) (uses `ash::ash1`/`ash::bin1`) - `scale_color_pokemon` : - `scale_fill_pokemon` : discrete pokemon scales (data taken from the hard work by the ) +- `byte_format`: + helpers. e.g. turn `10000` into `10 Kb` ### News - Version 0.1.5.9000 - Pokemon discrete color scales! -- Version 0.1.2.9000 - Fixed bug with limits not working thx to @mstrimas +- Version 0.1.2.9000 - Fixed bug with limits not working in coord_proj thx to @mstrimas - Version 0.1.1 - CRAN! - Version 0.1.0.9000 - Tweaks for ggplot2 2.0 release - Version 0.0.4.9000 - `stat_ash` diff --git a/man/byte_format.Rd b/man/byte_format.Rd new file mode 100644 index 0000000..91c1eed --- /dev/null +++ b/man/byte_format.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/formatters.r +\name{byte_format} +\alias{Gb} +\alias{Kb} +\alias{Mb} +\alias{byte_format} +\alias{bytes} +\title{Bytes formatter: convert to byte measurement and display symbol.} +\usage{ +byte_format(symbol = "auto", units = "binary") + +Kb(x) + +Mb(x) + +Gb(x) + +bytes(x, symbol = "auto", units = c("binary", "si")) +} +\arguments{ +\item{symbol}{byte symbol to use. If "\code{auto}" the symbol used will be +determined by the maximum value of \code{x}. Valid symbols are +"\code{b}", "\code{K}", "\code{Mb}", "\code{Gb}", "\code{Tb}", "\code{Pb}", +"\code{Eb}", "\code{Zb}", and "\code{Yb}", along with their upper case +equivalents and "\code{iB}" equivalents.} + +\item{units}{which unit base to use, "\code{binary}" (1024 base) or +"\code{si}" (1000 base) for ISI units.} + +\item{x}{a numeric vector to format} +} +\value{ +a function with three parameters, \code{x}, a numeric vector that + returns a character vector, \code{symbol} the byte symbol (e.g. "\code{Kb}") + desired and the measurement \code{units} (traditional \code{binary} or + \code{si} for ISI metric units). +} +\description{ +Bytes formatter: convert to byte measurement and display symbol. +} +\examples{ +byte_format()(sample(3000000000, 10)) +bytes(sample(3000000000, 10)) +} +\references{ +Units of Information (Wikipedia) : + \url{http://en.wikipedia.org/wiki/Units_of_information} +} + diff --git a/man/geom_bkde.Rd b/man/geom_bkde.Rd index f00dda4..d6944d2 100644 --- a/man/geom_bkde.Rd +++ b/man/geom_bkde.Rd @@ -17,12 +17,22 @@ stat_bkde(mapping = NULL, data = NULL, geom = "area", \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or \code{\link{aes_}}. If specified and \code{inherit.aes = TRUE} (the -default), is combined with the default mapping at the top level of the -plot. You only need to supply \code{mapping} if there isn't a mapping -defined for the plot.} +default), it is combined with the default mapping at the top level of the +plot. You must supply \code{mapping} if there is no plot mapping.} -\item{data}{A data frame. If specified, overrides the default data frame -defined at the top level of the plot.} +\item{data}{The data to be displayed in this layer. There are three + options: + + If \code{NULL}, the default, the data is inherited from the plot + data as specified in the call to \code{\link{ggplot}}. + + A \code{data.frame}, or other object, will override the plot + data. All objects will be fortified to produce a data frame. See + \code{\link{fortify}} for which variables will be created. + + A \code{function} will be called with a single argument, + the plot data. The return value must be a \code{data.frame.}, and + will be used as the layer data.} \item{position}{Position adjustment, either as a string, or the result of a call to a position adjustment function.} @@ -47,16 +57,10 @@ rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. \code{\link{borders}}.} -\item{...}{other arguments passed on to \code{\link{layer}}. There are - three types of arguments you can use here: - - \itemize{ - \item Aesthetics: to set an aesthetic to a fixed value, like - \code{color = "red"} or \code{size = 3}. - \item Other arguments to the layer, for example you override the - default \code{stat} associated with the layer. - \item Other arguments passed on to the stat. - }} +\item{...}{other arguments passed on to \code{\link{layer}}. These are +often aesthetics, used to set an aesthetic to a fixed value, like +\code{color = "red"} or \code{size = 3}. They may also be parameters +to the paired geom/stat.} \item{geom, stat}{Use to override the default connection between \code{geom_bkde} and \code{stat_bkde}.} diff --git a/man/geom_bkde2d.Rd b/man/geom_bkde2d.Rd index ce2ee5f..b09e1e7 100644 --- a/man/geom_bkde2d.Rd +++ b/man/geom_bkde2d.Rd @@ -18,12 +18,22 @@ stat_bkde2d(mapping = NULL, data = NULL, geom = "density2d", \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or \code{\link{aes_}}. If specified and \code{inherit.aes = TRUE} (the -default), is combined with the default mapping at the top level of the -plot. You only need to supply \code{mapping} if there isn't a mapping -defined for the plot.} +default), it is combined with the default mapping at the top level of the +plot. You must supply \code{mapping} if there is no plot mapping.} -\item{data}{A data frame. If specified, overrides the default data frame -defined at the top level of the plot.} +\item{data}{The data to be displayed in this layer. There are three + options: + + If \code{NULL}, the default, the data is inherited from the plot + data as specified in the call to \code{\link{ggplot}}. + + A \code{data.frame}, or other object, will override the plot + data. All objects will be fortified to produce a data frame. See + \code{\link{fortify}} for which variables will be created. + + A \code{function} will be called with a single argument, + the plot data. The return value must be a \code{data.frame.}, and + will be used as the layer data.} \item{stat}{The statistical transformation to use on the data for this layer, as a string.} @@ -61,16 +71,10 @@ rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. \code{\link{borders}}.} -\item{...}{other arguments passed on to \code{\link{layer}}. There are - three types of arguments you can use here: - - \itemize{ - \item Aesthetics: to set an aesthetic to a fixed value, like - \code{color = "red"} or \code{size = 3}. - \item Other arguments to the layer, for example you override the - default \code{stat} associated with the layer. - \item Other arguments passed on to the stat. - }} +\item{...}{other arguments passed on to \code{\link{layer}}. These are +often aesthetics, used to set an aesthetic to a fixed value, like +\code{color = "red"} or \code{size = 3}. They may also be parameters +to the paired geom/stat.} \item{geom}{default geom to use with this stat} diff --git a/man/geom_xspline.Rd b/man/geom_xspline.Rd index b4b5f7b..72676fd 100644 --- a/man/geom_xspline.Rd +++ b/man/geom_xspline.Rd @@ -18,12 +18,22 @@ stat_xspline(mapping = NULL, data = NULL, geom = "line", \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or \code{\link{aes_}}. If specified and \code{inherit.aes = TRUE} (the -default), is combined with the default mapping at the top level of the -plot. You only need to supply \code{mapping} if there isn't a mapping -defined for the plot.} +default), it is combined with the default mapping at the top level of the +plot. You must supply \code{mapping} if there is no plot mapping.} -\item{data}{A data frame. If specified, overrides the default data frame -defined at the top level of the plot.} +\item{data}{The data to be displayed in this layer. There are three + options: + + If \code{NULL}, the default, the data is inherited from the plot + data as specified in the call to \code{\link{ggplot}}. + + A \code{data.frame}, or other object, will override the plot + data. All objects will be fortified to produce a data frame. See + \code{\link{fortify}} for which variables will be created. + + A \code{function} will be called with a single argument, + the plot data. The return value must be a \code{data.frame.}, and + will be used as the layer data.} \item{position}{Position adjustment, either as a string, or the result of a call to a position adjustment function.} @@ -50,16 +60,10 @@ closed shape.} first and last control points should be replicated for drawing the curve. Ignored for closed X-splines.} -\item{...}{other arguments passed on to \code{\link{layer}}. There are - three types of arguments you can use here: - - \itemize{ - \item Aesthetics: to set an aesthetic to a fixed value, like - \code{color = "red"} or \code{size = 3}. - \item Other arguments to the layer, for example you override the - default \code{stat} associated with the layer. - \item Other arguments passed on to the stat. - }} +\item{...}{other arguments passed on to \code{\link{layer}}. These are +often aesthetics, used to set an aesthetic to a fixed value, like +\code{color = "red"} or \code{size = 3}. They may also be parameters +to the paired geom/stat.} \item{geom, stat}{Use to override the default connection between \code{geom_xspline} and \code{stat_xspline}.} diff --git a/man/stat_ash.Rd b/man/stat_ash.Rd index 0871129..6c20a2e 100644 --- a/man/stat_ash.Rd +++ b/man/stat_ash.Rd @@ -11,12 +11,22 @@ stat_ash(mapping = NULL, data = NULL, geom = "area", position = "stack", \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or \code{\link{aes_}}. If specified and \code{inherit.aes = TRUE} (the -default), is combined with the default mapping at the top level of the -plot. You only need to supply \code{mapping} if there isn't a mapping -defined for the plot.} +default), it is combined with the default mapping at the top level of the +plot. You must supply \code{mapping} if there is no plot mapping.} -\item{data}{A data frame. If specified, overrides the default data frame -defined at the top level of the plot.} +\item{data}{The data to be displayed in this layer. There are three + options: + + If \code{NULL}, the default, the data is inherited from the plot + data as specified in the call to \code{\link{ggplot}}. + + A \code{data.frame}, or other object, will override the plot + data. All objects will be fortified to produce a data frame. See + \code{\link{fortify}} for which variables will be created. + + A \code{function} will be called with a single argument, + the plot data. The return value must be a \code{data.frame.}, and + will be used as the layer data.} \item{geom}{Use to override the default Geom} @@ -47,16 +57,10 @@ rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. \code{\link{borders}}.} -\item{...}{other arguments passed on to \code{\link{layer}}. There are - three types of arguments you can use here: - - \itemize{ - \item Aesthetics: to set an aesthetic to a fixed value, like - \code{color = "red"} or \code{size = 3}. - \item Other arguments to the layer, for example you override the - default \code{stat} associated with the layer. - \item Other arguments passed on to the stat. - }} +\item{...}{other arguments passed on to \code{\link{layer}}. These are +often aesthetics, used to set an aesthetic to a fixed value, like +\code{color = "red"} or \code{size = 3}. They may also be parameters +to the paired geom/stat.} } \description{ See \code{\link[ash]{bin1}} & \code{\link[ash]{ash1}} for more information.