-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.Rmd
102 lines (76 loc) · 2.42 KB
/
functions.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
---
title: "functions"
author: "Kalei Shotwell"
date: "2/5/2020"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
#Source relevant functions
#you can use source inside a loop statement or within an apply statement to go through a directory of functions rather than one by one
source('conversions.R')
```
## Load your librariey
```{r}
library(roxygen2)
library(mytools)
```
```{r}
airtemps<- c(212,30.3,80,32,0)
cel1<-(airtemps[1]-32)*5/9
cel2<-(airtemps[2]-32)*5/9
cel3<-(airtemps[3]-32)*5/9
cel4<-(airtemps[4]-32)*5/9
cel5<-(airtemps[5]-32)*5/9
```
## Define conversions functions
First, F to C: documentation in functions can be made with the comment sign (#) with an apostrophe after it (')
param is where the arguements are in the help section
return is the value section in the help section
help files are saved in an rd format, and roxygen packages help you convert it so something like rstudio can figure out what it is, keywords is used for classifying the r package in the heirarchy of help
```{r}
#' Convert degrees Fahrenheit to Celsius
#'
#' We use the simple formulat for temperature conversiton to convert.
#' One can find this in any intro physics book.
#' @param fahr the temperature to be converted in degrees Fahrenheit
#' @return the temperature converted to degrees Celsius
#' @keywords conversion
#' @examples
#' fahr_to_celsius(32)
#' fahr_to_celsius(c(212,100,32))
fahr_to_celsius <- function(fahr) {
#stopifnot(is.numeric(fahr))
#assertthat(is.numeric(fahr))
celsius<-(fahr-32)*5/9
return(celsius)
}
#ask about source files
```
```{r}
celsius6<-fahr_to_celsius(airtemps[1])
celsius6==cel1
celsius<-fahr_to_celsius(airtemps)
celsius
```
Second, C to F:
```{r}
cel_to_fahr <- function(cel) {
fahrenheit<-(cel*9/5)+32
return(fahrenheit)
}
fahrenheit<-cel_to_fahr(celsius)
fahrenheit==airtemps
```
namespace is a way to separate functions out and keep them from colliding with one another.
mean in base is from stats names space, so full name of mean is
stats::mean, that is it's namespace
sometimes having a function in the markdown files is a little overwhelming and possibly not helpful for the markdown file.
RMD files are somewhat front end to the audience. think about your audience in how you decide whether to use functions or markdown to show your code.
```{r}
environment_info("Works from functions test")
```