#LyX 2.3 created this file. For more info see http://www.lyx.org/
\lyxformat 544
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass scrartcl
\begin_preamble
% \VignetteIndexEntry{rnw2pdf-report-sweave}
% \VignetteEngine{Sweave}
% \VignetteBuilder{stationery::rnw2pdf}
% Don't remove next commented line, its needed to fool Sweave
%\usepackage{Sweave}

\usepackage[includehead,includefoot,
lmargin=1in,
rmargin=1in,
tmargin=0.5in,
bmargin=1.0in,
headheight=0pt,
headsep=0pt,
marginparwidth=0pt,
footskip=1.5\baselineskip,
]{geometry}

\usepackage{multicol}
\usepackage{dcolumn}
\usepackage{booktabs}


\input{theme/reportPreambleHeader.tex}
\input{theme/preambleFooter.tex}
%% Load Hyperref, don't let LyX do this
\input{theme/reportPreambleSweavel.tex}

\usepackage{fancyvrb}
\fvset{listparameters={\setlength{\topsep}{0em}}}

\usepackage{siunitx}
\end_preamble
\use_default_options false
\begin_modules
logicalmkup
sweave
\end_modules
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding utf8
\fontencoding global
\font_roman "lmodern" "default"
\font_sans "lmss" "default"
\font_typewriter "lmtt" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\use_microtype false
\use_dash_ligatures true
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize 11
\spacing single
\use_hyperref true
\pdf_bookmarks true
\pdf_bookmarksnumbered false
\pdf_bookmarksopen false
\pdf_bookmarksopenlevel 1
\pdf_breaklinks false
\pdf_pdfborder false
\pdf_colorlinks false
\pdf_backref false
\pdf_pdfusetitle true
\papersize letterpaper
\use_geometry false
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 0
\use_package esint 1
\use_package mathdots 0
\use_package mathtools 0
\use_package mhchem 0
\use_package stackrel 0
\use_package stmaryrd 0
\use_package undertilde 0
\cite_engine natbib
\cite_engine_type authoryear
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date true
\justification true
\use_refstyle 0
\use_minted 0
\branch R
\selected 0
\filename_suffix 0
\color #faf0e6
\end_branch
\index Index
\shortcut idx
\color #008000
\end_index
\leftmargin 1in
\topmargin 1in
\rightmargin 1in
\bottommargin 1in
\secnumdepth 3
\tocdepth 3
\paragraph_separation skip
\defskip medskip
\is_math_indent 0
\math_numbering_side default
\quotes_style english
\dynamic_quotes 0
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Standard
\noindent
\begin_inset ERT
status open

\begin_layout Plain Layout

%% Fill in values of the arguments here, 
\end_layout

\begin_layout Plain Layout

%% If blanks are needed, must insert value " ~ "
\end_layout

\begin_layout Plain Layout

%% If comma needed inside value, wrap in {}.
\end_layout

\begin_layout Plain Layout

%% Delete secondauthor and thirdauthor if not needed
\end_layout

\begin_layout Plain Layout


\backslash
reportsetup{%
\end_layout

\begin_layout Plain Layout

  author={
\end_layout

\begin_layout Plain Layout

    lastname=Johnson,
\end_layout

\begin_layout Plain Layout

    firstname=Paul~E., 
\end_layout

\begin_layout Plain Layout

    affiliation=Director{,}~CRMDA,
\end_layout

\begin_layout Plain Layout

    email=pauljohn@ku.edu},
\end_layout

\begin_layout Plain Layout

  title={Instructions for authoring reports with Sweave code chunks},
\end_layout

\begin_layout Plain Layout

  rightlogo={theme/logo.pdf},
\end_layout

\begin_layout Plain Layout

}
\end_layout

\begin_layout Plain Layout


\backslash
reporthdr
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

% 1.
 If you have an address file, use it here:
\end_layout

\begin_layout Plain Layout


\backslash
input{addressFooter.tex}
\end_layout

\begin_layout Plain Layout

% 2.
 If you don't have an address file, remove % signs and fill in your information
\end_layout

\begin_layout Plain Layout

% This stanza must end with 
\begin_inset Quotes eld
\end_inset


\backslash
guideftr
\begin_inset Quotes erd
\end_inset


\end_layout

\begin_layout Plain Layout

% (after removing percent signs):
\end_layout

\begin_layout Plain Layout

%
\backslash
urlstyle{tt}
\end_layout

\begin_layout Plain Layout

%
\backslash
footersetup{
\end_layout

\begin_layout Plain Layout

%  leftside={
\end_layout

\begin_layout Plain Layout

%    lone={Address line 1}, 
\end_layout

\begin_layout Plain Layout

%    ltwo={Address line 2}, 
\end_layout

\begin_layout Plain Layout

%    lthree={City State Zipcode}},
\end_layout

\begin_layout Plain Layout

%  rightside={
\end_layout

\begin_layout Plain Layout

%    rone={Web: 
\backslash
url{https://crmda.ku.edu}},
\end_layout

\begin_layout Plain Layout

%    rtwo={Email: 
\backslash
href{mailto:you@where.edu}{
\backslash
url{you@where.edu}}},
\end_layout

\begin_layout Plain Layout

%    rthree={Phone: 123-345-5678}}
\end_layout

\begin_layout Plain Layout

%}
\end_layout

\begin_layout Plain Layout

%
\backslash
guideftr
\end_layout

\begin_layout Plain Layout

% 3.
 If you don't want an address footer, just delete this subsection.
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

% Please leave this code
\end_layout

\begin_layout Plain Layout

<<ignoremeRsetup, echo=FALSE, include=FALSE, results=hide>>=
\end_layout

\begin_layout Plain Layout

tdir <- 
\begin_inset Quotes eld
\end_inset

tmpout
\begin_inset Quotes erd
\end_inset


\end_layout

\begin_layout Plain Layout

if(!dir.exists(tdir)) dir.create(tdir, showWarnings=FALSE)
\end_layout

\begin_layout Plain Layout

library(stationery)
\end_layout

\begin_layout Plain Layout

opts.orig <- options()
\end_layout

\begin_layout Plain Layout

options(width=80, prompt=" ", continue="  ")
\end_layout

\begin_layout Plain Layout

options(useFancyQuotes = FALSE) 
\end_layout

\begin_layout Plain Layout

set.seed(12345)
\end_layout

\begin_layout Plain Layout

par.orig <- par(no.readonly=TRUE)
\end_layout

\begin_layout Plain Layout

options(SweaveHooks=list(fig=function() par(ps=10)))
\end_layout

\begin_layout Plain Layout

pdf.options(onefile=FALSE,family="Times",pointsize=10)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\begin_layout Plain Layout

%LaTeX in document options
\end_layout

\begin_layout Plain Layout


\backslash
fvset{listparameters={
\backslash
setlength{
\backslash
topsep}{0em}}}
\end_layout

\begin_layout Plain Layout


\backslash
SweaveOpts{prefix.string=tmpout/t, split=TRUE, ae=FALSE, height=4, width=6}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

% This concerns images in your header!
\end_layout

\begin_layout Plain Layout

<<ignoremeTexcopy, include=FALSE,echo=FALSE>>=
\end_layout

\begin_layout Plain Layout

## Put your desired 
\begin_inset Quotes eld
\end_inset

logo.pdf
\begin_inset Quotes erd
\end_inset

 in the theme folder.
 If
\end_layout

\begin_layout Plain Layout

## you don't, you can either use getFiles() to retrieve
\end_layout

\begin_layout Plain Layout

## one for you, or let blankPDF() create a blank image
\end_layout

\begin_layout Plain Layout

## for you.
  Its your choice.
\end_layout

\begin_layout Plain Layout

## This will retrieve a logo.pdf file if you want
\end_layout

\begin_layout Plain Layout

try(getFiles(
\begin_inset Quotes eld
\end_inset

logo.pdf
\begin_inset Quotes erd
\end_inset

, pkg = "stationery", overwrite = FALSE))
\end_layout

\begin_layout Plain Layout

## If you do not have a file after that, 
\end_layout

\begin_layout Plain Layout

## the following will manufacture a blank image for a placeholder
\end_layout

\begin_layout Plain Layout

if(!file.exists(
\begin_inset Quotes eld
\end_inset

theme/logo.pdf
\begin_inset Quotes erd
\end_inset

)){
\end_layout

\begin_layout Plain Layout

  blankPDF(file = "theme/logo.pdf", height=1, width=3.5, messg = 
\begin_inset Quotes eld
\end_inset


\begin_inset Quotes erd
\end_inset

)
\end_layout

\begin_layout Plain Layout

}
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Abstract
\noindent
This shows how we use R 
\begin_inset CommandInset citation
LatexCommand citep
key "RCore"
literal "true"

\end_inset

 to make report documents using the CRMDA style.
 This is a LaTeX 
\begin_inset Quotes eld
\end_inset

noweb
\begin_inset Quotes erd
\end_inset

 report document using the Sweave code chunk engine.
\end_layout

\begin_layout Section
Introduction
\end_layout

\begin_layout Standard
Create a template (skeleton) document by opening R and running
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<eval=F>>=
\end_layout

\begin_layout Plain Layout

library(stationery)
\end_layout

\begin_layout Plain Layout

initWriteup("rnw2pdf-report-sweave")
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
That will create a folder 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
rnw2pdf-report-sweave
\end_layout

\end_inset

.
 If you do not want a directory to be created, then instead run
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<eval=F>>=
\end_layout

\begin_layout Plain Layout

library(stationery)
\end_layout

\begin_layout Plain Layout

initWriteup("rnw2pdf-report-sweave", dir = 
\begin_inset Quotes eld
\end_inset

.
\begin_inset Quotes erd
\end_inset

)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
The 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
initWriteup
\end_layout

\end_inset

 function will create several files, including 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
skeleton.{lyx, Rnw}
\end_layout

\end_inset

, 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
instructions.{lyx, Rnw}
\end_layout

\end_inset

, as well as some auxiliary input files and a theme folder.
 
\end_layout

\begin_layout Section
Proceed as follows
\end_layout

\begin_layout Standard
If you allowed 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
initWriteup
\end_layout

\end_inset

 to create a directory, rename that directory in a way that will be helpful
 to your project.
 For example, 
\begin_inset Quotes eld
\end_inset

homework-1-2019
\begin_inset Quotes erd
\end_inset

 (anything that helps you remember what is in the write-up).
 
\end_layout

\begin_layout Standard
Decide whether you want to edit the LyX skeleton file (lyx) or the noweb
 skeleton (Rnw) file.
 
\end_layout

\begin_layout Enumerate
Copy the skeleton file to a new name.
 Choose a name that helps you, possibly 
\begin_inset Quotes eld
\end_inset

hw-1-2019.lyx
\begin_inset Quotes erd
\end_inset

 or such.
\end_layout

\begin_layout Enumerate
See the code chunk above named 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
ignoremeTexcopy
\end_layout

\end_inset

.
 That is used to retrieve image files for the header on page 1.
 If you don't have image logo files that you want to use, the last part
 of that chunk will manufacture empty 
\begin_inset Quotes eld
\end_inset

blank
\begin_inset Quotes erd
\end_inset

 pdf logo files.
 It is safe to delete the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
ignoremeTexcopy
\end_layout

\end_inset

 chunk after you have the logo files you want in the theme folder.
 
\end_layout

\begin_layout Enumerate
Compile the document without making other changes.
 For instructions on compiling, see Section 
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Compile"
plural "false"
caps "false"
noprefix "false"

\end_inset

.
\end_layout

\begin_layout Enumerate
Make revisions incrementally.
 Start by inserting your own title and address information.
 Re-compile often to make sure no errors have been made.
 
\end_layout

\begin_layout Standard
Don't make changes that you don't understand in the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
ignoremeRoptions
\end_layout

\end_inset

 code chunk above.
 For goodness sake, don't delete that one.
\end_layout

\begin_layout Section
Check our instructions
\end_layout

\begin_layout Standard
With every skeleton, we also provide an instructions file in the working
 directory.
\end_layout

\begin_layout Standard
There are also several vignettes distributed with the stationery package.
 Please review them.
\end_layout

\begin_layout Enumerate
\begin_inset Quotes eld
\end_inset

stationery
\begin_inset Quotes erd
\end_inset

: the package framework overview
\end_layout

\begin_layout Enumerate
\begin_inset Quotes eld
\end_inset

code_chunks
\begin_inset Quotes erd
\end_inset

: discusses display of code in LaTeX documents with both Sweave and knitr
 chunks
\end_layout

\begin_layout Enumerate
\begin_inset Quotes eld
\end_inset

Rmarkdown
\begin_inset Quotes erd
\end_inset

: about using Markdown with R, an alternative to LaTeX/Noweb.
\end_layout

\begin_layout Enumerate
\begin_inset Quotes eld
\end_inset

HTML Special Features
\begin_inset Quotes erd
\end_inset

: In Markdown intended for HTML output, authors can exploit some features.
 
\end_layout

\begin_layout Section
Compile the document
\begin_inset CommandInset label
LatexCommand label
name "sec:Compile"

\end_inset


\end_layout

\begin_layout Standard
The skeleton file is provided in 2 formats, LyX and Rnw.
 The stationery vignette has an overview of reasons why one might author
 with LyX, or with another program (with the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
Rnw
\end_layout

\end_inset

 file).
 It also explains the compilation process.
 
\end_layout

\begin_layout Standard
Whether one writes the LyX file or the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
Rnw
\end_layout

\end_inset

 file, it is necessary to understand that compiling is a two step process.
\end_layout

\begin_layout Enumerate
knitting: Run R to do the calculations in the R code chunks and write out
 a LaTeX file
\end_layout

\begin_layout Enumerate
compiling: Run a LaTeX program, such as 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
pdflatex
\end_layout

\end_inset

 or 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
xelatex
\end_layout

\end_inset

 or such.
 It is also necessary for a bibliography builder program to make a pass
 through the data.
 The aggregator program texi2pdf will this (if you have it).
\end_layout

\begin_layout Subsection
Working with the LyX file.
\end_layout

\begin_layout Enumerate
Use the LyX editor.
 Use the menus in the LyX editor.
 The file can be compiled to PDF in LyX, just like any other LyX file.
 A LyX module called 
\begin_inset Quotes eld
\end_inset

sweave
\begin_inset Quotes erd
\end_inset

 can be activated, so that compiling to PDF with the pdflatex program runs
 the chunks and builds a PDF.
 
\end_layout

\begin_layout Enumerate
Run LyX from the command line:
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

$ lyx -e pdf2 skeleton.lyx
\end_layout

\end_inset


\end_layout

\end_deeper
\begin_layout Enumerate
Open an R session, run
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

> library(stationery)
\end_layout

\begin_layout Plain Layout

> rnw2pdf("skeleton.lyx", engine = "Sweave") # or .Rnw
\end_layout

\end_inset


\end_layout

\end_deeper
\begin_layout Enumerate
The shell script 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
rnw2pdf.sh
\end_layout

\end_inset

 is provided with the skeleton.
 Run:
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

$ ./rnw2pdf.sh --engine='"Sweave"' skeleton.lyx
\end_layout

\end_inset


\end_layout

\end_deeper
\begin_layout Enumerate
In case you want to track the steps of compiling one by one, open the file
 in LyX.
 Use the pull down menu 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
File
\end_layout

\end_inset

 
\begin_inset Formula $\rightarrow$
\end_inset

 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
Export
\end_layout

\end_inset

 
\begin_inset Formula $\rightarrow$
\end_inset

 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
Rnw (knitr)
\end_layout

\end_inset

.
 That will create a file named 
\begin_inset Quotes eld
\end_inset

skeleton.Rnw
\begin_inset Quotes erd
\end_inset

.
 This is the equivalent of the command line statement
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

$ lyx -e sweave skeleton.lyx
\end_layout

\end_inset


\end_layout

\begin_layout Standard
After that Rnw file is created.
 Proceed as described in the next subsection.
 After that 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
.Rnw
\end_layout

\end_inset

 file is created, run
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

$ R CMD Sweave skeleton.Rnw
\end_layout

\end_inset


\end_layout

\begin_layout Standard
That creates a LaTeX file and errors are usually easy to diagnose.
 This two-step process is valuable for debugging.
\end_layout

\end_deeper
\begin_layout Subsection
Working with the Rnw file
\end_layout

\begin_layout Standard
The Rnw file we provide is produced by LyX.
 It is the traditional way that the R team developed Sweave and how most
 vignettes are made for packages.
\end_layout

\begin_layout Standard
The work flow here will vary, depending on your experience and the editor
 you choose to use.
 Here are some possibilities:
\end_layout

\begin_layout Standard
Our function, by default, will create and R file summary of the command
 chunks.
 This file is referred to as a 
\begin_inset Quotes eld
\end_inset

purled
\begin_inset Quotes erd
\end_inset

 or 
\begin_inset Quotes eld
\end_inset

tangled
\begin_inset Quotes erd
\end_inset

 file.
 
\end_layout

\begin_layout Enumerate
In R, run
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

> library(stationery)
\end_layout

\begin_layout Plain Layout

> rnw2pdf("skeleton.Rnw", engine = "Sweave")
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Additional arguments can be used, mainly to control the verbosity of the
 output to preserve intermediate files.
 Our function, by default, will create and R file summary of the command
 chunks.
 This file is referred to as a 
\begin_inset Quotes eld
\end_inset

purled
\begin_inset Quotes erd
\end_inset

 or 
\begin_inset Quotes eld
\end_inset

tangled
\begin_inset Quotes erd
\end_inset

 file.
 For debugging, we use parameters 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
--verbose=FALSE
\end_layout

\end_inset

 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
--clean=FALSE
\end_layout

\end_inset

.
\end_layout

\end_deeper
\begin_layout Enumerate
The shell script 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
rnw2pdf.sh
\end_layout

\end_inset

 is provided with the skeleton.
 Run
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

$ ./rnw2pdf.sh --engine='"Sweave"' skeleton.Rnw
\end_layout

\end_inset


\end_layout

\begin_layout Standard
The command script answers to all of the arguments followed by the R function
 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
rnw2pdf
\end_layout

\end_inset

.
 The usage is nearly identical.
 Where the R function call would be 
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

> rnw2pdf("skeleton.Rnw", purl = FALSE, clean = FALSE, verbose = TRUE, keep_tex
 = TRUE)
\end_layout

\end_inset


\end_layout

\begin_layout Standard
the shell command would be
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

$ ./rnw2pdf -engine='"Sweave"' --purl=FALSE --clean=FALSE --keep_tex=TRUE
 --verbose=TRUE skeleton.Rnw
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Note the trick to include a parameter 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
"Sweave"
\end_layout

\end_inset

 that is a quoted string.
 The single quotes that are protecting the double quotes in the command
 line.
\end_layout

\end_deeper
\begin_layout Enumerate
This is the old fashioned way (therefore most virtuous for senior R users).
 
\end_layout

\begin_deeper
\begin_layout Enumerate
Step 1:
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

$ R CMD Sweave skeleton.Rnw
\end_layout

\end_inset


\end_layout

\begin_layout Standard
That will create 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
skeleton.tex
\end_layout

\end_inset

.
 
\end_layout

\end_deeper
\begin_layout Enumerate
Step 2:
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

$ texi2pdf skeleton.tex
\end_layout

\end_inset


\end_layout

\begin_layout Standard
The major difference between running this and the script we provide is that
 our script will handle LyX files and it will, by default, create a purled
 copy of the R code.
 
\end_layout

\end_deeper
\end_deeper
\begin_layout Section
Editor Cautions
\end_layout

\begin_layout Subsection*
Rstudio
\end_layout

\begin_layout Standard
While editing an Rnw file in Rstudio, there will be a button labeled knit
 and a small triangle by that item.
 It is very (VERY!) important to choose the correct output format.
 If you choose incorrectly, Rstudio may damage your document by trying to
 revise it.
\end_layout

\begin_layout Subsection*
LyX
\end_layout

\begin_layout Standard
The document preamble has manual settings for margins (geometry) as well
 as hyperlinks (PDF hyperref).
 At the current time, these settings are not integrated with the LyX settings.
 As a result, using the LyX pull down menu to revise geometry and PDF hyperrefs
 may break the document entirely.
 To change those features, please edit the document preamble or config files.
 Repeat 
\series bold
CAUTION
\series default
: Don't change the page margins or settings for hyperlinks with pull down
 menus.
\end_layout

\begin_layout Section
What to edit
\end_layout

\begin_layout Subsection*
Title and author information
\end_layout

\begin_layout Standard
The first block in the document has the title and author information.
 
\end_layout

\begin_layout Subsection*
Footer information
\end_layout

\begin_layout Standard
The footer in this document uses data that is provided in a file named 
\begin_inset Quotes eld
\end_inset

addressFooter.tex
\begin_inset Quotes erd
\end_inset

.
 If you don't have an addressFooter.tex file, the address can be entered
 directly into this document.
\end_layout

\begin_layout Subsection*
About the theme folder
\end_layout

\begin_layout Standard
The theme folder should will have copies of the theme files, but the logo
 files are not copied until the document is compiled for the first times.
 It is done that way to preserve the author's customizations.
\end_layout

\begin_layout Standard
Authors are free to edit the theme files, to replace logo images.
\end_layout

\begin_layout Section
Math Check
\end_layout

\begin_layout Standard
A linear regression is an estimate of this theoretical model, which is displayed
 as a numbered equation
\begin_inset Formula 
\begin{equation}
y_{i}=\beta_{0}+\beta_{1}x1_{i}+\beta_{2}x2_{i}+\varepsilon_{i}\label{eq:linear}
\end{equation}

\end_inset


\end_layout

\begin_layout Standard
The equation can be referred to as equation (
\begin_inset CommandInset ref
LatexCommand ref
reference "eq:linear"
plural "false"
caps "false"
noprefix "false"

\end_inset

).
\end_layout

\begin_layout Section
Code Chunk Check
\end_layout

\begin_layout Subsection*
Reports don't include raw code chunks
\end_layout

\begin_layout Standard
Simply put, a report document does not reveal source code and it should
 not distract the reader with raw output.
 A report is supposed to be presentable to a non R user.
 We don't want the report reader to see ugly output:
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<Rsummary, echo=F>>=
\end_layout

\begin_layout Plain Layout

set.seed(234234)
\end_layout

\begin_layout Plain Layout

dat <- data.frame(x = rnorm(100), y = rnorm(100))
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<tab1a, echo=F>>=
\end_layout

\begin_layout Plain Layout

## results hidden to prevent ugly raw output
\end_layout

\begin_layout Plain Layout

tab1 <- rockchalk::summarize(dat)
\end_layout

\begin_layout Plain Layout

tab1
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Instead, we want to refer the reader to a numbered table, a LaTeX floating
 object.
 Using a LaTeX cross-references, we refer the reader to Table 
\begin_inset ERT
status open

\begin_layout Plain Layout


\backslash
ref{tab:summ}
\end_layout

\end_inset

.
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<tab1, echo=F, results=tex>>=
\end_layout

\begin_layout Plain Layout

tab1.x <- xtable::xtable(attr(tab1, 
\begin_inset Quotes eld
\end_inset

numeric.formatted
\begin_inset Quotes erd
\end_inset

), caption = "Floating Table with Summary Statistics", label = "tab:summ")
\end_layout

\begin_layout Plain Layout

print(tab1.x, caption.placement = "top")
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
As a result, it is important to think about whether to embed the R code
 in the document at all.
 One might keep the R code in another file, and use it to generate graphs
 and tables that can be included into this report document.
 
\end_layout

\begin_layout Standard
As we have mentioned in the 
\begin_inset Quotes eld
\end_inset

code_chunks
\begin_inset Quotes erd
\end_inset

 vignette, there are some differences in the way code chunks are used in
 Sweave and knitr.
 To prevent the code chunks from printing their input and raw output into
 the document, the code chunks will often employ flags like 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
include=FALSE
\end_layout

\end_inset

, 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
echo=FALSE
\end_layout

\end_inset

, and 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
results=hide
\end_layout

\end_inset

.
 
\end_layout

\begin_layout Subsection*
Pleasant Output Tables
\end_layout

\begin_layout Standard
There are many R packages that generate LaTeX output which is presentable,
 close to the final desired result.
 In the development process, we will use these nearly-good-enough tables
 and then, when a final presentation demands find-grained adjustments, we
 do so manually on the table files.
\end_layout

\begin_layout Standard
There are several packages in R that can be used to generate LaTeX tables.
 Some of them can also create HTML or other formats suitable for other output
 document types.
 Some of the packages that we have used for these purposes are
\begin_inset Flex Code
status open

\begin_layout Plain Layout
xtable
\end_layout

\end_inset

,
\begin_inset Flex Code
status open

\begin_layout Plain Layout
Hmisc
\end_layout

\end_inset

, 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
memisc
\end_layout

\end_inset

, 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
texreg
\end_layout

\end_inset

, 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
rockchalk
\end_layout

\end_inset

, 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
kutils
\end_layout

\end_inset

, 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
knitr
\end_layout

\end_inset

, 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
pander
\end_layout

\end_inset

, and others.
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<m1, include=F, results=hide>>=
\end_layout

\begin_layout Plain Layout

dat <- data.frame(x = rnorm(100), y = rnorm(100))
\end_layout

\begin_layout Plain Layout

m1 <- lm(y ~ x, data = dat)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Subsubsection*
Regression tables
\end_layout

\begin_layout Standard
Some examples of nearly-good-enough tables are now presented.
 The regression result presented in Table 
\begin_inset CommandInset ref
LatexCommand ref
reference "tab:A-Regression-Table"

\end_inset

 may not be perfect by APA standards, but it is certainly good enough for
 intermediate versions.
 This is produced by the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
outreg()
\end_layout

\end_inset

 function in the rockchalk package.
 There are many other packages for creating LaTeX renditions of regression
 tables.
 They differ, for the most part, in the selection of summary statistics
 presented in the bottom part of the table.
\end_layout

\begin_layout Standard
\begin_inset Float table
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Caption Standard

\begin_layout Plain Layout
A Regression Table
\begin_inset CommandInset label
LatexCommand label
name "tab:A-Regression-Table"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset ERT
status open

\begin_layout Plain Layout

<<outreg1, echo=F, results=tex>>=
\end_layout

\begin_layout Plain Layout

library(rockchalk)
\end_layout

\begin_layout Plain Layout

vl <- c("x" = "Excellent Predictor")
\end_layout

\begin_layout Plain Layout

or <- outreg(list("First Model" = m1), varLabels = vl, tight = FALSE, centering
 = 
\begin_inset Quotes eld
\end_inset

siunitx
\begin_inset Quotes erd
\end_inset

)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Subsubsection*
Structural equation models
\end_layout

\begin_layout Standard
In the good looking table department, we also need to display structural
 equation models.
 This has been a long term objective in CRMDA.
 In the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
kutils
\end_layout

\end_inset

 package, we made a function 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
semTable
\end_layout

\end_inset

.
 Please see example output in Table 
\begin_inset CommandInset ref
LatexCommand ref
reference "tab:A-Confirmatory-Factor"

\end_inset

.
 
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<include=F>>=
\end_layout

\begin_layout Plain Layout

library(kutils)
\end_layout

\begin_layout Plain Layout

require(lavaan)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<cfa1, echo=F, include=F,results=tex>>=
\end_layout

\begin_layout Plain Layout

HS.model <- 'visual  =~ x1 + x2 + x3
\end_layout

\begin_layout Plain Layout

textual =~ x4 + x5 + x6
\end_layout

\begin_layout Plain Layout

speed   =~ x7 + x8 + x9'
\end_layout

\begin_layout Plain Layout

output1 <- cfa(HS.model, data = HolzingerSwineford1939, std.lv = TRUE)
\end_layout

\begin_layout Plain Layout

cfa1 <- semTable(output1, fits = "rmsea", paramSets = c("loadings", "latentvaria
nces"), type = "latex", print.results = FALSE, centering = 
\begin_inset Quotes erd
\end_inset

siunitx
\begin_inset Quotes erd
\end_inset

)
\end_layout

\begin_layout Plain Layout

cat(cfa1, file = 
\begin_inset Quotes eld
\end_inset

tmpout/t-cfa1.tex
\begin_inset Quotes erd
\end_inset

)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset Float table
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Caption Standard

\begin_layout Plain Layout
A Confirmatory Factor Analysis Table
\begin_inset CommandInset label
LatexCommand label
name "tab:A-Confirmatory-Factor"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset ERT
status open

\begin_layout Plain Layout


\backslash
input{tmpout/t-cfa1.tex}
\end_layout

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Standard
A warning is in order.
 Some R packages will export a table in using the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
longtable
\end_layout

\end_inset

 package.
 We noticed that longtable objects cannot be included in LaTeX floating
 tables.
 Instead, a longtable provides its own caption and label arguments, and
 longtable objects will thus float on their own.
\end_layout

\begin_layout Subsection*
Sweave's split feature
\end_layout

\begin_layout Standard
Sweave has a feature called 
\begin_inset Quotes eld
\end_inset

split
\begin_inset Quotes erd
\end_inset

, which means that the individual code chunks, their output, and graphics
 will be saved into separate files in an intermediate output folder.
 In the top of the .Rnw file, there will be a LaTeX macro:
\end_layout

\begin_layout LyX-Code

\backslash
SweaveOpts{prefix.string=tmpout/t, split=TRUE, ae=FALSE, height=5, width=6}
\end_layout

\begin_layout Standard
In our case, the pieces are saved into a folder named 
\begin_inset Quotes eld
\end_inset


\begin_inset Flex Code
status open

\begin_layout Plain Layout
tmpout
\end_layout

\end_inset


\begin_inset Quotes erd
\end_inset

.
 These chunk files will be created while the document is being compiled.
 We choose to have the letter 
\begin_inset Quotes eld
\end_inset

t
\begin_inset Quotes erd
\end_inset

 prepended with each chunk name.
\end_layout

\begin_layout Standard
The attraction of splitting the chunks is that the individual pieces can
 be used in several ways.
 First, we may want to keep a document 
\begin_inset Quotes eld
\end_inset

clean
\begin_inset Quotes erd
\end_inset

 by concentrating the code chunks in a section at the top of the file, and
 then insert the results in later sections where desired.
 Second, we may need to create a presentation or brief overview document
 that uses some of the chunks.
 If we have a research report and a client asks for a 10 minute slide show,
 we might write the slide show and then integrate a few of the code chunks
 for the presentation 
\emph on
without re-running
\emph default
 all of the analysis in the larger report.
 Third, if tables created by R need some minor enhancements, we can isolate
 their files easily and make manual corrections.
\end_layout

\begin_layout Standard
Using 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
split=TRUE
\end_layout

\end_inset

 causes a complication in tangling the code chunks.
 If we want to generate one R document to accumulate all of the code, then
 it is necessary to change 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
split=TRUE
\end_layout

\end_inset

 to 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
split=FALSE
\end_layout

\end_inset

 in the document.
 The function 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
stationery::rnw2pdf
\end_layout

\end_inset

 has a sub-routine that handles that 
\begin_inset Quotes eld
\end_inset

smart tangling
\begin_inset Quotes erd
\end_inset

 problem, so the author does not need to change the split setting in the
 document itself.
\end_layout

\begin_layout Standard
Splitting goes against the grain of some advice about reproducible documents.
 In the typical documentation, a user is told to type in a chunk and then
 the output plops into the document 
\begin_inset Quotes eld
\end_inset

right there
\begin_inset Quotes erd
\end_inset

.
 If the output from the chunk is not perfectly correct, then one ought to
 revise the software to fix the chunk output.
 Theoretically, that is sound advice.
 It is simply not practical at the current time.
 
\end_layout

\begin_layout Standard
Possibly for reasons mentioned in the previous paragraph, the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
knitr
\end_layout

\end_inset

 code chunk system does not allow splitting.
 In addition, the R CRAN archive will not accept package vignettes that
 use chunk splitting.
 
\end_layout

\begin_layout Subsubsection*
If Split is Forbidden
\end_layout

\begin_layout Standard
If policy does not allow splitting code chunks, then one can use R built-in
 functions to export text files, such as cat.
 The 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
xtable
\end_layout

\end_inset

 package includes a function 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
print 
\end_layout

\end_inset

that can write into files.
 Many regression-table writing packages have a file-export parameter.
\end_layout

\begin_layout Standard
If you see nothing in the output document before 
\begin_inset Quotes eld
\end_inset

it means
\begin_inset Quotes erd
\end_inset

 on the next line,
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<chunkfig, fig=T, include=F, echo=F, results=hide>>=
\end_layout

\begin_layout Plain Layout

x <- rnorm(1000)
\end_layout

\begin_layout Plain Layout

hist(x)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
it means that the chunk is invisible.
 However, the code chunk does generate a figure, 
\begin_inset Quotes eld
\end_inset


\begin_inset Flex Code
status open

\begin_layout Plain Layout
tmpout/t-chunkfig.pdf
\end_layout

\end_inset


\begin_inset Quotes erd
\end_inset

.
\end_layout

\begin_layout Standard
To insert that graphic in the document, write a LaTeX statement such as:
\end_layout

\begin_layout Standard
\begin_inset listings
lstparams "style=Rinput"
inline false
status open

\begin_layout Plain Layout


\backslash
includegraphics[width=5in]{tmpout/t-chunkfig}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Note: don't put 
\begin_inset Quotes eld
\end_inset


\begin_inset Flex Code
status open

\begin_layout Plain Layout
.pdf
\end_layout

\end_inset


\begin_inset Quotes erd
\end_inset

 on the end of the file name, LaTeX finds the file named 
\begin_inset Quotes eld
\end_inset


\begin_inset Flex Code
status open

\begin_layout Plain Layout
t-chunkfig.pdf
\end_layout

\end_inset


\begin_inset Quotes erd
\end_inset

.
 
\end_layout

\begin_layout Standard
If the code makes a LaTeX table that is presentable as is, then we can include
 that in the document at the suitable point.
 
\end_layout

\begin_layout Subsubsection*
Finally, check the tmp out folder
\end_layout

\begin_layout Standard
After compiling this document, the files we have in the 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
tmpout
\end_layout

\end_inset

 folder are:
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<tmpprint, echo=F>>=
\end_layout

\begin_layout Plain Layout

list.files(tdir)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
We see here one of the reasons why we suggest that all chunks should have
 names.
 It is easier to find them in the output directory if they have meaningful
 names.
\end_layout

\begin_layout Standard
As we complete this document, we will delete the test document folder 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
rnw2pdf-report-sweave
\end_layout

\end_inset

 at this point, but will leave 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
tmpout
\end_layout

\end_inset

 for user inspection.
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<rnwdirclean, echo=F>>=
\end_layout

\begin_layout Plain Layout

unlink("rnw2pdf-report-sweave", recursive = TRUE)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Section
Session Information
\end_layout

\begin_layout Standard
Session Information is usually not written into a report, but an output
 file is created by the following pieces.
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<session, include=F, results=hide, echo=F>>=
\end_layout

\begin_layout Plain Layout

zz <- "instructions.Rout"
\end_layout

\begin_layout Plain Layout

capture.output(sessionInfo(), file = zz, append = FALSE)
\end_layout

\begin_layout Plain Layout

if (!is.null(warnings())){
\end_layout

\begin_layout Plain Layout

    capture.output(warnings(), file = zz, append = TRUE)
\end_layout

\begin_layout Plain Layout

}
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<RoptionsRestore, echo=F, include=F, results=hide>>=
\end_layout

\begin_layout Plain Layout

## Don't delete this.
 It puts the interactive session options
\end_layout

\begin_layout Plain Layout

## back the way they were.
 If this is compiled within a session
\end_layout

\begin_layout Plain Layout

## it is vital to do this.
\end_layout

\begin_layout Plain Layout

options(opts.orig)
\end_layout

\begin_layout Plain Layout

par(par.orig)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset CommandInset bibtex
LatexCommand bibtex
btprint "btPrintCited"
bibfiles "R"
options "apalike2"

\end_inset


\end_layout

\end_body
\end_document
