-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example.rnw
88 lines (65 loc) · 3.09 KB
/
Example.rnw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
% Load in R "sweave" (.rnw) using setting "knitr"
% Kyle Davis; Ohio State University
%-------------------------------------------------------------------------------
% Here we can load any typical LaTeX packages and settings we'd like:
\documentclass[12pt]{article}
% Packages used:
% Some math tools to make things easier/nicer
\usepackage{amsfonts, amsmath, amsthm, amssymb}
% Setting margins
\usepackage[top = 1in, left = 1in, right = 1in, bottom = 1in]{geometry}
% hiding any hyperlink boxes
\usepackage[bookmarks = false, hidelinks]{hyperref}
\pagestyle{plain} % Removes page numbering
%-------------------------------------------------------------------------------
\begin{document}
\begin{flushleft}
\textbf{Name}\\
Note that here we can add any comments, etc to talk about what we're coding.
% We set up code in the following format:
<<DGP, echo=TRUE, message=FALSE, warning=FALSE>>=
# <<label , options=TRUE, options=FALSE>>=
set.seed(12345)
x <- 5 # Five x's
y <- 5 # Five y's
# @
@
We can do some cool things with our code:
<< setup, include=TRUE, cache=FALSE, echo=TRUE>>=
# Here echo =TRUE to show code, to hide from .pdf: echo=FALSE
# Code Setup/ and Formatting:
library(knitr)
# This code is for figures, which may be similarly customized output visualizations:
## See https://github.com/yihui/knitr/blob/master/inst/examples/knitr-themes.Rnw
## and https://github.com/yihui/knitr/blob/master/inst/misc/Sweavel.sty
# to fix arrow <_ issue vs <-
options(formatR.arrow=TRUE, width=78)
##! Changing out theme to "kellys"
##! For Printing, consider changing this to "moe" or "edit-xcode"
##! Check out Themes at: http://animation.r-forge.r-project.org/knitr/
theme = knit_theme$get("kellys")
knit_theme$set(theme)
@
\begin{center}
<<Example, out.width='4in'>>=
# Now we have fancy code highlights:
# Added in a plot here to see ease of replicable code:
# out.width='size parameter',
# Note this will remember our x and y values from before
z <- rnorm(1000, x, y)
library(ggplot2)
qplot(z) +
theme_bw()
@
\end{center}
% All images will be saved to a new folder ("figures") wherever this .rnw is saved.
Essentially this is just a beautiful cross between \LaTeX and R; a few things to note here:
\begin{itemize}
\item[1.] Spell checker is a little more difficult in this format. Also the \LaTeX code will not auto-complete, making things difficult sometimes.
\item[2.] However, your code has to run 100\% correctly from just what you enter, meaning any that reads this will have 100\% reproducability!
\item[3.] You don't ever have to show your R code, you can have it running underneath (echo=FALSE). This means that you can actually write papers here like you would in \LaTeX but have your data and replication update automatically. This includes figure names, tables, and image customizations.
\item[4.] All of \LaTeX commands work here as well, so creating tables, images, and references work as well!
\end{itemize}
%-------------------------------------------------------------------------------
\end{flushleft}
\end{document}