#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 article
\begin_preamble
% \VignetteIndexEntry{rnw2pdf-guide-sweave}
% \VignetteEngine{Sweave}
% \VignetteBuilder{stationery::rnw2pdf}

% Following comment is required. Do not delete it.
%\usepackage{Sweave}

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

\input{theme/guidePreambleHeader.tex}
\input{theme/preambleFooter.tex}
\input{theme/guidePreambleSweavel.tex}
\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 false
\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
\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
guidesetup{%
\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=CRMDA,
\end_layout

\begin_layout Plain Layout

    email=pauljohn@ku.edu},
\end_layout

\begin_layout Plain Layout

  author={
\end_layout

\begin_layout Plain Layout

    lastname=Sullivan, 
\end_layout

\begin_layout Plain Layout

    firstname=Meghan, 
\end_layout

\begin_layout Plain Layout

    affiliation=CRMDA, 
\end_layout

\begin_layout Plain Layout

    email=meg.sullivan@ku.edu}, 
\end_layout

\begin_layout Plain Layout

  url={crmda.ku.edu/guides},
\end_layout

\begin_layout Plain Layout

  keywords={Sweave, LaTeX, guide, listings},
\end_layout

\begin_layout Plain Layout

  title={Writing Sweaved Guides:},
\end_layout

\begin_layout Plain Layout

  subtitle={Using the CRMDA Template},
\end_layout

\begin_layout Plain Layout

  leftlogo={theme/logoleft.pdf},
\end_layout

\begin_layout Plain Layout

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

\begin_layout Plain Layout

  number=00,
\end_layout

\begin_layout Plain Layout

}
\end_layout

\begin_layout Plain Layout


\backslash
guidehdr
\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

% In document Latex 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

% We need 2 image files for logos, top left (logoleft) and right
\end_layout

\begin_layout Plain Layout

% (logo-vert).
 
\end_layout

\begin_layout Plain Layout

% You can manually copy your desired image files
\end_layout

\begin_layout Plain Layout

% 
\begin_inset Quotes eld
\end_inset

logoleft.pdf
\begin_inset Quotes erd
\end_inset

 and 
\begin_inset Quotes eld
\end_inset

logo-vert.pdf
\begin_inset Quotes erd
\end_inset

 into the theme folder.
 
\end_layout

\begin_layout Plain Layout

% Or run this to retrieve or create them:
\end_layout

\begin_layout Plain Layout

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

\begin_layout Plain Layout

## The logos we need are
\end_layout

\begin_layout Plain Layout

logos <- c("logoleft.pdf", "logo-vert.pdf")
\end_layout

\begin_layout Plain Layout

## This will retrieve logo files from a designated package:
\end_layout

\begin_layout Plain Layout

try(getFiles(logos, 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 images for placeholders
\end_layout

\begin_layout Plain Layout

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

theme/logoleft.pdf
\begin_inset Quotes erd
\end_inset

)){
\end_layout

\begin_layout Plain Layout

  blankPDF(file = "theme/logoleft.pdf", height=1.2, width=1.2, 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

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

theme/logo-vert.pdf
\begin_inset Quotes erd
\end_inset

)){
\end_layout

\begin_layout Plain Layout

  blankPDF(file = "theme/logo-vert.pdf", height=2, width=1.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 Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

%The listings class used here allows within-document style
\end_layout

\begin_layout Plain Layout

%changes.
 R input boxes are governed by 
\end_layout

\begin_layout Plain Layout

%"Rsize", "Rbackground" and "Rcolor", while R output boxes depend on
\end_layout

\begin_layout Plain Layout

%"Routsize", "Routbackground", and "Routcolor".
 Colors
\end_layout

\begin_layout Plain Layout

%can be specified in many ways, as shown here
\end_layout

\begin_layout Plain Layout

%
\backslash
def
\backslash
Rsize{
\backslash
huge
\backslash
ttfamily}
\end_layout

\begin_layout Plain Layout

%
\backslash
def
\backslash
Routsize{
\backslash
huge}
\end_layout

\begin_layout Plain Layout

%
\backslash
def
\backslash
Rbackground{
\backslash
color[gray]{0.90}}
\end_layout

\begin_layout Plain Layout

%
\backslash
def
\backslash
Routbackground{
\backslash
color[gray]{0.40}}
\end_layout

\begin_layout Plain Layout

%
\backslash
def
\backslash
Rcolor{
\backslash
color[gray]{0.60}
\end_layout

\begin_layout Plain Layout

%
\backslash
def
\backslash
Routcolor{
\backslash
color[rgb]{0.9, 0.1, 0.1}]}
\end_layout

\begin_layout Plain Layout

%
\backslash
def
\backslash
Rcommentcolor{
\backslash
color{green}}
\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 guide documents using the CRMDA style.
 This is a LaTeX 
\begin_inset Quotes eld
\end_inset

noweb
\begin_inset Quotes erd
\end_inset

 guide document generated with the Sweave engine.
\end_layout

\begin_layout Abstract
\begin_inset CommandInset toc
LatexCommand tableofcontents

\end_inset


\end_layout

\begin_layout Section
Introduction
\end_layout

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

\end_inset

 to make guide documents using the CRMDA style.
 
\end_layout

\begin_layout Standard
Create a 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-guide-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-guide-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-guide-knit", 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 Standard
We suggest you
\end_layout

\begin_layout Enumerate
Compile this document 
\emph on
as is 
\emph default
to test your setup
\end_layout

\begin_layout Enumerate
Make revisions incrementally, and re-compile often to make sure nothing
 has been broken.
\end_layout

\begin_layout Standard
Don't make changes that you don't understand in the code chunks above or
 the last chunks below.
\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*
Compile a LyX file.
\end_layout

\begin_layout Enumerate
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

\begin_layout Standard
Because lyx uses a separate working directory for the compilation work,
 the project directory stays clean.
 None of the intermediate LaTeX files (*.log, *.log, *.bbl) will clutter up
 the current working directory.
\end_layout

\end_deeper
\begin_layout Enumerate
Open an R session and make sure the working directory is the same as the
 project writeup.
\end_layout

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

\begin_layout Plain Layout

rnw2pdf("skeleton.lyx", engine = "Sweave")
\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 in the same folder.
 It can be run in the shell as
\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
Sweave
\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.
\end_layout

\begin_layout Standard
This two-step process is valuable for debugging.
 It makes it easier to spot trouble by focusing on the separate transitions.
\end_layout

\end_deeper
\begin_layout Subsection*
Edit the Rnw file
\end_layout

\begin_layout Standard
The Rnw file we provide is produced by LyX, it is an intermediate step in
 the document production sequence.
 A two step compilation procedure is necessary.
 First, one must convert the 
\begin_inset Quotes eld
\end_inset

Rnw
\begin_inset Quotes erd
\end_inset

 file to 
\begin_inset Quotes eld
\end_inset

pdf
\begin_inset Quotes erd
\end_inset

 (with Sweave), and then the weaved tex file is compiled into pdf by pdflatex
 (or one of the other LaTeX compilers).
 
\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 Enumerate
You may have a 
\begin_inset Quotes eld
\end_inset

noweb
\begin_inset Quotes erd
\end_inset

 aware editor.
 Emacs, Rstudio, and others have menus that can initiate the work of weaving
 and rendering the document.
\end_layout

\begin_layout Enumerate
Open an R session and make sure the working directory is the same as the
 project writeup.
\end_layout

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

\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 and the creation of subsidiary 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

tangled
\begin_inset Quotes erd
\end_inset

 (if using knitr, it is referred to as a purled file).
 
\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 in the same folder.
 It can be run in the shell as
\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 --purl=FALSE --clean=FALSE --keep_tex=TRUE --verbose=TRUE skeleton.Rnw
\end_layout

\end_inset


\end_layout

\begin_layout Standard
The only difference in usage arises when a quoted string must be passed
 through.
 Suppose the files are in a subdirectory named 
\begin_inset Quotes eld
\end_inset

project
\begin_inset Quotes erd
\end_inset

.
 Inside the R code, the quoted string to specify the directory where the
 file resides (the working directory) would be like so:
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

rnw2pdf("skeleton.lyx", engine="Sweave" wd = "project")
\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"' --wd='"project"' skeleton.Rnw
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Note the single quotes that are protecting the double quotes.
\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 knit 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 Standard
Illustrative R code can be included in the document.
 The author has a good deal of control over how, and at which, the input
 and output are displayed.
 Correctly formatted LaTeX code can be written by R functions and it can
 appear in the document.
 The vignette 
\begin_inset Quotes eld
\end_inset

code_chunks
\begin_inset Quotes erd
\end_inset

 has full details.
 This is a brief highlight.
\end_layout

\begin_layout Subsection
Raw R input and output
\end_layout

\begin_layout Standard
Consider a regression.
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<>>=
\end_layout

\begin_layout Plain Layout

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

\begin_layout Plain Layout

m1 <- glm(y ~ x, data = dat, family = "poisson")
\end_layout

\begin_layout Plain Layout

summary(m1)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Subsection
Controlling display of chunk input and output
\end_layout

\begin_layout Standard
The chunk option 
\begin_inset Quotes eld
\end_inset

include
\begin_inset Quotes erd
\end_inset

 can be used to regulate whether the input and output appear immediately
 in the document.
 When combined with 
\begin_inset Quotes eld
\end_inset

echo
\begin_inset Quotes erd
\end_inset

 and 
\begin_inset Quotes eld
\end_inset

results
\begin_inset Quotes erd
\end_inset

, we can have a great deal of control.
\end_layout

\begin_layout Standard
In this chunk, we create a regression table object, but we hide everything:
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

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

\begin_layout Plain Layout

library(rockchalk)
\end_layout

\begin_layout Plain Layout

or <- outreg(list("My Poisson"= m1), varLabels = c("x" = "A Normal Predictor"),
 tight = FALSE)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
To display the object 
\begin_inset ERT
status open

\begin_layout Plain Layout


\backslash
code{or}
\end_layout

\end_inset

 to the reader, we have two options.
 
\end_layout

\begin_layout Standard
The standard Sweave approach is to include another chunk, and then cause
 the LaTeX markup for the object 
\begin_inset ERT
status open

\begin_layout Plain Layout


\backslash
code{or}
\end_layout

\end_inset

 to be woven directly into the document (depends on 
\begin_inset Quotes eld
\end_inset

results=tex
\begin_inset Quotes erd
\end_inset

).
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

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

\begin_layout Plain Layout

cat(or, file="tmpout/t-pois11.tex")
\end_layout

\begin_layout Plain Layout

cat(or)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\noindent
That chunk might be placed in a LaTeX floating table object, of course,
 as we show next.
\end_layout

\begin_layout Standard
A second option, which is more convenient, is to simply use LaTeX to input
 the saved file.
 When the code chunk 
\begin_inset Quotes eld
\end_inset

pois11
\begin_inset Quotes erd
\end_inset

 is executed, it creates a file named 
\begin_inset Quotes eld
\end_inset

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

.
 In Table 
\begin_inset CommandInset ref
LatexCommand ref
reference "tab:A-Poisson-Regression"

\end_inset

 we demonstrate how that can be included in a numbered floating table.
\end_layout

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

\begin_layout Plain Layout
\begin_inset Caption Standard

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

\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-pois11.tex}
\end_layout

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Standard
The reader has not yet had a chance to see the code chunk that calculated
 the regression.
 The code chunk that ran the regression was marked 
\begin_inset Quotes eld
\end_inset

include=F, results=hide
\begin_inset Quotes erd
\end_inset

.
 To show that code, there are (at least) 2 ways.
 
\end_layout

\begin_layout Enumerate
The chunk is named 
\begin_inset Quotes eld
\end_inset

pois10
\begin_inset Quotes erd
\end_inset

.
 Because we have the R Sweave argument split=T in the above, the code file
 is written separately and we can retrieve it with an input statement like
 so:
\end_layout

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

\begin_layout Plain Layout


\backslash
input{tmpout/t-pois10}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Because this note is included as an R vignette, and the R packaging policy
 prohibits the use of split=T in vignettes, this approach cannot be used
 in this document.
 
\end_layout

\end_deeper
\begin_layout Enumerate
An standard approach using Sweave itself, without split, is to create another
 chunk and then display it inside double 
\begin_inset Quotes eld
\end_inset

<<>>
\begin_inset Quotes erd
\end_inset

 brackets.
 Here we turn off evaluation (set eval=F) to prevent R from re-running the
 code chunk:
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<pois15, echo=T, eval=F, include=T>>=
\end_layout

\begin_layout Plain Layout

<<pois10>>
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\end_deeper
\begin_layout Standard
Those two code displays should be identical in the document.
\end_layout

\begin_layout Subsection
The listings package works well with Sweave
\end_layout

\begin_layout Standard
The document preamble includes settings for the LaTeX package listings,
 which is used to display code input and output.
 Inline references to 
\begin_inset Flex Code
status open

\begin_layout Plain Layout
code
\end_layout

\end_inset

 can be marked for highlighting (by LaTeX macro 
\begin_inset Quotes eld
\end_inset


\backslash
code
\begin_inset Quotes erd
\end_inset

) that will mimic the color styling of the code displays.
\end_layout

\begin_layout Standard
One advantage of using our Sweave-based LaTeX documents is the listings
 class can handle very long lines (allows linewrap) and also lets us have
 fine grained control over the display of code input and output.
 In guide documents, we have ine numbers turned on.
 is used.
 Among its benefits, we get 
\begin_inset Quotes eld
\end_inset

line wrap
\begin_inset Quotes erd
\end_inset

 on long lines.
\end_layout

\begin_layout Standard
The listings class used here allows within-document style changes.
 We expect that report documents will not be customized by most authors,
 but guide documents are less formal.
 In order to make output fit within the indicated space, it may be necessary
 to fiddle with the font size, for example.
 Here are the highlights: 
\end_layout

\begin_layout Enumerate
The font size and colors of R input chunks are controlled by LaTeX settings
 "Rsize", "Rbackground" and "Rcolor".
 Output displays depend on "Routsize", "Routbackground", and "Routcolor".
 
\end_layout

\begin_layout Enumerate
The font can be adjusted by declarations like this
\end_layout

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

\begin_layout Plain Layout


\backslash
def
\backslash
Rsize{
\backslash
huge
\backslash
ttfamily}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routsize{
\backslash
huge}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
These can be placed at the very beginning of the document to control all
 following chunks, but they can be placed immediately before any chunk to
 adjust just that one chunk.
\end_layout

\end_deeper
\begin_layout Enumerate
Colors can be specified in many ways
\end_layout

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

\begin_layout Plain Layout


\backslash
def
\backslash
Rbackground{
\backslash
color[gray]{0.90}}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routbackground{
\backslash
color[gray]{0.40}}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Rcolor{
\backslash
color[gray]{0.60}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routcolor{
\backslash
color[rgb]{0.9, 0.1, 0.1}]}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Rcommentcolor{
\backslash
color{green}}
\end_layout

\end_inset


\end_layout

\end_deeper
\begin_layout Standard
To demonstrate this customization, compare these two chunks.
 The first uses the defaults:
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<rnorm1>>=
\end_layout

\begin_layout Plain Layout

x <- rnorm(100)
\end_layout

\begin_layout Plain Layout

mean(x)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
While the second offers a shockingly beautiful offering (emphasis on shockingly).
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

{
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Rbackground{
\backslash
color[rgb]{0.4, 0.7, 0.6}}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routbackground{
\backslash
color[rgb]{0.6, 0.5, 0.8}}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routcolor{
\backslash
color{blue}}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routsize{
\backslash
huge}
\end_layout

\begin_layout Plain Layout

<<rnorm1replay>>=
\end_layout

\begin_layout Plain Layout

<<rnorm1>>
\end_layout

\begin_layout Plain Layout

@
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Note we use a LaTeX group here–the squiggly braces–to confine the beautifying
 impact of the change to the immediately following output.
 
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

{
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Rbackground{
\backslash
color[rgb]{0.4, 0.7, 0.6}}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routbackground{
\backslash
color[rgb]{0.6, 0.5, 0.8}}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routcolor{
\backslash
color{blue}}
\end_layout

\begin_layout Plain Layout


\backslash
def
\backslash
Routsize{
\backslash
huge}
\end_layout

\begin_layout Plain Layout


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

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Otherwise, at least in this document type, the change applies to all following
 chunks.
\end_layout

\begin_layout Subsection
listings can help with line wrap, if you let it.
\end_layout

\begin_layout Standard
This chunk shows what happens if the R input long.
 The line wrapping power of the listings class prevents code input from
 running into the margin.
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<norm10>>=
\end_layout

\begin_layout Plain Layout

dataFrame <- data.frame(x1 = rnorm(100, m = 13, s = 23), x2 = rnorm(100,
 m = 13, s = 23), x3 = rnorm(100, m = 13, s = 23))
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Section
References
\end_layout

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

\end_inset


\end_layout

\begin_layout Section
Session Info
\begin_inset CommandInset label
LatexCommand label
name "sec:Session-Info"

\end_inset


\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

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

\begin_layout Plain Layout

sessionInfo()
\end_layout

\begin_layout Plain Layout

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

\begin_layout Plain Layout

    print("Warnings:")
\end_layout

\begin_layout Plain Layout

    warnings()}
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<opts20, include=F>>=
\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

@
\end_layout

\end_inset


\end_layout

\end_body
\end_document
