This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathprog2.Rmd
116 lines (82 loc) · 3.27 KB
/
prog2.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
title: "programming"
author: "Naomi Tague"
date: "Feb 2, 2016"
output: html_document
---
To create a package - start by creating a new project
Name this project - make sure it reflects something about what the project is
Make sure you click to create a git repository with your package
Add any existing source files
```{r setup, echo=FALSE}
#make sure you have libraries
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
library(lubridate)
library(testthat)
library(devtools)
library(roxygen2)
```
```{r code for spring.summary.R, echo=FALSE}
#' Summary information about spring climate
#'
#' computes summary information about spring temperature and precipitation
#' @param clim.data data frame with columns tmax, tmin (C)
#' rain (precip in mm), year, month (integer), day
#' @param months (as integer) to include in spring; default 4,5,6
#' @return returns a list containing, mean spring temperature (mean.springT, (C))
#' year with lowest spring temperature (coldest.spring (year))
#' mean spring precipitation (mean.springP (mm))
#' spring (as year) with highest precip (wettest.spring (year))
spring.summary = function(clim.data, spring.months = c(4:6)) {
spring = subset(clim.data, clim.data$month %in% spring.months)
mean.springT = mean(c(spring$tmax, spring$tmin))
lowyear = spring$year[which.min(spring$tmin)]
spring.precip = as.data.frame(matrix(nrow=unique(spring$year), ncol=2))
colnames(spring.precip)=c("precip","year")
spring.precip = aggregate(spring$rain, by=list(spring$year), sum)
colnames(spring.precip) = c("year","precip")
mean.spring.precip = mean(spring.precip$precip)
wettest.spring = spring.precip$year[which.max(spring.precip$precip)]
return(list(mean.springT = mean.springT, coldest.spring=lowyear,
mean.springP=mean.spring.precip,wettest.spring=wettest.spring ))
}
```
Code for functions are in the classexample package
```{r use the function and save data}
#read in data
clim=read.csv("../clim.csv")
# load "stuff" in your package including R
load_all()
result = spring.summary(clim)
View(result)
# save data for use in your R package
save(clim, file="data/clim.RData")
# generate data
tmp = c("ponderosa","jack","white","lodgepole","douglasfir","oak")
obs.trees= list(species=sample(tmp, replace=T, size=100))
obs.trees$carbon = runif(min=5, max=20, n=100)
coeff.species.growth = data.frame(species=c("ponderosa","jack","white","lodgepole","douglasfir","oak"),
maxrate=c(1.2,1.1,1.3,1.6,1.9,1.2),
topt = c(9,7,6,5,7,12), pmax = c(300,300,300,400,600,400), pmin = c(100,200,200,250,250,100))
# run our functions
compute_simpson_index(obs.trees$species)
compute_NPV(value=100, time=20, discount=0.01)
compute_carbon(obs.trees$carbon, obs.trees$species, coeff.species.growth, 9, 200)
# save data for use in your R package
save(obs.trees, file="data/obstrees.RData")
save(coeff.species.growth, file="data/coeff.species.growth.RData")
```
```{r more complex function}
# more complex use of building blocks
compute_ecobenefit(obs.trees, 20, 10, coeff.species.growth, clim, 0.01)
compute_ecobenefit(obs.trees, 20, 10, coeff.species.growth, clim, 0.05)
# document all functions
document()
```
Testing
```{r testing}
expect_true(spring.summary(clim.data, spring.months=c(1:4))$coldest.spring > 2)
test_dir("tests")