Skip to content

Commit

Permalink
Merge pull request #52 from MattCowgill/dev
Browse files Browse the repository at this point in the history
v0.3.1
  • Loading branch information
MattCowgill authored Jun 10, 2019
2 parents 459a527 + e26ea3b commit 772f2e6
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 19 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: grattantheme
Type: Package
Title: Create Graphs In The Grattan Institute Style
Version: 0.3.0.900
Title: Create Graphs in the Grattan Institute Style
Version: 0.3.1
Authors@R: c(
person("Matt", "Cowgill", email = "[email protected]",
role = c("aut", "cre")),
person("Will", "Mackey", email = "[email protected]",
role = "aut"))
Description: This package enables the user to create ggplot2 charts that conform
Description: Enables the user to create ggplot2 charts that conform
to the Grattan Institute style guide.
License: GPL-3
Depends: R (>= 3.1)
Expand All @@ -27,5 +27,5 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
Suggests:
testthat,
testthat (>= 2.1.0),
covr
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ importFrom(ggthemes,theme_foundation)
importFrom(gridExtra,grid.arrange)
importFrom(knitr,opts_chunk)
importFrom(purrr,walk2)
importFrom(rmarkdown,pandoc_available)
importFrom(rmarkdown,pandoc_version)
importFrom(rmarkdown,render)
importFrom(tools,file_path_sans_ext)
importFrom(utils,tail)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# grattantheme 0.3.0.900
# grattantheme 0.3.1
* New make_presentation() function takes a list of graphs and creates a .pptx presentation with multiple slides
* grattan_save() accepts type="all", which will save your graph in all available types
* Bugs fixed

# grattantheme 0.3.0
* New make_slide() function creates a .pptx slide with an embedded Grattan-y graph
Expand Down
8 changes: 3 additions & 5 deletions R/grattan_save.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,12 @@ grattan_save <- function(filename,

types <- chart_types$type

filenames <- paste0(dir, "/", file_name, "_", types, ".", filetype)
filenames <- file.path(dir, paste0(file_name, "_", types, ".", filetype))

if("gg" %in% class(object)){
utils::write.csv(x = object$data,
file = paste0(dir,
"/",
file_name,
".csv"))
file = file.path(dir,
paste0(file_name,".csv")))
} else {
warning("save_data only works with ggplot graph objects. Your data has not been saved.")
}
Expand Down
21 changes: 19 additions & 2 deletions R/make_slide.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @importFrom rmarkdown render
#' @importFrom knitr opts_chunk
#' @importFrom tools file_path_sans_ext
#' @importFrom rmarkdown pandoc_version pandoc_available
#'
#' @examples
#'
Expand All @@ -30,7 +31,7 @@
#' # By default, make_slide() will use the last plot you generated; you can specify a
#' # different object to use with the `graph` argument.
#'
#' make_slide(filename = "test.pptx")
#' \donttest{make_slide(filename = "test.pptx")}
#'
#' @export

Expand All @@ -40,6 +41,8 @@ make_slide <- function(graph = last_plot(),
path = ".",
type = "16:9"){

pandoc_test()

if(!"gg" %in% class(graph)){
stop("The object is not a ggplot2 graph and cannot be plotted with make_slide()")
}
Expand Down Expand Up @@ -206,7 +209,8 @@ make_slide <- function(graph = last_plot(),
#'
#' # Now, create a Powerpoint presentation with one slide per graph in your list:
#'
#' make_presentation(graphs, filename = "test.pptx")
#' \donttest{make_presentation(graphs, filename = "test.pptx")}
#'
#' @export


Expand All @@ -217,6 +221,8 @@ make_presentation <- function(graphs,
title = NULL,
subtitle = NULL){

pandoc_test()

if(is.null(filename)){
stop("You must specify a filename (such as 'my_presentation') for the Powerpoint presentation you wish to create.")
}
Expand Down Expand Up @@ -379,3 +385,14 @@ make_presentation <- function(graphs,

}

pandoc_test <- function(){

if(!rmarkdown::pandoc_available()){
stop("To use this function, you must install 'pandoc' on your system from pandoc.org. See the grattantheme administrators if you need help.")
}

if(rmarkdown::pandoc_version() < 2.1){
stop("The version of 'pandoc' on your system is too old to use this function. Install a newer version from pandoc.org. See the grattantheme administrators if you need help.")
}

}
Binary file added inst/extdata/rmd-word-template.docx
Binary file not shown.
5 changes: 3 additions & 2 deletions man/make_slide.Rd

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

64 changes: 64 additions & 0 deletions tests/testthat/test-theme_grattan.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
context("Check that theme_grattan() and associated functions work")

# Create base object to use for test
base_plot <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point()

p <- base_plot + theme_grattan()

test_that("theme_grattan() is a theme obkect", {
expect_is(theme_grattan(), "theme")
})

test_that("plot with theme_grattan() is a ggplot2 object",{
expect_is(p, "gg")
})

test_that("theme conforms with style guide in key ways",{
expect_equal(p$theme$text$size, 18)

expect_equal(p$theme$line$colour, "#C3C7CB")

expect_equal(p$theme$rect$fill, "white")

})

test_that("theme_grattan() arguments work",{
p_flipped <- base_plot + theme_grattan(flipped = TRUE)

expect_equal(attr(p_flipped$theme$panel.grid.major.x, "class")[1], "element_line")

p_orange <- base_plot + theme_grattan(background = "orange")

expect_equal(p_orange$theme$rect$fill, "#FEF0DE")

expect_equal(p$theme$legend.position, "none")

p_legend <- base_plot + theme_grattan(legend = "top")

expect_equal(p_legend$theme$legend.position, "top")

})

test_that("grattan colour functions work as expected", {

expect_length(grattan_pal(), 5)

expect_length(grattan_pal(n = 2), 2)

expect_length(grattan_pal(n = 10), 10)

expect_warning(grattan_pal(n = 10))

expect_error(grattan_pal(n = 11))

plot_w_col <- base_plot +
geom_point(aes(col = factor(cyl))) +
grattan_colour_manual(n = 3)

expect_is(plot_w_col, "gg")

plot_w_col_built <- ggplot_build(plot_w_col)

expect_equal(length(unique(plot_w_col_built$data[[2]]$colour)), 3)
})
26 changes: 21 additions & 5 deletions tests/testthat/test_make_slide.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
context("Powerpoint slide creation")

library(grattantheme)
library(ggplot2)

# Create base object to use for test
base_plot <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point()

p <- base_plot + theme_grattan()

p_labs <- p + labs(title = "Title", subtitle = "Subtitle", caption = "Notes: Source:")

p_labs_2 <- p_labs + labs(title = "This is my second slide")

plot_list <- list(p_labs, p_labs_2)

# Tests
test_that("theme_grattan() creates ggplot2 object",{
expect_is(p, "gg")
})

test_that("make_slide() saves .pptx object",{

p_labs <- p + labs(title = "Title", subtitle = "Subtitle", caption = "Notes: Source:")
skip_if_not(pandoc_version() >= 2.1)

make_slide(graph = p_labs, filename = "testslide.pptx")

expect_that(file.exists("./testslide.pptx"), equals(TRUE))
expect_true(file.exists("./testslide.pptx"))

file.remove("./testslide.pptx")

})

test_that("make_presentation() saves .pptx object",{

skip_if_not(pandoc_version() >= 2.1)

make_presentation(plot_list, filename = "testpresentation.pptx",
title = "Test presentation", subtitle = "Subtitle")

expect_true(file.exists("./testpresentation.pptx"))

file.remove("./testpresentation.pptx")

})

0 comments on commit 772f2e6

Please sign in to comment.