-
Notifications
You must be signed in to change notification settings - Fork 0
/
whiskR_Markdown.Rmd
209 lines (162 loc) · 4.88 KB
/
whiskR_Markdown.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
---
title: "Isotopes Analysis Package - Mammal Lab"
author: "Gary Truong and Ben Walker"
date: "07/07/2020"
output: html_document
---
```{r setup, include=FALSE}
#install.packages("devtools")
#install.packages("roxygen2")
#install.packages("usethis")
devtools::install_github("r-lib/pkgdown")
install.packages("Rtools")
# install.packages("installr")
#installr::install.Rtools(choose_version = TRUE, check = FALSE, GUI = TRUE, page_with_download_url = "https://cran.r-project.org/bin/windows/Rtools/")
library(magrittr)
library(devtools)
library(roxygen2)
library(tidyverse)
library(pkgdown)
devtools::install_github("GaryTruong/whiskR")
library(whiskR)
# devtools::create("whiskR")
```
Add functions
```{r}
x <- (92.6-(14*(0.0126*141.5)))/(1-(14*0.0126))
# LP1 calculates the cut section length that should be cut to obtain a defined time period.
LP1 <- function(L, Time, k, L.asym){
section = format((L - (Time*(k*L.asym)))/(1-(Time*k)), digits = 4, nsmall = 1)
paste(section, "mm", sep = "")
}
# k is estimated from seal whiskers and adapted for devils, assumption that whiskers fall out after 1 year
# L.asym is asymptotic length which is longest recorded length + 1% at the given position
# L is length of cut or plucked whisker
# Time is specified numnber of days represented by sample
# This function does not factor in intradermal length
LP1(156.2,75, 0.0126, 171.4)
LP1(129.5, 14, 0.0126, 150.3)
LP1(125.045, 14, 0.0126, 150.3)
#TP1 calculates the time period that is represented based on a cut section
TP1 <- function(Lp, Lp1, k, L.asym){
time = format((Lp - Lp1)/(k*(L.asym-Lp1)), digits = 3, nsmall = 0)
paste(time, "days", sep = " ")
}
## Simple Von Bertanlanffy equation for L and T
## L = L.asym * (1-exp(-KT))
## T = (-log(1-156.2/171.4))/(0.0126)
# MaxTime uses simple Von Bertanlanffy model (growth function) to caclulate maximum theoretically time that the sample whisker represents
MaxTime <- function(L , L.asym, k){
Time = format((-log(1-L/L.asym))/(k), digits = 4, nsmall = 1)
paste(Time, "days", sep = " ")
}
MaxTime(169.7, 171.4, 0.0126)
# K - growth coefficient
k <- function(L, L.asym, Time){
k = format((-log(1-L/L.asym))/(Time), digits = 4)
k
}
k(1000, 1010, 365)
#Intra time calculates the days represented by the intradermal length
Intra.time <- function(L.int, L.asym, L, k){
time = format((L.int)/(k*(L.asym - (L - L.int))), digits = 3, nsmall = 0)
paste(time, "days", sep = " ")
}
Intra.time(5.3, 163.4, 127.8, 0.0126)
## Vindi tassie devil
MaxTime(127.8, 163.4, 0.0126)
#Vindi sampled on 1st August 2018 , 120.9 max days, should go back to april
Date
```
```{r}
TP1(L=150 ,L.asym=170, LP1=140.5, k=0.0126)
MaxTime(150,170,0.0126)
library(dplyr)
sample <- function(L, L.asym, Time, k){
a <- as.numeric(whiskR::LP1(L, L.asym, Time, k))
b <- as.numeric(whiskR::LP1(L, L.asym, Time, k)) %>%
whiskR::LP1(L.asym, Time, k)
as.numeric(c(a,b,Time))
}
a <- c()
a <- c(1)
b <- c(a)
sample(150,170,14,0.0126)
section <- function(L, L.asym, k, Time = 1, date = "1900/01/01"){
out <- list(0)
name <- value <- L.start <- L.end <- NULL
a <- as.numeric(whiskR::LP1(L, L.asym, Time, k))
out[1] = a
out[2] = Time
out[3] = L
out[4] = lubridate::as_date(date)
out[[3]][2] = a
for(i in 1:1000){
if(a > 0 & a < L){
a <- as.numeric(out[[1]][i]) %>%
whiskR::LP1(L.asym, Time, k)
out[[1]][i+1] = a
out[[2]][i+1] = Time
out[[3]][i+2] = a
out[[4]][i+1] = lubridate::as_date(date) - Time*i
}
else if(a <0){
out[[1]][i] = 0
out[[2]][i] = 1 + whiskR::TP1(a, L.asym, 0, k)
out[[4]][i] = lubridate::as_date(date) - Time*(i-1)
}
else{
stop
}
}
x <- tibble::enframe(as.numeric(out[[3]])) %>%
dplyr::select(name, L.start = value)
y <- tibble::enframe(as.numeric(out[[2]])) %>%
dplyr::select(name, time = value)
z <- tibble::enframe(as.numeric(out[[1]])) %>%
dplyr::select(name,L.end = value)
w <- tibble::enframe(lubridate::as_date(out[[4]])) %>%
dplyr::select(name, date = value)
x %>% dplyr::left_join(z, by = "name") %>%
dplyr::left_join(y, by = "name") %>%
dplyr::left_join(w, by = "name") %>%
dplyr::filter(L.start > 0) %>%
dplyr::mutate(SectionLength = L.start-L.end) %>%
dplyr::mutate(CumulativeLength = cumsum(SectionLength))
}
c <- section(169.5,170, 0.0126, 1)
```
Generate documents for functions
```{r}
devtools::document()
```
Create a vignette
```{r}
library(usethis)
usethis::use_vignette("whiskR_vignette")
```
Creating Website
```{r}
# not working for R 4.0.2
#install.packages("rpkgdown")
devtools::install_github("r-lib/pkgdown")
library(pkgdown)
# Run once to configure package to use pkgdown
usethis::use_pkgdown()
# Run to build the website
pkgdown::build_site()
## running R CMD check through devtools
devtools::check()
```
Testing
```{r}
# usethis::use_testthat()
# use_test("TP1")
#
# devtools::test()
#
# #use_travis()
```
Test
```{r}
```