forked from mganjoo/apple-health-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotHeartRate.R
54 lines (49 loc) · 1.53 KB
/
plotHeartRate.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
library(dplyr)
library(feather)
library(ggplot2)
library(lubridate)
library(shiny)
source("loadHealthData.R")
heartRateData <- loadHealthData() %>%
filter(type == "HKQuantityTypeIdentifierHeartRate") %>%
mutate(dayOnly = as.Date(format(endDate, "%Y-%m-%d")))
ui <- fluidPage(
titlePanel("Plot Heart Rate"),
sidebarLayout(
sidebarPanel(
helpText("View heart rate plot across a date range (granularity of 1 day)"),
dateRangeInput("dateRange", label = "Date range",
min = min(heartRateData$dayOnly),
max = max(heartRateData$dayOnly),
start = max(heartRateData$dayOnly),
end = max(heartRateData$dayOnly))
),
mainPanel(
plotOutput("heartRate"),
tableOutput("dataSummary")
)
)
)
server <- function(input, output) {
heartRateDataForRange <- reactive({
begin <- as.Date(input$dateRange[1])
end <- as.Date(input$dateRange[2]) + days(1)
heartRateData %>% filter(begin <= endDate & endDate < end)
})
output$heartRate <- renderPlot({
heartRateDataForRange() %>%
ggplot(aes(x = endDate, y = value)) + geom_line() +
labs(x = "Date", y = "Heart rate (bpm)")
})
output$dataSummary <- renderTable({
heartRateDataForRange() %>%
summarize(
earliestRecord = as.character(min(endDate)),
latestRecord = as.character(max(endDate)),
maxHeartRate = max(value),
minHeartRate = min(value),
numMeasurements = n()
)
})
}
shinyApp(ui = ui, server = server)