Skip to content

Commit

Permalink
Added Beers Law lab and chem_scatter
Browse files Browse the repository at this point in the history
  • Loading branch information
ismayc committed Jul 19, 2016
0 parents commit 4d09496
Show file tree
Hide file tree
Showing 44 changed files with 1,891 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Rproj.user
.Rhistory
.RData
21 changes: 21 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Package: chemistr
Title: Facilitates use of R functions and R Markdown files for Chemistry lab
reports
Version: 0.0.0.9000
Authors@R: person("Chester", "Ismay", email = "chester.ismay@gmail.com", role = c("aut", "cre"))
Description: This package is designed to assist students with little to no
programming experience create reproducible analyses using R and R Markdown.
One goal is to reduce the sometimes large amount of arguments pass into
functions, while also allowing students to be able to customize as they
wish. Another goal is to add error messages into R when, say, a percent
sign is used in a LaTeX table title to assist with debugging.
Depends:
R (>= 3.2.0)
Imports:
dplyr,
xtable,
ggplot2
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 5.0.1
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(Beers_Law)
export(chem_scatter)
25 changes: 25 additions & 0 deletions R/chem_lab.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' Creates an R Markdown PDF document for the Beers Law Lab
#'
#' This is a function called in output in the YAML of the driver Rmd file to
#' specify using a tweaked version of the default LaTeX pandoc template.
#'
#' @export
#' @param fig_height A positive numeric giving the default height of images
#' @param fig_width A positive numeric giving the default width of images
#' @return A modified \code{pdf_document} based on slightly modified knitr
#' LaTeX default file
#' @examples
#' \dontrun{
#' output:
#' chemistr::Beers_Law
#' }
Beers_Law <- function(fig_height = 2.5, fig_width = 5){
template <- find_resource("Beers_Law", "template.tex")

base <- rmarkdown::pdf_document(template = template,
fig_caption = TRUE,
fig_height = fig_height,
fig_width = fig_width,
keep_tex = TRUE)
base
}
57 changes: 57 additions & 0 deletions R/chem_scatter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' Creates a scatterplot with regression line
#'
#'
#' @export
#' @param data A data frame containing variables to be plotted
#' @param xvar The name of the x-variable
#' @param yvar The name of the y-variable
#' @param xlab A string containing the x-axis label
#' @param ylab A string containing the y-axis label
#' @return A plot
#' @examples
#' \dontrun{
#' chem_scatter(iris, Sepal.Width, Sepal.Length)
#' }

chem_scatter <- function(data, xvar, yvar,
xlab = "Insert X-axis label",
ylab = "Insert Y-axis label",
gg = TRUE, intercept = FALSE){
data_gd <- NULL
data_gd$xvar <- tryCatch(
expr = lazyeval::lazy_eval(substitute(xvar), data = data),
error = function(e) eval(envir = data, expr = parse(text = xvar))
)
data_gd$yvar <- tryCatch(
expr = lazyeval::lazy_eval(substitute(yvar), data = data),
error = function(e) eval(envir = data, expr = parse(text = yvar))
)
if(gg == TRUE){
# if(intercept == FALSE){
ggplot(data = as.data.frame(data_gd), mapping = aes(x = xvar, y = yvar)) +
geom_point() +
theme_bw() +
geom_smooth(method = "lm", se = FALSE, color = "black", size = 0.3,
formula = if(intercept == FALSE){y ~ x + 0} else{y ~ x}) +
labs(x = xlab, y = ylab)
#}
# else{
# ggplot(data = as.data.frame(data_gd), mapping = aes(x = xvar, y = yvar)) +
# geom_point() +
# theme_bw() +
# geom_smooth(method = "lm", se = FALSE, color = "black", size = 0.3,
# formula = y ~ x) +
# labs(x = xlab, y = ylab)}
}
else{
plot(yvar ~ xvar + 0, data = data,
xlab = "Insert X-axis label",
ylab = "Insert Y-axis label",
pch = 20)
if(intercept == FALSE)
abline(lm(yvar ~ xvar + 0))
else
abline(lm(yvar ~ xvar))

}
}
78 changes: 78 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
find_file <- function(template, file) {
template <- system.file("rmarkdown", "templates", template, "skeleton",
file,
package = "chemistr")
if (template == "") {
stop("Couldn't find template file ", template, "/", file, call. = FALSE)
}

template
}

find_resource <- function(template, file) {
find_file(template, file.path("../resources", file))
}

knitr_fun <- function(name) utils::getFromNamespace(name, 'knitr')

output_asis <- knitr_fun('output_asis')

#' Render a pandoc template.
#'
#' This is a hacky way to access the pandoc templating engine.
#'
#' @param metadata A named list containing metadata to pass to template.
#' @param template Path to a pandoc template.
#' @param output Path to save output.
#' @return (Invisibly) The path of the generate file.
#' @examples
#' x <- rticles:::template_pandoc(
#' list(preamble = "%abc", filename = "wickham"),
#' rticles:::find_resource("rjournal_article", "RJwrapper.tex"),
#' tempfile()
#' )
#' if (interactive()) file.show(x)
#' @noRd
template_pandoc <- function(metadata, template, output, verbose = FALSE) {
tmp <- tempfile(fileext = ".md")
on.exit(unlink(tmp))

cat("---\n", file = tmp)
cat(yaml::as.yaml(metadata), file = tmp, append = TRUE)
cat("---\n", file = tmp, append = TRUE)
cat("\n", file = tmp, append = TRUE)

rmarkdown::pandoc_convert(tmp, "markdown", output = output,
options = paste0("--template=", template), verbose = verbose)

invisible(output)
}


# Call rmarkdown::pdf_document and mark the return value as inheriting pdf_document
inherit_pdf_document <- function(...) {
fmt <- rmarkdown::pdf_document(...)
fmt$inherits <- "pdf_document"
fmt
}

# Helper function to create a custom format derived from pdf_document
# that includes a custom LaTeX template and custom CSL definition
pdf_document_format <- function(..., format, template, csl) {

# base format
fmt <- inherit_pdf_document(..., template = find_resource(format, template))

# add csl to pandoc_args
fmt$pandoc$args <- c(fmt$pandoc$args,
"--csl",
rmarkdown::pandoc_path_arg(find_resource(format, csl)))


# return format
fmt
}




21 changes: 21 additions & 0 deletions chemistr.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
Loading

0 comments on commit 4d09496

Please sign in to comment.