Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #89

Merged
merged 10 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: thesisdown
Title: An updated R Markdown thesis template using the bookdown package
Version: 0.0.3
Version: 0.1.0
Authors@R: c(
person("Chester", "Ismay", email = "chester.ismay@gmail.com", role = "cre"),
person("Nick", "Solomon", email = "nick@nicksolomon.me", role = "aut"))
Expand All @@ -15,9 +15,10 @@ Depends:
dplyr,
ggplot2,
bookdown,
knitr
knitr,
here
License: MIT
Encoding: UTF-8
LazyData: true
SystemRequirements: pandoc (>= 1.18) - http://pandoc.org
RoxygenNote: 7.0.2
RoxygenNote: 7.1.0
70 changes: 39 additions & 31 deletions R/thesis.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,40 @@
#'
#' @export
#'
#' @param toc A Boolean (TRUE or FALSE) specifying whether table of contents should be created
#' @param toc A Boolean (TRUE or FALSE) specifying whether table of contents
#' should be created
#' @param toc_depth A positive integer
#' @param ... Further arguments passed to or from other methods.
#' @param highlight Syntax highlighting style. Supported styles include "default", "tango", "pygments", "kate", "monochrome", "espresso", "zenburn", and "haddock". Pass NULL to prevent syntax highlighting.
#' @param highlight Syntax highlighting style. Supported styles include
#' "default", "tango", "pygments", "kate", "monochrome", "espresso", "zenburn",
#' and "haddock". Pass NULL to prevent syntax highlighting.
#'
#' @return A modified \code{pdf_document} based on the Reed Senior Thesis LaTeX
#' template
#' @examples
#' \dontrun{
#' output: thesisdown::thesis_pdf
#' output:thesisdown::thesis_pdf
#' }
thesis_pdf <- function(toc = TRUE, toc_depth = 3, highlight = "default", ...){

base <- bookdown::pdf_book(template = "template.tex",
thesis_pdf <- function(toc = TRUE, toc_depth = 3, highlight = "default", ...) {
base <- bookdown::pdf_book(
template = "template.tex",
toc = toc,
toc_depth = toc_depth,
highlight = highlight,
keep_tex = TRUE,
pandoc_args = "--top-level-division=chapter",
...)
...
)

# Mostly copied from knitr::render_sweave
base$knitr$opts_chunk$comment <- NA
#base$knitr$opts_chunk$fig.align <- "center"
# base$knitr$opts_chunk$fig.align <- "center"

old_opt <- getOption("bookdown.post.latex")
options(bookdown.post.latex = fix_envs)
on.exit(options(bookdown.post.late = old_opt))

base

}

#' Creates an R Markdown gitbook Thesis document
Expand All @@ -49,14 +52,19 @@ thesis_pdf <- function(toc = TRUE, toc_depth = 3, highlight = "default", ...){
#' @return A gitbook webpage
#' @examples
#' \dontrun{
#' output: thesisdown::thesis_gitbook
#' output:thesisdown::thesis_gitbook
#' }
thesis_gitbook <- function(...){

thesis_gitbook <- function(...) {
config_default <- list(
toc = list(collapse = "section",
before = '<li><a href="./"></a></li>',
after = '<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>'))
toc = list(
collapse = "section",
before = '<li><a href="./"></a></li>',
after = paste(
'<li><a href="https://github.com/rstudio/bookdown',
'target="blank">Published with bookdown</a></li>'
)
)
)

listarg <- list(...)

Expand Down Expand Up @@ -89,7 +97,6 @@ thesis_gitbook <- function(...){
base$knitr$opts_chunk$fig.align <- "center"

base

}

#' Creates an R Markdown Word Thesis document
Expand All @@ -104,18 +111,16 @@ thesis_gitbook <- function(...){
#' the Reed Senior Thesis Word template
#' @examples
#' \dontrun{
#' output: thesisdown::thesis_word
#' output:thesisdown::thesis_word
#' }
thesis_word <- function(...){

thesis_word <- function(...) {
base <- bookdown::word_document2(...)

# Mostly copied from knitr::render_sweave
base$knitr$opts_chunk$comment <- NA
base$knitr$opts_chunk$fig.align <- "center"

base

}

#' Creates an R Markdown epub Thesis document
Expand All @@ -129,28 +134,31 @@ thesis_word <- function(...){
#' @return A ebook version of the thesis
#' @examples
#' \dontrun{
#' output: thesisdown::thesis_epub
#' output:thesisdown::thesis_epub
#' }
thesis_epub <- function(...){

thesis_epub <- function(...) {
base <- bookdown::epub_book(...)

# Mostly copied from knitr::render_sweave
base$knitr$opts_chunk$comment <- NA
base$knitr$opts_chunk$fig.align <- "center"

base

}

fix_envs = function(x){
beg_reg <- '^\\s*\\\\begin\\{.*\\}'
end_reg <- '^\\s*\\\\end\\{.*\\}'
i3 = if (length(i1 <- grep(beg_reg, x))) (i1 - 1)[grepl("^\\s*$", x[i1 - 1])]
fix_envs <- function(x) {
beg_reg <- "^\\s*\\\\begin\\{.*\\}"
end_reg <- "^\\s*\\\\end\\{.*\\}"
i3 <- if (length(i1 <- grep(beg_reg, x))) {
(i1 - 1)[grepl("^\\s*$", x[i1 - 1])]
}

i3 = c(i3,
if (length(i2 <- grep(end_reg, x))) (i2 + 1)[grepl("^\\s*$", x[i2 + 1])]
i3 <- c(
i3,
if (length(i2 <- grep(end_reg, x))) {
(i2 + 1)[grepl("^\\s*$", x[i2 + 1])]
}
)
if (length(i3)) x = x[-i3]
if (length(i3)) x <- x[-i3]
x
}
2 changes: 1 addition & 1 deletion R/thesisdown.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' thesisdown: A package for creating undergraduate, Masters, and PhD theses
#'using R Markdown
#' using R Markdown
#'
#'
#' @section thesis_gitbook:
Expand Down
Empty file.
39 changes: 24 additions & 15 deletions inst/rmarkdown/templates/thesis/skeleton/01-chap1.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ pkg <- c("dplyr", "ggplot2", "knitr", "bookdown", "devtools")
new.pkg <- pkg[!(pkg %in% installed.packages())]
# If there are any packages in the list that aren't installed,
# install them
if (length(new.pkg))
if (length(new.pkg)) {
install.packages(new.pkg, repos = "http://cran.rstudio.com")
}
# Load packages (thesisdown will load all of the packages as well)
library(thesisdown)
```
Expand All @@ -135,22 +136,23 @@ The example we show here does the following:
- Using `flights2`, we determine the largest arrival delay for each of the carriers.

```{r max_delays}
flights2 <- flights %>%
flights2 <- flights %>%
select(carrier_name, arr_delay)
max_delays <- flights2 %>%
max_delays <- flights2 %>%
group_by(carrier_name) %>%
summarize(max_arr_delay = max(arr_delay, na.rm = TRUE))
```

A useful function in the `knitr` package for making nice tables in _R Markdown_ is called `kable`. It is much easier to use than manually entering values into a table by copying and pasting values into Excel or LaTeX. This again goes to show how nice reproducible documents can be! (Note the use of `results="asis"`, which will produce the table instead of the code to create the table.) The `caption.short` argument is used to include a shorter title to appear in the List of Tables.

```{r maxdelays, results="asis"}
kable(max_delays,
col.names = c("Airline", "Max Arrival Delay"),
caption = "Maximum Delays by Airline",
caption.short = "Max Delays by Airline",
longtable = TRUE,
booktabs = TRUE)
kable(max_delays,
col.names = c("Airline", "Max Arrival Delay"),
caption = "Maximum Delays by Airline",
caption.short = "Max Delays by Airline",
longtable = TRUE,
booktabs = TRUE
)
```

The last two options make the table a little easier-to-read.
Expand All @@ -159,17 +161,24 @@ We can further look into the properties of the largest value here for American A


```{r max_props}
flights %>% filter(arr_delay == 1539,
carrier_name == "American Airlines Inc.") %>%
select(-c(month, day, carrier, dest_name, hour,
minute, carrier_name, arr_delay))
flights %>%
filter(
arr_delay == 1539,
carrier_name == "American Airlines Inc."
) %>%
select(-c(
month, day, carrier, dest_name, hour,
minute, carrier_name, arr_delay
))
```

We see that the flight occurred on March 3rd and departed a little after 2 PM on its way to Dallas/Fort Worth. Lastly, we show how we can visualize the arrival delay of all departing flights from Portland on March 3rd against time of departure.

```{r march3plot, fig.height=3, fig.width=6}
flights %>% filter(month == 3, day == 3) %>%
ggplot(aes(x = dep_time, y = arr_delay)) + geom_point()
flights %>%
filter(month == 3, day == 3) %>%
ggplot(aes(x = dep_time, y = arr_delay)) +
geom_point()
```

## Additional resources
Expand Down
74 changes: 62 additions & 12 deletions inst/rmarkdown/templates/thesis/skeleton/03-chap3.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,67 @@
# installed and loaded. This thesisdown package includes
# the template files for the thesis and also two functions
# used for labeling and referencing
if(!require(devtools))
install.packages("devtools", repos = "http://cran.rstudio.com")
if(!require(dplyr))
install.packages("dplyr", repos = "http://cran.rstudio.com")
if(!require(ggplot2))
install.packages("ggplot2", repos = "http://cran.rstudio.com")
if(!require(ggplot2))
install.packages("bookdown", repos = "http://cran.rstudio.com")
if(!require(thesisdown)){
library(devtools)
devtools::install_github("ismayc/thesisdown")
if (!require(remotes)) {
if (params$`Install needed packages for {thesisdown}`) {
install.packages("remotes", repos = "https://cran.rstudio.com")
} else {
stop(
paste(
'You need to run install.packages("remotes")',
"first in the Console."
)
)
}
}
if (!require(dplyr)) {
if (params$`Install needed packages for {thesisdown}`) {
install.packages("dplyr", repos = "https://cran.rstudio.com")
} else {
stop(
paste(
'You need to run install.packages("dplyr")',
"first in the Console."
)
)
}
}
if (!require(ggplot2)) {
if (params$`Install needed packages for {thesisdown}`) {
install.packages("ggplot2", repos = "https://cran.rstudio.com")
} else {
stop(
paste(
'You need to run install.packages("ggplot2")',
"first in the Console."
)
)
}
}
if (!require(bookdown)) {
if (params$`Install needed packages for {thesisdown}`) {
install.packages("bookdown", repos = "https://cran.rstudio.com")
} else {
stop(
paste(
'You need to run install.packages("bookdown")',
"first in the Console."
)
)
}
}
if (!require(thesisdown)) {
if (params$`Install needed packages for {thesisdown}`) {
remotes::install_github("ismayc/thesisdown")
} else {
stop(
paste(
"You need to run",
'remotes::install_github("ismayc/thesisdown")',
"first in the Console."
)
)
}
}
library(thesisdown)
flights <- read.csv("data/flights.csv")
```
Expand Down Expand Up @@ -77,7 +126,8 @@ Here is a reference to the Reed logo: Figure \@ref(fig:reedlogo). Note the use
Below we will investigate how to save the output of an **R** plot and label it in a way similar to that done above. Recall the `flights` dataset from Chapter \@ref(rmd-basics). (Note that we've shown a different way to reference a section or chapter here.) We will next explore a bar graph with the mean flight departure delays by airline from Portland for 2014. Note also the use of the `scale` parameter which is discussed on the next page.

```{r delaysboxplot, warnings=FALSE, messages=FALSE, fig.cap="Mean Delays by Airline", fig.width=6}
flights %>% group_by(carrier) %>%
flights %>%
group_by(carrier) %>%
summarize(mean_dep_delay = mean(dep_delay)) %>%
ggplot(aes(x = carrier, y = mean_dep_delay)) +
geom_bar(position = "identity", stat = "identity", fill = "red")
Expand Down
6 changes: 3 additions & 3 deletions inst/rmarkdown/templates/thesis/skeleton/05-appendix.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

`r if(!knitr:::is_latex_output()) '# (APPENDIX) Appendix {-}'`


# The First Appendix

<!--
If you feel it necessary to include an appendix, it goes here.
-->


# The First Appendix

This first appendix includes all of the R chunks of code that were hidden throughout the document (using the `include = FALSE` chunk tag) to help with readibility and/or setup.

**In the main Rmd file**
Expand Down
7 changes: 5 additions & 2 deletions inst/rmarkdown/templates/thesis/skeleton/_bookdown.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
book_filename: "thesis"
chapter_name: "Chapter "
rmd_files: ["index.Rmd", "01-chap1.Rmd", "02-chap2.Rmd", "03-chap3.Rmd", "04-conclusion.Rmd", "05-appendix.Rmd", "99-references.Rmd"]
download: [["thesis.pdf", "PDF"], ["thesis.epub", "EPUB"], ["thesis.docx", "Word"]]
download: [
["thesis.pdf", "PDF"],
["thesis.epub", "EPUB"],
["thesis.docx", "Word"]
]
delete_merged_file: true
3 changes: 1 addition & 2 deletions inst/rmarkdown/templates/thesis/skeleton/reedthesis.cls
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@
%\setlength{\oddsidemargin}{.6in}
%\setlength{\evensidemargin}{0in}
%\setlength{\textwidth}{5.9in}
%\setlength\topmargin{0in}
%\addtolength\headheight{2.5pt}
\addtolength\headheight{2.5pt}
%\addtolength\topmargin{-\headheight}
%\addtolength\topmargin{-\headsep}
%\addtolength\textheight{1in}
Expand Down
Loading