diff --git a/DESCRIPTION b/DESCRIPTION index 432003cd..dcd5aa55 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,6 +42,7 @@ Depends: Imports: graphics, grDevices, + methods, stats, tools, utils diff --git a/NAMESPACE b/NAMESPACE index ca4f2e2f..c5ef4735 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method(tinyplot,data.frame) S3method(tinyplot,default) S3method(tinyplot,density) S3method(tinyplot,formula) @@ -91,6 +92,7 @@ importFrom(graphics,segments) importFrom(graphics,strwidth) importFrom(graphics,text) importFrom(graphics,title) +importFrom(methods,is) importFrom(stats,aggregate) importFrom(stats,approx) importFrom(stats,as.formula) diff --git a/R/tinyplot.R b/R/tinyplot.R index 2ac3727e..9e13b222 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -522,7 +522,16 @@ #' ) #' # reset the theme #' tinytheme() -#' +#' +#' # tinyplot can be used with pipes without specifying the data argument, +#' # as long as a formula is provided: +#' +#' \dontrun{ +#' aq |> +#' subset(Month == "May") |> +#' tinyplot(Temp ~ Wind) +#' } +#' #' # For more examples and a detailed walkthrough, please see the introductory #' # tinyplot tutorial available online: #' # https://grantmcdermott.com/tinyplot/vignettes/introduction.html @@ -1489,6 +1498,15 @@ tinyplot.density = function( } +#' @rdname tinyplot +#' @importFrom methods is +#' @export +tinyplot.data.frame <- function(x = NULL, formula = NULL, ...) { + pars <- as.list(match.call()[-1]) + if(!is(pars$formula, "call")) { stop("If the first argument to tinyplot is a data frame, then the second must be a formula.") } + tinyplot(formula, x, ...) +} + #' @export #' @name plt diff --git a/man/tinyplot.Rd b/man/tinyplot.Rd index 24e0ba77..3a8036d7 100644 --- a/man/tinyplot.Rd +++ b/man/tinyplot.Rd @@ -5,6 +5,7 @@ \alias{tinyplot.default} \alias{tinyplot.formula} \alias{tinyplot.density} +\alias{tinyplot.data.frame} \alias{plt} \title{Lightweight extension of the base R plotting function} \usage{ @@ -90,6 +91,8 @@ tinyplot(x, ...) \method{tinyplot}{density}(x = NULL, type = c("l", "area"), ...) +\method{tinyplot}{data.frame}(x = NULL, formula = NULL, ...) + plt(x, ...) } \arguments{ @@ -675,6 +678,15 @@ tinyplot( # reset the theme tinytheme() +# tinyplot can be used with pipes without specifying the data argument, +# as long as a formula is provided: + +\dontrun{ +aq |> + subset(Month == "May") |> + tinyplot(Temp ~ Wind) +} + # For more examples and a detailed walkthrough, please see the introductory # tinyplot tutorial available online: # https://grantmcdermott.com/tinyplot/vignettes/introduction.html