-
Notifications
You must be signed in to change notification settings - Fork 6
/
activity.R
executable file
·467 lines (442 loc) · 11.4 KB
/
activity.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#' Activity Summary
#'
#' See \url{https://dev.fitbit.com/build/reference/web-api/activity/} for more details.
#'
#' @param date The date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @importFrom dplyr bind_rows mutate select everything
#' @importFrom httr content
#' @importFrom janitor clean_names
#'
#' @examples
#' \dontrun{
#' date <- lubridate::today()
#' get_activity_summary(date)
#' }
#' @return A tibble with the `date` and a number of activity summary metrics for the day.
#' @export
get_activity_summary <- function(date) {
validate_date(date)
url <- sprintf(
"%s/1/user/%s/activities/date/%s.json",
base_url,
fetch_user_id(),
date
)
r <- perform_get(url)
r %>%
content(as = "parsed", type = "application/json") %>%
pluck("summary") %>%
list_modify("heartRateZones" = NULL) %>%
list_modify("distances" = NULL) %>%
bind_rows() %>%
mutate(
date = as.Date(date),
across(-"date", as.numeric)
) %>%
select("date", everything()) %>%
clean_names()
}
#' Activity Time Series
#'
#' See \url{https://dev.fitbit.com/build/reference/web-api/activity/} for more details.
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param resource_path The resource path. See \url{https://dev.fitbit.com/build/reference/web-api/activity/} for options
#'
#' @importFrom purrr flatten_dfr
#' @importFrom rlang :=
#' @importFrom dplyr rename
#'
#' @noRd
get_activity_time_series <- function(start_date, end_date, resource_path) {
validate_date(start_date)
validate_date(end_date)
url <- sprintf(
"%s/1/user/%s/activities/%s/date/%s/%s.json",
base_url,
fetch_user_id(),
resource_path,
start_date,
end_date
)
r <- perform_get(url)
r %>%
content(as = "parsed", type = "application/json") %>%
flatten_dfr() %>%
rename(
date = "dateTime",
!!resource_path := "value"
) %>%
mutate(
date = as.Date(.data$date),
across(-"date", as.numeric)
) %>%
clean_names()
}
#' Calories Time Series
#'
#' Resource path /activities/calories
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' calories(date)
#' }
#'
#' @return A tibble with two columns: `date` and `calories`
#' @export
get_calories <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "calories"
)
}
#' Calories BMR Time Series
#'
#' Resource path /activities/caloriesBMR
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_calories_bmr(date)
#' }
#'
#' @return A tibble with two columns: `date` and `calories_bmr`
#' @export
get_calories_bmr <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "caloriesBMR"
)
}
#' Steps Time Series
#'
#' Resource path /activities/steps
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_steps(date)
#' }
#'
#' @return A tibble with two columns: `date` and `steps`
#' @export
get_steps <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "steps"
)
}
#' Distance Time Series
#'
#' Resource path /activities/distance
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_distance(date)
#' }
#'
#' @return A tibble with two columns: `date` and `distance`
#' @export
get_distance <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "distance"
)
}
#' Floors Time Series
#'
#' Resource path /activities/floors
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_floors(date)
#' }
#'
#' @return A tibble with two columns: `date` and `floors`
#' @export
get_floors <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "floors"
)
}
#' Elevation Time Series
#'
#' Resource path /activities/elevation
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_elevation(date)
#' }
#'
#' @return A tibble with two columns: `date` and `elevation`
#' @export
get_elevation <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "elevation"
)
}
#' Minutes Sedentary Time Series
#'
#' Resource path /activities/minutesSedentary
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_minutes_sedentary(date)
#' }
#'
#' @return A tibble with two columns: `date` and `minutes_sedentary`
#' @export
get_minutes_sedentary <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "minutesSedentary"
)
}
#' Minutes Lightly Active Time Series
#'
#' Resource path /activities/minutesLightlyActive
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_minutes_lightly_active(date)
#' }
#'
#' @return A tibble with two columns: `date` and `minutes_lightly_active`
#' @export
get_minutes_lightly_active <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "minutesLightlyActive"
)
}
#' Minutes Fairly Active Time Series
#'
#' Resource path /activities/minutesFairlyActive
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_minutes_fairly_active(date)
#' }
#'
#' @return A tibble with two columns: `date` and `minutes_fairly_active`
#' @export
get_minutes_fairly_active <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "minutesFairlyActive"
)
}
#' Minutes Very Active Time Series
#'
#' Resource path /activities/minutesVeryActive
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_minutes_very_active(date)
#' }
#'
#' @return A tibble with two columns: `date` and `minutes_very_active`
#' @export
get_minutes_very_active <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "minutesVeryActive"
)
}
#' Activity Calories Time Series
#'
#' Resource path /activities/activityCalories
#'
#' @param start_date The start date of records to be returned in "yyyy-mm-dd" or date(time) format
#' @param end_date The end date of records to be returned in "yyyy-mm-dd" or date(time) format
#'
#' @examples
#' \dontrun{
#' start_date <- lubridate::today() - lubridate::weeks(1)
#' end_date <- lubridate::today()
#' get_activity_calories(date)
#' }
#'
#' @return A tibble with two columns: `date` and `activity_calories`
#' @export
get_activity_calories <- function(start_date, end_date) {
get_activity_time_series(
start_date,
end_date,
resource_path = "activityCalories"
)
}
#' @noRd
get_bests_and_totals <- function(best, tracker) {
url <- sprintf(
"%s/1/user/%s/activities.json",
base_url,
fetch_user_id()
)
r <- perform_get(url)
r %>%
content(as = "parsed", type = "application/json") %>%
pluck(
ifelse(best, "best", "lifetime"), ## pluck best or lifetime
ifelse(tracker, "tracker", "total") ## pluck tracker or total
)
}
#' Tracker Totals
#'
#' Retrieve tracker total distance, floors, and steps
#'
#'
#' @examples
#' \dontrun{
#' get_tracker_totals()
#' }
#'
#' @export
#' @return A tibble of all-time tracker totals (i.e. the total `distance`, `floors`, and `steps` tracked by your tracker)
get_tracker_totals <- function() {
get_bests_and_totals(
best = FALSE,
tracker = TRUE
) %>%
bind_rows() %>%
select(
"distance",
"floors",
"steps"
)
}
#' Lifetime Totals
#'
#' Retrieve lifetime total distance, floors, and steps
#'
#'
#' @examples
#' \dontrun{
#' get_lifetime_totals()
#' }
#'
#' @return A tibble of all-time totals across trackers (i.e. the total `distance`, `floors`, and `steps` tracked across all of your trackers)
#' @export
get_lifetime_totals <- function() {
get_bests_and_totals(
best = FALSE,
tracker = FALSE
) %>%
bind_rows() %>%
select(
"distance",
"floors",
"steps"
)
}
#' Tracker Bests
#'
#' Retrieve tracker best distance, floors, and steps
#'
#'
#' @examples
#' \dontrun{
#' get_tracker_bests()
#' }
#'
#' @importFrom tidyr unnest_wider
#' @importFrom tibble enframe
#'
#' @return A tibble the best `distance`, `floors`, and `steps` (by date) tracked on your tracker
#' @export
get_tracker_bests <- function() {
get_bests_and_totals(
best = TRUE,
tracker = TRUE
) %>%
enframe() %>%
unnest_wider("value") %>%
rename(
metric = "name"
)
}
#' Lifetime Bests
#'
#' Retrieve lifetime best distance, floors, and steps
#'
#'
#' @examples
#' \dontrun{
#' get_lifetime_bests()
#' }
#'
#' @return A tibble the best `distance`, `floors`, and `steps` (by date) tracked on any of your trackers
#' @export
get_lifetime_bests <- function() {
get_bests_and_totals(
best = TRUE,
tracker = FALSE
) %>%
enframe() %>%
unnest_wider("value") %>%
rename(
metric = "name"
)
}