Skip to content

Commit

Permalink
Tests: New test of knitting bare skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
crsh committed Feb 12, 2016
1 parent 3c822b5 commit 171f3aa
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -19,3 +19,4 @@ vignettes/*.pdf
*~
.DS_Store
Thumbs.db
.Rproj.user
4 changes: 3 additions & 1 deletion DESCRIPTION
Expand Up @@ -2,7 +2,7 @@ Package: prereg
Type: Package
Title: An RMarkdown template that facilitates authoring preregistrations of
scientific studies
Version: 0.1
Version: 0.1.0
Date: 2015-12-18
Authors@R: c(person("Frederik", "Aust", email = "frederik.aust@uni-koeln.de", role = c("aut", "cre")))
Maintainer: Frederik Aust, <frederik.aust@uni-koeln.de>
Expand All @@ -15,6 +15,8 @@ Depends:
R (>= 2.11.1)
Imports:
rmarkdown (>= 0.8.1)
Suggests:
testthat
License: MIT + file LICENSE
LazyData: TRUE
RoxygenNote: 5.0.1
29 changes: 18 additions & 11 deletions R/cos_prereg.R
@@ -1,21 +1,28 @@
#' COS Preregistration Challenge template

#' Knit a PDF document
#'
#' @param ... additional arguments to \code{rmarkdown::pdf_document}
#' Knit a PDF document using the COS Preregistration Challenge template
#'
#' @param ... additional arguments to \code{rmarkdown::pdf_document}; \code{template} is ignored.
#' @export

cos_prereg <- function(...) {
ellipsis <- list(...)
if(!is.null(ellipsis$template)) ellipsis$template <- NULL

# Get cos_prereg template
template <- system.file(
template <- system.file(
"rmarkdown", "templates", "cos_prereg", "resources"
, "cos_prereg.tex"
, package = "prereg"
)
if(template == "") stop("No LaTeX template file found.")
if(template == "") stop("No LaTeX template file found.") else ellipsis$template <- template

# Create format
cos_prereg_format <- do.call(rmarkdown::pdf_document, ellipsis)

## Overwrite preprocessor to set correct margin and CSL defaults
cos_prereg_format$pre_processor <- pre_processor

cos_prereg_format <- rmarkdown::pdf_document(template = template, ...)
cos_prereg_format$pre_processor <- prereg:::pre_processor
cos_prereg_format
}

Expand All @@ -28,13 +35,13 @@ pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, o
# save files dir (for generating intermediates)
saved_files_dir <<- files_dir

prereg:::pdf_pre_processor(metadata, input_file, runtime, knit_meta, files_dir, output_dir)
pdf_pre_processor(metadata, input_file, runtime, knit_meta, files_dir, output_dir)
}


pdf_pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir) {
args <- c()

# Set margins if no other geometry options specified
has_geometry <- function(text) {
length(grep("^geometry:.*$", text)) > 0
Expand All @@ -46,7 +53,7 @@ pdf_pre_processor <- function(metadata, input_file, runtime, knit_meta, files_di
, "--variable", "geometry:top=1.25in"
, "--variable", "geometry:right=1in"
)

# Use APA6 CSL citations template if no other file is supplied
has_csl <- function(text) {
length(grep("^csl:.*$", text)) > 0
Expand All @@ -60,6 +67,6 @@ pdf_pre_processor <- function(metadata, input_file, runtime, knit_meta, files_di
if(csl_template == "") stop("No CSL template file found.")
args <- c(args, c("--csl", rmarkdown::pandoc_path_arg(csl_template)))
}

args
}
2 changes: 2 additions & 0 deletions tests/test_prereg.R
@@ -0,0 +1,2 @@
require("testthat")
test_check("prereg")
17 changes: 17 additions & 0 deletions tests/testthat/test_skeleton.R
@@ -0,0 +1,17 @@
test_that(
"Knit bare skeleton"
, {
# Render skeleton
rmarkdown::draft(
"bare_skeleton.Rmd"
, "cos_prereg"
, package = "prereg"
, create_dir = FALSE
, edit = FALSE
)
rmarkdown::render("bare_skeleton.Rmd")

# Clean up
file.remove(list.files(pattern = "bare"))
}
)

0 comments on commit 171f3aa

Please sign in to comment.