-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleVignette.rmd
95 lines (65 loc) · 2.81 KB
/
ExampleVignette.rmd
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
89
90
91
92
93
94
95
---
title: "Software Carpentry Workshop Example"
author: "Martina Morris"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
This is meant to demostrate the use of a git repo along with R, and knitr/markdown
I was hoping to keep the .md file with "keep_md: true" but i'm not sure how to specify that in the YAML code above.
## Vignette Info
Here we will show how to write a function to summarize some data
## Data
The data here come from [NOAA](http://www.ncdc.noaa.gov/qclcd/QCLCD), and I've downloaded the December 2015 dailies for Sand Point (in Seattle). The datafile is in the repo, and it's called QCLData.txt
## Function
The function summarizes data from an arbitrary file -- the filename is the argument to the function -- but the data from NOAA was sufficiently hinky that the commands in the function really only work with that one file...
```{r, fig.show='hold'}
summarize <- function(datafile) {
mydir <- "DataFiles/" # data directory
rawdata <- read.csv(paste(mydir, datafile, sep=""), skip = 6,
header=T, na.strings="M") #read it in
aaa <- rawdata[,c("Tmax","Tmin", "Tavg", "Depart")] #subset
# par(mfrow=c(1,2))
plot(aaa$Tmin, aaa$Tmax, pch="@", col="red",
main="Dec 2015 Daily Temps", xlab="Min", ylab="Max")
abline(lm(aaa$Tmax ~ aaa$Tmin))
plot(aaa$Depart, col="blue",
main="Departure from Average", xlab="Date", ylab="Degrees")
abline(h=0, col="red")
meanvec <- colMeans(aaa, na.rm=TRUE)
return(meanvec)
}
```
So, now let's use the function:
```{r}
summarize("QCLData.txt")
ls()
```
Note that the function is in the workspace, but the meanvec is not. The "return(meanvec)" sent it to the screen. To send it to an object in the workspace:
```{r}
meanvec.test <- summarize("QCLData.txt")
ls()
```
But this way you don't see it printed on the screen. You can do both at once with:
```{r}
(meanvec.test <- summarize("QCLData.txt"))
ls()
```
Voila!
And just to check that the R project association is working correctly with the Git local repository I created with Git Gui (cloning the repo from GitHub), I'll see if this commit/push this works.
You can enable figure captions by `fig_caption: yes` in YAML:
output:
rmarkdown::html_vignette:
fig_caption: yes
Then you can use the chunk option `fig.cap = "Your figure caption."` in **knitr**.
## More Examples
You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using `knitr::kable()`.
```{r, echo=FALSE, results='asis'}
knitr::kable(head(mtcars, 10))
```
Also a quote using `>`:
> "He who gives up [code] safety for [code] speed deserves neither."
([via](https://twitter.com/hadleywickham/status/504368538874703872))