\documentclass[a4paper,twoside]{article} \usepackage{mycourse} \newcommand{\Title}{Template} <>= Module <- sub(".Rnw", "", current_input()) cat(paste0("\\newcommand{\\Module}{", Module, "}")) @ \begin{document} % A self paced guide using R. Suggested as a 2 hour workshop. <>= @ \input{crossref.tex} \maketitle\thispagestyle{empty} \label{cha:template} \todo[inline]{Introduce the chapter through two or three paragraphs.} \CiteDate{20180603} This document serves as a template from which all other chapters derive their basic structure. The intent is that when starting a new chapter we begin by taking a copy of this document as the first version of that chapter. Remove the packages in the next section that are not relevant. Some basic examples are also included to copy and paste or remove as appropriate. On taking this document as the first cut version of any new chapter, once copied edit the new document to replace \textit{template:} with something appropriate for the new chapter, as a single word prefix for each code block. \todo[inline]{The chapter consists of a series of sections, each section limited to a single page.} <>= @ %----------------------------------------------------------------------- \section{Packages Used} %----------------------------------------------------------------------- \CiteDate{20180603} Packages used in this chapter include \Sexpr{Rpackage(C50,cite=TRUE)}, \Sexpr{Rpackage(Hmisc,cite=TRUE)}, \Sexpr{Rpackage(ROCR,cite=TRUE)}, \Sexpr{Rpackage(ada,cite=TRUE)}, \Sexpr{Rpackage(caret,cite=TRUE)}, \Sexpr{Rpackage(ckanr,cite=TRUE)}, \Sexpr{Rpackage(diagram,cite=TRUE)}, \Sexpr{Rpackage(directlabels,cite=TRUE)}, \Sexpr{Rpackage(dplyr,cite=TRUE)}, \Sexpr{Rpackage(ggplot2,cite=TRUE)}, \Sexpr{Rpackage(glue,cite=TRUE)}, \Sexpr{Rpackage(lubridate,cite=TRUE)}, \Sexpr{Rpackage(magrittr,cite=TRUE)}, \Sexpr{Rpackage(nnet,cite=TRUE)}, \Sexpr{Rpackage(party,cite=TRUE)}, \Sexpr{Rpackage(pryr,cite=TRUE)}, \Sexpr{Rpackage(randomForest,cite=TRUE)}, \Sexpr{Rpackage(readr,cite=TRUE)}, \Sexpr{Rpackage(rpart,cite=TRUE)}, \Sexpr{Rpackage(rvest,cite=TRUE)}, \Sexpr{Rpackage(scales,cite=TRUE)}, \Sexpr{Rpackage(stringi,cite=TRUE)}, \Sexpr{Rpackage(stringr,cite=TRUE)}, \Sexpr{Rpackage(tibble,cite=TRUE)}, \Sexpr{Rpackage(tidyr,cite=TRUE)}, \Sexpr{Rpackage(xtable,cite=TRUE)}, \Sexpr{Rpackage(tidyverse,cite=TRUE)}, and \Sexpr{Rpackage(rattle,cite=TRUE)}. Packages are loaded here into the \R{} session from the local library folders using \Sexpr{Rfunction(library)}. Any packages that are missing can be installed using \Sexpr{Rfunction(install.packages)}. <>= # Load required packages from local library into the R session. library(C50) # Original C5.0 decision tree builder. library(Hmisc) # Escape special LaTeX charaters. library(ROCR) # Use prediction() for evaluation. library(ada) # Model: Adaptive boosting ada(). library(caret) # Unified model builder. library(ckanr) # Access data from CKAN. library(diagram) # Produce a flowchart. library(directlabels) # Dodging labels for ggplot2. library(dplyr) # Wrangling: tbl_df(), group_by(), print(). library(glue) # Format strings. library(ggplot2) # Visualise data. library(grid) # Layout of plots: viewport(). library(lubridate) # Dates and time. library(magrittr) # Data pipelines: %>% %<>% %T>% equals(). library(nnet) # Neural network model builder. library(party) # Conditional trees ctree() cforest(). library(pryr) # Meta tools: object_size(), mem_used(). library(randomForest) # Model: randomForest() na.roughfix() for missing data. library(rattle) # Support: normVarNames(), riskchart(), errorMatrix(). library(readr) # Modern and efficient data reader. library(readxl) # Read Excel spreadsheets: read_excel(). library(rpart) # Model: decision tree rpart(). library(rvest) # Scrape data from the Internet. library(scales) # commas(), percent(). library(stringi) # String concat operator: %s+%. library(stringr) # str_replace_all(). library(tibble) # Table data frame: rownames_to_column(), glimpse(). library(tidyr) # Tidy the dataset: gather(). library(xtable) # Generate LaTeX tables. library(tidyverse) # ggplot2, tibble, tidyr, readr, purr, dplyr, stringr @ <>= # These are dependencies that would otherwise be attached as required. library(Hmisc) # Escape special LaTeX characters. library(diagram) # Produce a flowchart. library(stringi) # The string concat operator %s+%. library(xtable) # Format R data frames as LaTeX tables. @ <>= @ \begin{TODO} %----------------------------------------------------------------------- \section{ToDo} %----------------------------------------------------------------------- \CiteDate{20180603} \todo[inline]{As separate todo items list what else to be included or to be finished for this chapter. It is by default not generated in the visible documents by virtue of being enclosed in the TODO environment.} \end{TODO} %----------------------------------------------------------------------- \section{Sample Template} %----------------------------------------------------------------------- The purpose of this template is to provide a starting point for writing a new chapter for the One Pager Data Science series. Copy the file and rename it appropriately. This template also serves to capture all style decisions. Here is an example of some \R{} code using the \Sexpr{Rfunction(summary)} command and noting that the output is truncated. <>= summary(iris) @ And here we show the output from the more modern \Sexpr{Rfunction(glimpse)} function, displaying all the output. <>= glimpse(iris) @ %----------------------------------------------------------------------- \section{Sample Graphic} %----------------------------------------------------------------------- <>= iris %>% ggplot(aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) + geom_point() @ \CiteDate{20180603} Here we illustrate the inclusion of a graphic using \Sexpr{Rfunction(ggplot)}. It may be appealing to include the actual graphic at the top of the page, followed by a narrative, and then followed by the actual code to generate the graphic. The narrative itself might incorporate a description of the code, or restrict itself to interpreting the story the graphic tells. Alternatively the description of the code may come after the code listing itself. <>= @ %----------------------------------------------------------------------- \section{Command Summary} %----------------------------------------------------------------------- \todo[inline]{At the end of the chapter include a summary of the commands introduced in this chapter.} \CiteDate{20180603} This chapter has introduced, demonstrated and described the following \R{} packages, functions, commands, operators, and datasets: \\[2mm] \begin{outdent} \textbf{\Sexpr{Rfunction(ggplot, Defn=TRUE)}} \textit{Function from the \Sexpr{Rfunction(ggplot, Pkg=TRUE)} package}. Construct the canvas for a new plot without yet adding anything to the canvas. \textbf{\Sexpr{Rfunction(glimpse, Defn=TRUE)}} \textit{Function from the \Sexpr{Rfunction(glimpse, Pkg=TRUE)} package}. Generate a brief view of the dataset. \textbf{\Sexpr{Rfunction(install.packages, Defn=TRUE)}} \textit{Function from the \Sexpr{Rfunction(install.packages, Pkg=TRUE)} package}. Install the specified package from an appropriate R repository. The package is installed locally into a specific folder which we refer to in \R{} as a library. \textbf{\Sexpr{Rfunction(library, Defn=TRUE)}} \textit{Function from the \Sexpr{Rfunction(library, Pkg=TRUE)} package}. Load the named package from a local library (folder) into the \R{} session. \textbf{\Sexpr{Rfunction(summary, Defn=TRUE)}} \textit{Function from the \Sexpr{Rfunction(summary, Pkg=TRUE)} package}. Generate a summary of the basic statistical distribution of the dataset. \end{outdent} %----------------------------------------------------------------------- \section{Exercises} %----------------------------------------------------------------------- \todo[inline]{Create a set of exercises to review the chapter material and develop beyond what has been covered.} \CiteDate{20180603} \begin{Exercise}[title={A title for the table of contents.}] A relatively straightforward exercise to build up some confidence. \end{Exercise} \begin{Exercise}[title={Another title for the table of contents.}] Each is increasingly complex and challenging, taking it slowly to get to the most difficult. \end{Exercise} %----------------------------------------------------------------------- \section{Further Reading and Acknowledgements} \label{sec:template:further} %----------------------------------------------------------------------- \rattlereading \todo[inline]{List any other resources of use that extend beyond what we have presented in this chapter.} Other resources include: \begin{itemize} \item {Under development.} \end{itemize} %----------------------------------------------------------------------- <>= @ %----------------------------------------------------------------------- \end{document}