stepcount:分析我的朋友和家人健康数据的 Shiny APP

stepcount:分析我的朋友和家人健康数据的 Shiny APP

我写的第二个 shiny APP!用于分析苹果手机健康数据的一个网页应用。2018·Footprint。如果你也想把你的数据加进来,可以联系我哈!

依赖项

使用

离线使用

R
1
shiny::runGitHub("czxa/stepcount")

在线使用

访问2018·Footprint

预览

PC 端







移动端

主页 概览 月度回顾
月度回顾 周度回顾 周度回顾
有趣的事 有趣的事 和我比一比?

代码

这个项目的代码分为两块,一块是把手机导出的 xml 数据整理成 rds 数据的,另一块是生成 Shiny APP 的。

首先是处理 xml,我的处理方法是首先使用xml2json这个工具把 xml 文件转换成 json 文件,然后再处理 json 文件:

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
# 用于整理xml文件生成rds文件
library(tidyverse)
library(lubridate)
library(jsonlite)

# 整理数据
rds_transform <- function(name = "程向英"){
system(paste0("/Users/mr.cheng/anaconda3/bin/xml2json -t xml2json -o ", name, ".json ", name, ".xml"))
fromJSON(paste0(name, ".json")) %>%
as.data.frame() %>%
filter(HealthData.Record..type %in% paste0("HKQuantityTypeIdentifier", c("StepCount", "DistanceWalkingRunning", "FlightsClimbed"))) %>%
transmute(
type = HealthData.Record..type %>%
gsub(pattern = "HKQuantityTypeIdentifier", replacement = "") %>%
factor(levels = c("StepCount", "DistanceWalkingRunning", "FlightsClimbed"),
labels = c("Step Count (count)", "Distance Walking & Running (km)", "Flights Climbed (count)")),
time = HealthData.Record..startDate %>%
substr(1, 19) %>%
ymd_hms(),
start = HealthData.Record..startDate %>%
substr(1, 10),
end = HealthData.Record..endDate %>%
substr(1, 10),
value = HealthData.Record..value %>%
as.numeric()) %>%
mutate(
year = time %>% year()
) %>%
filter(year == 2018) %>%
filter(start == end) %>%
select(-year, -start, -end) %>%
as_tibble() %>%
write_rds(paste0(name, ".rds"))
system(paste0("rm ", name, ".json"))
}

rds_transform("程向英")
rds_transform("程彦珍")
rds_transform("程振兴")
# 丁昕悦的xml文件需要把最后一个work标签删除才行
rds_transform("丁昕悦")
rds_transform("刘含笑")
rds_transform("伍海军")
rds_transform("周晓时")

用于应用生成的代码:

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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
library(shiny)
library(shinybulma)
library(hrbrthemes)
library(tidyverse)
library(lubridate)

scales = c("#31CF65", "#FC3C63", "#3273DC", '#FFDD57', '#31CF65', '#FC3C63', '#3273DC', '#FFDD57', "#31CF65", "#FC3C63", "#3273DC", '#FFDD57',"#31CF65", "#FC3C63", "#3273DC", '#FFDD57', '#31CF65', '#FC3C63', '#3273DC', '#FFDD57', "#31CF65", "#FC3C63", "#3273DC", '#FFDD57')

shinyApp(
ui = bulmaPage(
bulmaHero(
fullheight = T,
color = "danger",
bulmaHeroBody(
bulmaContainer(
bulmaTitle("2018 · Footprint"),
bulmaSubtitle("Health Data Analysis Report "),
bulmaFigure("https://www.czxa.top/stepcount/img/playground.png")
)
)
),
bulmaHero(
fullheight = T,
color = "primary",
bulmaHeroBody(
bulmaContainer(
bulmaTitle("Overview"),
selectInput(
inputId = "name",
label = "",
choices = c("程振兴", "刘含笑", "程向英",
"程彦珍", "丁昕悦", "伍海军",
"周晓时"),
selected = "程振兴"
),
bulmaTileAncestor(
bulmaTileParent(
plotOutput("pic1"),
color = "primary"
)
)
)
)
),
bulmaHero(
fullheight = T,
color = "link",
bulmaHeroBody(
bulmaContainer(
bulmaTitle("Monthly Review"),
bulmaTileAncestor(
bulmaTileParent(
bulmaTileChild(
bulmaSubtitle("Total Step Count (count)"),
HTML('<div id="pic2" class="shiny-plot-output" style="width: 100% ; height: 400px;"></div>'),
color = "danger"
)
),
bulmaTileParent(
vertical = T,
bulmaTileChild(
bulmaSubtitle("Total Distance, Walking or Running (km)"),
HTML('<div id="pic3" class="shiny-plot-output" style="width: 100% ; height: 160px"></div>'),
color = "success"
),
bulmaTileChild(
bulmaSubtitle("Total Flights Climbed (count)"),
HTML('<div id="pic4" class="shiny-plot-output" style="width: 100% ; height: 160px"></div>'),
color = "success"
)
)
)
)
)
),
bulmaHero(
fullheight = T,
color = "info",
bulmaHeroBody(
bulmaContainer(
bulmaTitle("Weekly Review"),
bulmaTileAncestor(
bulmaTileParent(
bulmaTileChild(
bulmaSubtitle("Average Step Count (count)"),
HTML('<div id="pic5" class="shiny-plot-output" style="width: 100% ; height: 400px;"></div>'),
color = "danger"
)
),
bulmaTileParent(
vertical = T,
bulmaTileChild(
bulmaSubtitle("Average Distance, Walking or Running (km)"),
HTML('<div id="pic6" class="shiny-plot-output" style="width: 100% ; height: 160px"></div>'),
color = "success"
),
bulmaTileChild(
bulmaSubtitle("Average Flights Climbed (count)"),
HTML('<div id="pic7" class="shiny-plot-output" style="width: 100% ; height: 160px"></div>'),
color = "success"
)
)
)
)
)
),
bulmaHero(
fullheight = T,
color = "warning",
bulmaHeroBody(
bulmaContainer(
bulmaTitle("Something Interesting"),
bulmaTimeline(
centered = T,
bulmaTimelineHeader(
text = "来看一下你的2018吧!",
size = "medium",
color = "primary"
),
bulmaTimelineItem(
color = "primary",
content_header = HTML('<a id="fcdate" class="shiny-text-output"></a>'),
content_body = HTML('这天,你爬了<a id="fcv" class="shiny-text-output"></a>层楼,创下了今年之最!'),
marker_color = "danger",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/stairs128.png"), marker_image_size = "32x32"
),
bulmaTimelineItem(
color = "primary",
content_header = HTML('<a id="dwdate" class="shiny-text-output"></a>'),
content_body = HTML('这天,你走了<a id="dwv" class="shiny-text-output"></a>千米,创下了今年最远的记录!'),
marker_color = "info",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/distance128.png"), marker_image_size = "32x32"
),
bulmaTimelineItem(
color = "primary",
content_header = HTML('<a id="scdate" class="shiny-text-output"></a>'),
content_body = HTML('这天,你走了<a id="scv" class="shiny-text-output"></a>步,真棒!'),
marker_color = "success",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/run128.png"), marker_image_size = "32x32"
),
bulmaTimelineHeader(
text = "2018总结",
size = "small",
color = "primary"
),
bulmaTimelineItem(
color = "primary",
content_header = HTML('今年全年'),
content_body = HTML('你一共走了<a id="scvt" class="shiny-text-output"></a>步,太厉害了!'),
marker_color = "success",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/run128.png"), marker_image_size = "32x32"
),
bulmaTimelineItem(
color = "primary",
content_header = HTML('今年全年'),
content_body = HTML('你一共走了<a id="dwvt" class="shiny-text-output"></a>千米,明年继续加油!'),
marker_color = "info",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/distance128.png"), marker_image_size = "32x32"
),
bulmaTimelineItem(
color = "primary",
content_header = HTML('今年全年'),
content_body = HTML('你一共爬了<a id="fcvt" class="shiny-text-output"></a>层楼梯,继续保持哦!'),
marker_color = "danger",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/stairs128.png"), marker_image_size = "32x32"
),

# 年度平均
bulmaTimelineItem(
color = "primary",
content_header = HTML('今年平均每天'),
content_body = HTML('你走了<a id="scva" class="shiny-text-output"></a>步,还不错!'),
marker_color = "success",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/run128.png"), marker_image_size = "32x32"
),
bulmaTimelineItem(
color = "primary",
content_header = HTML('今年平均每天'),
content_body = HTML('你走了<a id="dwva" class="shiny-text-output"></a>千米,蛮多的呢!'),
marker_color = "info",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/distance128.png"), marker_image_size = "32x32"
),
bulmaTimelineItem(
color = "primary",
content_header = HTML('今年平均每天'),
content_body = HTML('你爬了<a id="fcva" class="shiny-text-output"></a>层楼梯,这得什么时候才能到月球啊!'),
marker_color = "danger",
marker_image = T,
tags$img(src = "https://www.czxa.top/stepcount/img/stairs128.png"), marker_image_size = "32x32"
),
bulmaTimelineHeader(text = "新的一年继续加油哦!", size = "medium", color = "primary")
)
)
)
),
bulmaHero(
fullheight = T,
color = "danger",
bulmaHeroBody(
bulmaContainer(
bulmaTitle("Compare with me ?"),
HTML('<h2 class="subtitle">我们的运动距离数的相关系数是(使用每个人的前300个观测值计算):<a id="corr" class="shiny-text-output"></a></h2>'),
bulmaTileParent(
plotOutput("pic8"),
color = "danger"
)
)
)
)
),
server = function(input, output) {
# 数据集选择
df <- reactive({
df <- read_rds(paste0(ifelse(input$name=="",
"程振兴",
input$name), ".rds"))
da1 <- df[which(df$type == "Distance Walking & Running (km)"),]
da2 <- df[which(df$type == "Flights Climbed (count)"),]
da3 <- df[which(df$type == "Step Count (count)"),]
da1 <- da1[!duplicated(da1$time),]
da2 <- da2[!duplicated(da2$time),]
da3 <- da3[!duplicated(da3$time),]
rbind(da1, da2, da3)
})
# 数据集之最大
df1 <- reactive({
df1 <- df() %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
arrange(desc(value)) %>%
slice(1:1) %>%
arrange(date) %>%
mutate(date = as.character.Date(date))
df1
})
# 年度汇总
df2 <- reactive({
df2 <- read_rds(paste0(ifelse(input$name=="",
"程振兴",
input$name), ".rds")) %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(year = year(date)) %>%
ungroup() %>%
group_by(type, year) %>%
summarise(value = sum(value)) %>%
arrange(type)
df2
})
# 和我比较的数据集
df3 <- reactive({
df3 <- read_rds("程振兴.rds") %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value))
df3
})
df4 <- reactive({
df4 <- read_rds(paste0(ifelse(input$name=="程振兴",
"刘含笑",
input$name), ".rds")) %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value))
df4
})
# 年度平均
df5 <- reactive({
df5 <- read_rds(paste0(ifelse(input$name=="",
"程振兴",
input$name), ".rds")) %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(year = year(date)) %>%
ungroup() %>%
group_by(type, year) %>%
summarise(value = mean(value, na.rm = T)) %>%
ungroup() %>%
arrange(type)
df5
})
# 文本渲染
output$corr <- renderText({
cor(df4()[which(df4()$type == "Distance Walking & Running (km)"),]$value[1:300], df3()[which(df3()$type == "Distance Walking & Running (km)"),]$value[1:300])
})
output$fcv <- renderText({
df1()[which(df1()$type == "Flights Climbed (count)"),]$value
})
output$dwv <- renderText({
df1()[which(df1()$type == "Distance Walking & Running (km)"),]$value
})
output$scv <- renderText({
df1()[which(df1()$type == "Step Count (count)"),]$value
})

output$fcdate <- renderText({
df1()[which(df1()$type == "Flights Climbed (count)"),]$date
})
output$dwdate <- renderText({
df1()[which(df1()$type == "Distance Walking & Running (km)"),]$date
})
output$scdate <- renderText({
df1()[which(df1()$type == "Step Count (count)"),]$date
})

output$scvt <- renderText({
df2()[which(df2()$type == "Step Count (count)"),]$value
})
output$dwvt <- renderText({
df2()[which(df2()$type == "Distance Walking & Running (km)"),]$value
})
output$fcvt <- renderText({
df2()[which(df2()$type == "Flights Climbed (count)"),]$value
})

output$scva <- renderText({
df5()[which(df5()$type == "Step Count (count)"),]$value
})
output$dwva <- renderText({
df5()[which(df5()$type == "Distance Walking & Running (km)"),]$value
})
output$fcva <- renderText({
df5()[which(df5()$type == "Flights Climbed (count)"),]$value
})

# 绘图
# overview
pic1 <- reactive({
pic1 <- df() %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(
month = date %>% month()
) %>%
ggplot(aes(x = date, y = value, fill = factor(month))) +
geom_col() +
facet_wrap( ~ type, ncol = 1, scales = "free_y") +
guides(fill = "none") +
scale_fill_manual(values = scales) +
scale_x_date(breaks = c("2018-01-01", "2018-05-01",
"2018-09-01", "2019-01-01") %>% ymd()) +
theme_ipsum() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank())
print(pic1)
})
# monthly review - step count
pic2 <- reactive({
pic2 <- df() %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(
month = date %>% month()
) %>%
group_by(month, type) %>%
summarise(value = sum(value)) %>%
filter(type == "Step Count (count)") %>%
ggplot(aes(factor(month), value, fill = factor(month))) +
geom_col() +
scale_fill_manual(values = scales) +
theme_ipsum() +
theme(legend.position = "none") +
labs(x = "month", y = "Total step count (count)") +
scale_y_comma() +
coord_flip()
print(pic2)
})
# monthly review - distance
pic3 <- reactive({
pic3 <- df() %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(
month = date %>% month()
) %>%
group_by(month, type) %>%
summarise(value = sum(value)) %>%
filter(type == "Distance Walking & Running (km)") %>%
ggplot(aes(factor(month), value, fill = factor(month))) +
geom_col() +
scale_fill_manual(values = scales) +
theme_ipsum() +
theme(legend.position = "none") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank())
print(pic3)
})
# monthly review - floors
pic4 <- reactive({
pic4 <- df() %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(
month = date %>% month()
) %>%
group_by(month, type) %>%
summarise(value = sum(value)) %>%
filter(type == "Flights Climbed (count)") %>%
ggplot(aes(factor(month), value, fill = factor(month))) +
geom_col() +
scale_fill_manual(values = scales) +
theme_ipsum() +
theme(legend.position = "none") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank())
print(pic4)
})
# weekly review - step count
pic5 <- reactive({
pic5 <- df() %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(
week = date %>% wday()
) %>%
group_by(week, type) %>%
summarise(value = mean(value)) %>%
filter(type == "Step Count (count)") %>%
ggplot(aes(factor(week), value, fill = factor(week))) +
geom_col() +
scale_fill_manual(values = scales) +
theme_ipsum() +
theme(legend.position = "none") +
scale_x_discrete(
breaks = 1:7,
labels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
) +
coord_flip() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank())
print(pic5)
})
# weekly review - distance
pic6 <- reactive({
pic6 <- df() %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(
week = date %>% wday()
) %>%
group_by(week, type) %>%
summarise(value = mean(value)) %>%
filter(type == "Distance Walking & Running (km)") %>%
ggplot(aes(factor(week), value, fill = factor(week))) +
geom_col() +
scale_fill_manual(values = scales) +
theme_ipsum() +
theme(legend.position = "none") +
scale_x_discrete(
breaks = 1:7,
labels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank())
print(pic6)
})
# weekly review - floors
pic7 <- reactive({
pic7 <- df() %>%
mutate(
date = time %>% date()
) %>%
group_by(type, date) %>%
summarise(value = sum(value)) %>%
mutate(
week = date %>% wday()
) %>%
group_by(week, type) %>%
summarise(value = mean(value)) %>%
filter(type == "Flights Climbed (count)") %>%
ggplot(aes(factor(week), value, fill = factor(week))) +
geom_col() +
scale_fill_manual(values = scales) +
theme_ipsum() +
theme(legend.position = "none") +
scale_x_discrete(
breaks = 1:7,
labels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank())
print(pic7)
})
# compare
pic8 <- reactive({
pic8 <- ggplot() +
geom_line(data = df3(), aes(x = date, y = value), color = "#00d1b2") +
geom_line(data = df4(), aes(x = date, y = value), color = "#ff3860") +
facet_wrap(~ type, ncol = 1, scales = "free_y") +
scale_x_date(breaks = c("2018-01-01", "2018-05-01",
"2018-09-01", "2019-01-01") %>% ymd()) +
theme_ipsum() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank()) +
labs(title = "Red Line is Yours!")
print(pic8)
})
# 图片渲染
# overview
output$pic1 <- renderPlot({
print(pic1())
})
# monthly review - step count
output$pic2 <- renderPlot({
print(pic2())
})
# monthly review - distance
output$pic3 <- renderPlot({
print(pic3())
})
# monthly review - floors
output$pic4 <- renderPlot({
print(pic4())
})
# weekly review - step count
output$pic5 <- renderPlot({
print(pic5())
})
# weekly review - distance
output$pic6 <- renderPlot({
print(pic6())
})
# weekly review - floors
output$pic7 <- renderPlot({
print(pic7())
})
# compare
output$pic8 <- renderPlot({
print(pic8())
})
}
)

Stata 命令

之前我也写过一个处理健康数据文件的 Stata 命令:czxah–处理 iPhone 手机导出的健康数据文件,不过经过这次的再次探索,发现那个命令对数据的处理方法是有些瑕疵的,因此不再建议使用了。

# Shiny

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×