-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding knit_print functions and vignette.
- Loading branch information
1 parent
7150d36
commit 4565f5d
Showing
17 changed files
with
953 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
# | ||
# | ||
|
Oops, something went wrong.