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.
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.
⌘+C
loc<-"导出.xml"# enter file path location of export.xml file here #xml <-xmlParse(loc)rc <-data.frame(XML:::xmlAttrsToDataFrame(xml["//Record"]), stringsAsFactors = F)saveRDS(rc, "export_health_care.rds")
⌘+C
rc <-readRDS("export_health_care.rds")## Filter useful dataapple_watch <- rc %>%filter(grepl("JZ",sourceName), unit =='count/min', type =="HKQuantityTypeIdentifierRestingHeartRate")# Adjusting to Local Timezoneapple_watch_dt <- apple_watch %>%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()
---title: Health Care Data Analysis with Apple Watchauthor: 'Jihong Zhang'date: '2021-07-06'draft: falseprivate: trueslug: health-care-data-analysis-with-apple-watchcategories: - blog - R - healthsummary: "Analyze the health care data from Apple Watch's app"lastmod: '2021-07-06T14:02:09-05:00'featured: no---> 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](https://osxdaily.com/2019/05/20/export-health-data-from-iphone/) and [How to Export, Parse and Explore Your Apple Health Data with Python](http://www.markwk.com/data-analysis-for-apple-health.html).## Load Required Packages```{r setup, comment=FALSE, echo=FALSE, results="hide", comment=FALSE}knitr::opts_chunk$set( echo = TRUE, result = 'hide', eval = FALSE, message=FALSE # suppress message from packages and warning)library(knitr)``````{r, eval=TRUE}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")```## Read in XML dataThe .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.```{r, eval=FALSE}loc<-"导出.xml" # enter file path location of export.xml file here #xml <- xmlParse(loc)rc <- data.frame(XML:::xmlAttrsToDataFrame(xml["//Record"]), stringsAsFactors = F)saveRDS(rc, "export_health_care.rds")``````{r, eval=TRUE}rc <- readRDS("export_health_care.rds")## Filter useful dataapple_watch <- rc %>% filter(grepl("JZ",sourceName), unit == 'count/min', type == "HKQuantityTypeIdentifierRestingHeartRate")# Adjusting to Local Timezoneapple_watch_dt <- apple_watch %>% 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()``````{r, eval=TRUE, results='asis'}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)```## Other materialsThere a decent [blog](https://www.inpredictable.com/2021/06/analyzing-your-run-data-with-r.html) illustrating how to handle with apple watch export file.