f <- list.files("~/R/Quantified Self", pattern = "_export") %>% stringr::str_extract(.,
"\\d{4}-\\d{2}-\\d{2}") %>% lubridate::ymd() %>% max() %>% {
list.files("~/R/Quantified Self", pattern = as.character(.))
}
IT <- readr::read_csv(f, col_names = c("Started At", "Duration", "Activity", "Preset"),
skip = 1, col_types = "cccc") %>% dplyr::mutate_at("Started At", lubridate::mdy_hms) %>%
filter(complete.cases(`Started At`))
# make date
googlesheets4::gs4_auth(email = "sholsen@alumni.emory.edu")
itgs <- googlesheets4::gs4_get("https://docs.google.com/spreadsheets/d/19Pz41A0dq0DsJIBuFO0VOw0fXAmzxO7UOa7bHKcIgYQ/edit?usp=sharing")
# YM <- googlesheets::gs_read(itgs, ws = 'Yoga/Meditation Exp') YM %<>%
# mutate_at(vars(`Started At`),.f = funs(lubridate::mdy_hms)) %>%
# filter(complete.cases(`Started At`)) names(IT) <- YM %>% names %>% .[1:4]
# IT.add_df <- IT %>% filter(`Started At` > {YM$`Started At` %>% max})
# googlesheets::gs_add_row(itgs,ws='Yoga/Meditation Exp',input=IT.add_df)
# ----------------------- Mon Apr 30 17:00:01 2018 ------------------------# Read
# merged data back in
.sn <- itgs$sheets$name[stringr::str_which(itgs$sheets$name, "Yoga/Med")]
YM <- googlesheets4::read_sheet(itgs, sheet = .sn, col_types = "cccc", range = "A:D")
YM <- dplyr::bind_rows(YM, setNames(IT %>% dplyr::mutate_at("Started At", as.character),
names(YM))) %>% dplyr::mutate_at("Started At", lubridate::as_datetime)
YM <- dplyr::distinct(YM, `Started At`, .keep_all = T) %>% dplyr::arrange(`Started At`)
YM <- YM %>% dplyr::mutate_at(dplyr::vars(Duration), ~{
.t <- tibble::as_tibble(stringr::str_split(., ":", simplify = T)) %>% setNames(c("hours",
"minutes", "seconds")) %>% dplyr::mutate_all(as.numeric)
out <- do.call(c, purrr::pmap(.t, ~do.call(lubridate::duration, rlang::list2(...))))
})
.ym <- YM %>% dplyr::mutate_at(dplyr::vars(Activity), ~dplyr::recode_factor(., `Guided Meditation` = "Meditation",
Chanting = "Meditation", Breathing = "Meditation", Walking = "Aerobic")) %>%
dplyr::filter(Activity %in% c("Meditation", "Yoga", "Aerobic")) %>% dplyr::mutate_at(dplyr::vars(Activity),
~forcats::fct_relevel(., levels = c("Meditation", "Yoga", "Aerobic")))
YMSummary <- .ym %>% mutate(WN = lubridate::week(`Started At`), YR = lubridate::year(`Started At`),
DY = lubridate::day(`Started At`)) %>% group_by(Activity, YR, WN, DY) %>% summarise(Total = sum(Duration),
AvgSessionLength = mean(Duration), SessxDay = n()) %>% ungroup(DY) %>% summarize(AvgTimepDay = mean(Total),
Total = sum(Total), AvgTimepSession = mean(AvgSessionLength), SessxDay = mean(SessxDay))
tags$p("Total Number of Sessions by Type:")
.ym %>% dplyr::group_by(Activity) %>% dplyr::summarise(Count = n())
# save(YMSummary,file='YMSummary.RData')
Total Number of Sessions by Type:
# load(file='YMSummary.RData')
tags$p("Total Number of Hours by Type")
tags$em("Meditation & Yoga tracked since 2013, Aerobic since 2017")
YMSummary %>% group_by(Activity) %>% summarise(Cumulative.hrs = sum(Total)/3600)
Total Number of Hours by Type
Meditation & Yoga tracked since 2013, Aerobic since 2017yLabels <- rlang::as_function(~lubridate::seconds_to_period(as.numeric(.x)))
YRSummary <- YMSummary %>% filter(Activity == "Meditation") %>% group_by(YR) %>%
summarize(AvgTimepDay = mean(AvgTimepDay), AvgTimepSession = mean(AvgTimepSession),
SessxDay = mean(SessxDay), y = qnorm(0.65, mean(quantile(Total)), sd(quantile(Total))),
x = qnorm(0.6, mean(quantile(WN)), sd(quantile(WN))), max = max(Total))
ggplot(YMSummary, ggplot2::aes(x = WN, y = Total, color = Activity)) + geom_line() +
facet_wrap(~YR, ncol = 1, scales = "free") + geom_text(YRSummary, mapping = ggplot2::aes(x = x,
y = y, label = paste0("Time/Day:", AvgTimepDay %>% round %>% lubridate::seconds_to_period(),
"\n", "Time/Session:", AvgTimepSession %>% round %>% lubridate::seconds_to_period(),
"\n", "Sessions/Day:", SessxDay %>% round(2))), color = "black", size = rel(3)) +
scale_x_continuous(breaks = seq(1, 52, by = 4), minor_breaks = 1:52) + scale_y_continuous(labels = yLabels) +
labs(title = "Primary Health Behaviors: All Tracked Time", subtitle = "Facet by Year",
caption = "", x = "Week Number", y = "Duration") + theme_grey(base_size = 10) +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5),
axis.text.y = element_text(angle = 0), strip.switch.pad.wrap = margin(0,
"pt"), strip.text.x = element_text(margin = margin(0, 0, 0, 0, "cm")),
panel.spacing.x = unit(0.8, "lines"), panel.spacing.y = unit(0, "lines"))