Skip to content

Commit

Permalink
Adding knit_print functions and vignette.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanedwards committed Apr 27, 2017
1 parent 7150d36 commit 4565f5d
Show file tree
Hide file tree
Showing 17 changed files with 953 additions and 18 deletions.
27 changes: 15 additions & 12 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ Title: splot - Stefan's plotting package
Version: 0.1.0
Author: Stefan McKinnon Edwards <sme@iysik.com>
Maintainer: Stefan McKinnon Edwards <sme@iysik.com>
Description: Yet another ggplot2 extension package.
Description: Yet another ggplot2 and knitr extension package.
Imports:
ggplot2,
grid,
gridExtra,
gtable,
ggplot2,
grid,
gridExtra,
gtable,
RCurl
Depends:
ggplot2,
grid,
gridExtra,
gtable,
Enhances:
ggplot2
ggplot2,
grid,
gridExtra,
gtable,
Enhances: ggplot2,
knitr
License: GPL-3
Encoding: UTF-8
LazyData: true
Expand All @@ -27,7 +28,9 @@ Collate:
'coord-capped.r'
'coord-flex.r'
'facet-rep-lab.r'
'knit_print.r'
'splot.r'
Suggests: knitr,
Suggests:
knitr,
rmarkdown
VignetteBuilder: knitr
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(knit_print,data.frame)
S3method(knit_print,grouped_df)
S3method(knit_print,tbl_df)
export(CoordFlexCartesian)
export(CoordFlexFixed)
export(CoordFlexFlipped)
Expand All @@ -17,11 +20,11 @@ export(coord_flex_fixed)
export(coord_flex_flip)
export(facet_rep_grid)
export(facet_rep_wrap)
export(find_panel)
import(ggplot2)
import(grid)
import(gridExtra)
import(gtable)
import(knitr)
importFrom(ggplot2,CoordCartesian)
importFrom(ggplot2,CoordFixed)
importFrom(ggplot2,CoordFlip)
Expand Down
56 changes: 56 additions & 0 deletions R/knit_print.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

#' knitr extension: Always use `kable` for data frames.
#'
#' Convenience function for working with R Notebooks that ensures data frames
#' (and dplyr tables) are printed with \code{\link[knitr]{knit_print}} while
#' allowing RStudio to render the data frame dynamically for inline display.
#' \strong{NB:} Automatically enables when package \code{splot} is loaded!
#'
#' To disable these functions use \code{options(knit_print=FALSE)}.
#' Set to \code{TRUE} to re-enable.
#'
#' To supply \code{\link[knitr]{kable}} functions such as \code{caption} or
#' \code{col.names}, set chunk options \code{kable.opts}:
#'
#' \preformatted{
#' ```{r kable.opts=list(caption='This is kable table caption.')}`
#' data.frame
#' ```
#' }
#'
#'
#'
#'
#' @param x an data frame or dplyr table object to be printed
#' @param options Current chunk options are passed through this argument.
#' @param ... Ignored for now.
#' @seealso \code{\link[knitr]{knit_print}}, \code{\link[knitr]{kable}}
#' @rdname knit_print
#' @keywords print
#' @export
#' @import knitr
knit_print.data.frame = function(x, options, ...) {
if (getOption('knit_print', default=TRUE) == FALSE)
return(normal_print(x, options, ...))
opts <- options$`kable.opts`
if (is.null(opts)) opts <- NULL
opts <- RCurl::merge.list(list(x=x, digits=2), opts)
res = paste(c("","", do.call(kable, opts)), collapse="\n")
asis_output(res)
}

#' @inheritParams knit_print.tbl_df
#' @rdname knit_print
#' @export
#' @keywords print
knit_print.tbl_df = function(x, ...) {
knit_print.data.frame(x, ...)
}

#' @inheritParams knit_print.tbl_df
#' @rdname knit_print
#' @export
#' @keywords print
knit_print.grouped_df = function(x, ...) {
knit_print.data.frame(x, ...)
}
6 changes: 6 additions & 0 deletions R/splot.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
#' \code{\link{brackets_horisontal}} and \code{\link{brackets_vertical}}.
#'
#'
#' @section Extending knitr:
#'
#' We automatically load knitr's \code{\link[knitr]{knit_print}} for
#' data frames and dplyr tables to provide automatic pretty printing of
#' data frame using \code{\link[knitr]{kable}}.
#'
#' See \code{\link{knit_print.data.frame}}.
#'
#' @docType package
#' @name splot
Expand Down
Binary file modified README-brackets_demo-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 27 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ knitr::opts_chunk$set(

# splot - Stefan's plotting package

Just another [ggplot2](http://ggplot2.tidyverse.org) extension.
Just another [ggplot2](http://ggplot2.tidyverse.org) and
[knitr](https://yihui.name/knitr/) extension package.


## Installation
Expand Down Expand Up @@ -49,4 +50,29 @@ ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) +
```


## Extensions to knitr

We automatically load knitr's `lnit_print` for
data frames and dplyr tables to provide automatic pretty printing of
these using `kable`:

Before loading `splot` package:
```{r knit_print_disable,include=FALSE}
options(knit_print=FALSE)
```
```{r}
data(USArrests)
head(USArrests)
```

After loading `splot`:
```{r knit_print_enable,include=FALSE}
options(knit_print=TRUE)
```
```{r}
head(USArrests)
```

See `knit_print.data.frame`.


39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ Just another [ggplot2](http://ggplot2.tidyverse.org) extension.
Installation
------------

Installation
------------

``` r
# Or the the development version from GitHub:
# install.packages("devtools")
Expand Down Expand Up @@ -48,3 +45,39 @@ ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) +
```

![](README-brackets_demo-1.png)

Extensions to knitr
-------------------

We automatically load knitr's k`nit_print` for data frames and dplyr tables to provide automatic pretty printing of these using `kable`:

Before loading `splot` package:

``` r
data(USArrests)
head(USArrests)
#> Murder Assault UrbanPop Rape
#> Alabama 13.2 236 58 21.2
#> Alaska 10.0 263 48 44.5
#> Arizona 8.1 294 80 31.0
#> Arkansas 8.8 190 50 19.5
#> California 9.0 276 91 40.6
#> Colorado 7.9 204 78 38.7
```

After loading `splot`:

``` r
head(USArrests)
```

| | Murder| Assault| UrbanPop| Rape|
|------------|-------:|--------:|---------:|-----:|
| Alabama | 13.2| 236| 58| 21.2|
| Alaska | 10.0| 263| 48| 44.5|
| Arizona | 8.1| 294| 80| 31.0|
| Arkansas | 8.8| 190| 50| 19.5|
| California | 9.0| 276| 91| 40.6|
| Colorado | 7.9| 204| 78| 38.7|

See `knit_print.data.frame`.
9 changes: 8 additions & 1 deletion man/brackets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions man/knit_print.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions man/splot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions vignettes/capped-axes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## ----setup,include=FALSE-------------------------------------------------
library(knitr)

knitr::opts_chunk$set(fig.height=4, fig.width=6)

## ----load_pkg_and_data,fig.cap='Default ggplot2 plotting.'---------------
#library(ggplot2)
library(splot)

dat1 <- data.frame(
gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30),
cl = sample.int(3, 30, replace=TRUE),
cl2 = sample(c('a','b','c'), 30, replace=TRUE)
)

my.theme <- theme_light()

(
p <- ggplot(dat1, aes(gp, y)) + geom_point() + my.theme
)

## ----theme---------------------------------------------------------------
my.theme <- my.theme + theme(panel.border=element_blank(), axis.line = element_line())
p <- p + my.theme

## ----fig.cap='Using `coord_capped_cart` to cap the bottom axis from the right. The left axis is unaffected.'----
p + coord_capped_cart(bottom='right')

## ----fig.cap='As before, but left axis is now also capped to give a consistent look.'----
p + coord_capped_cart(bottom='right', left='none')

## ----fig.cap='Placing brackets brackets instead of ticks emphasises that the x-scale is categorical and not nominal.'----
ggplot(dat1, aes(gp, y)) + geom_point(position=position_jitter(width=0.2, height=0)) +
coord_capped_cart(left='none', bottom=brackets_horisontal()) +
my.theme + theme(panel.grid.major.x = element_blank())

## ----eval=FALSE----------------------------------------------------------
#
# # Facet grid -----------------
# ggplot(dat1, aes(gp, y)) + geom_point() +
# coord_capped_flip(bottom = 'left', left='none') +
# theme(axis.title=element_blank(), plot.title=element_text(size=rel(1))) +
# facet_rep_grid(~cl)
#
# dat2 <- rbind(dat1, data.frame(gp=letters[1:3], y=rnorm(3, 2), cl=3, cl2='b'))
# ggplot(dat2, aes(gp, y)) + geom_point() +
# coord_capped_flip(bottom = 'left', left='none') +
# theme(axis.title=element_blank(), plot.title=element_text(size=rel(1))) +
# facet_rep_grid(.~cl, scales='free_y')
#
#

Loading

0 comments on commit 4565f5d

Please sign in to comment.