rm(list = ls(all = TRUE))
if(!require(XML)) install.packages("XML")
if(!require(tidyverse)) install.packages("tidyverse")
if(!require(lubridate)) install.packages("lubridate")
if(!require(scales)) install.packages("scales")
if(!require(ggthemes)) remotes::install_github(c("hadley/ggplot2", "jrnold/ggthemes"))
if(!require(ggridges)) remotes::install_github("wilkelab/ggridges")
if(!require(rpart)) install.packages("rpart")
if(!require(kableExtra)) install.packages("kableExtra")
The data contained within Health app on iPhone can be saved and exported for other uses. Perhaps you want to export Health app data to use in another health or fitness app, importing it to elsewhere, or maybe you want to use the raw Health data for your own purposes.
Exporting Health data from iPhone results in a zip archive that contains the raw data as gathered by Health app in XML format. This exported Health data will include any data stored or gathered by the Health app and any associated devices, including any Medical ID data, the native iPhone step counter and distance tracker, any data from an Apple Watch, and any data gathered from any third party devices that are syncing to Health app, like a smart scale or blood pressure monitor.
You can find more details about exporting data from How to Export Health Data form iPhone and How to Export, Parse and Explore Your Apple Health Data with Python.
1 Load Required Packages
2 Read in XML data
The .xml file could be downloaded and exported directly from iphone’s health app. To read in the XML file and transform it into data frame, XML
R package will help.
<-"导出.xml" # enter file path location of export.xml file here #
loc
<- xmlParse(loc)
xml
<- data.frame(XML:::xmlAttrsToDataFrame(xml["//Record"]), stringsAsFactors = F)
rc saveRDS(rc, "export_health_care.rds")
<- readRDS("export_health_care.rds")
rc
## Filter useful data
<- rc %>% filter(grepl("JZ",sourceName), unit == 'count/min',
apple_watch == "HKQuantityTypeIdentifierRestingHeartRate")
type
# Adjusting to Local Timezone
<- apple_watch %>%
apple_watch_dt mutate(cdt=as_datetime(creationDate, tz="US/Central"),
stm=as_datetime(startDate, tz="US/Central"),
etm=as_datetime(endDate, tz="US/Central"),
dst=as.numeric(as.character(value)))
# %>%
# group_by(creationDate) %>%
# mutate(TotalTime=cumsum(value)) %>% # cumulative distance covered #
# mutate(hr=hour(stm), min=minute(stm)) %>%
# mutate(elt=as.numeric(etm-mntm)) %>% # total elapsed time #
# mutate(dtm=as.numeric(etm-lag(etm))) %>%
# ungroup()
head(apple_watch_dt[,-1]) %>%
select(sourceName, unit, cdt, stm, etm, dst) %>%
kbl() %>%
kable_material_dark(full_width = F, html_font = "Maven Pro") |>
kable_styling(font_size = 10)
3 Other materials
There a decent blog illustrating how to handle with apple watch export file.