/
functions.R
216 lines (200 loc) · 5.25 KB
/
functions.R
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
209
210
211
212
213
214
215
216
#' @importFrom magrittr %>%
#' @export
magrittr::`%>%`
#' Convenience 'not-in' operator
#'
#' Complement of the built-in operator `%in%`. Returns the elements of `x` that are not in `y`.
#' @title `%nin%`
#' @param x vector of items
#' @param y vector of all values
#' @return logical vector of items in x not in y
#' @author Kieran Healy
#' @rdname nin
#' @examples
#' fruit <- c("apples", "oranges", "banana")
#' "apples" %nin% fruit
#' "pears" %nin% fruit
#' @export
"%nin%" <- function(x, y) {
return( !(x %in% y) )
}
#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param date PARAM_DESCRIPTION
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @rdname MMWRweekday
#' @author Kieran Healy
#' @source http://
MMWRweekday <- function (date)
{
factor(strftime(as.Date(date), "%w"), levels = 0:6, labels = c("Sunday",
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"))
}
#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param year PARAM_DESCRIPTION
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @rdname start_date
#' @author AUTHOR_NAME
#' @source http://
start_date <- function (year)
{
jan1 = as.Date(paste(year, "-01-01", sep = ""))
wday = as.numeric(MMWRweekday(jan1))
jan1 - (wday - 1) + 7 * (wday > 4)
}
#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param MMWRyear PARAM_DESCRIPTION
#' @param MMWRweek PARAM_DESCRIPTION
#' @param MMWRday PARAM_DESCRIPTION, Default: NULL
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @rdname MMWRweek2Date
#' @author Kieran Healy
#' @source http://
MMWRweek2Date <- function (MMWRyear, MMWRweek, MMWRday = NULL)
{
stopifnot(all(is.numeric(MMWRyear)))
stopifnot(all(is.numeric(MMWRweek)))
stopifnot(all(0 < MMWRweek & MMWRweek < 54))
stopifnot(length(MMWRyear) == length(MMWRweek))
if (is.null(MMWRday))
MMWRday = rep(1, length(MMWRweek))
stopifnot(all(0 < MMWRday & MMWRday < 8))
jan1 = start_date(MMWRyear)
return(jan1 + (MMWRweek - 1) * 7 + MMWRday - 1)
}
#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param year PARAM_DESCRIPTION
#' @param week PARAM_DESCRIPTION
#' @param day PARAM_DESCRIPTION, Default: NULL
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @seealso
#' \code{\link[MMWRweek]{MMWRweek2Date}}
#' @rdname mmwr_week_to_date
#' @author Kieran Healy
#' @source http://
#' @export
mmwr_week_to_date <- function (year, week, day = NULL)
{
year <- as.numeric(year)
week <- as.numeric(week)
day <- if (!is.null(day))
as.numeric(day)
else rep(1, length(week))
week <- ifelse(0 < week & week < 54, week, NA)
as.Date(ifelse(is.na(week), NA, MMWRweek2Date(year,
week, day)), origin = "1970-01-01")
}
#' @title fmt_nc
#' @description Format fmt_nc in df
#' @param x df
#' @return formatted string
#' @details use in fn documentation
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @rdname fmt_nc
#' @author Kieran Healy
fmt_nc <- function(x){
prettyNum(ncol(x), big.mark=",", scientific=FALSE)
}
#' @title fmt_nr
#' @description Format fmt_nr in df
#' @param x df
#' @return formatted string
#' @details use in fn documentation
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @author Kieran Healy
fmt_nr <- function(x){
prettyNum(nrow(x), big.mark=",", scientific=FALSE)
}
#' @title tabular
#' @description Make an Rd table from a data frame
#' @param df Data frame
#' @param ... Other args
#' @return Rd table
#' @details DETAILS
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @author Kieran Healy
#' @source http://
tabular <- function(df, ...) {
stopifnot(is.data.frame(df))
align <- function(x) if (is.numeric(x)) "r" else "l"
col_align <- vapply(df, align, character(1))
cols <- lapply(df, format, ...)
contents <- do.call("paste",
c(cols, list(sep = " \\tab ", collapse = "\\cr\n#' ")))
paste("#' \\tabular{", paste(col_align, collapse = ""), "}{\n#' ",
paste0("\\strong{", names(df), "}", sep = "", collapse = " \\tab "), " \\cr\n#' ",
contents, "\n#' }\n", sep = "")
}
#' Make a table of stmf country years
#'
#' @param df The stmf data frame
#' @return A tibble
#' @details Get a table of country x year coverage for stmf
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @author Kieran Healy
#' @source http://
stmf_country_years <- function(df = stmf) {
df %>%
dplyr::select(cname, year) %>%
dplyr::group_by(cname, year) %>%
dplyr::tally() %>%
dplyr::mutate(n = as.character(n),
n = dplyr::recode(n, "0" = "-", .default = "Y")) %>%
dplyr::group_by(year, cname) %>%
dplyr::arrange(year) %>%
tidyr::pivot_wider(names_from = year, values_from = n) %>%
dplyr::mutate(dplyr::across(where(is.character), dplyr::recode, .missing = "-")) %>%
dplyr::arrange(cname)
}