3 min read

New Year, Breaker of Pipelines

Previously, I blogged on my Accidental RAP.

Today I ran it for the first time in 2021, and two things broke. I didn’t show the code last time, mostly because it’s a long report anyway. I’m going to hide most of the code in this one to show off the errors.

There was one hardcoded reference to 2020, so this graph was just wrong.

this_year <- year(today()) #Good so far

dummy <- list(name = activities, 
                actiondate = seq(ymd("2020-01-01"), today(), by=1), #Oh, there's the hardcoded 2020
                volume = 0) %>%
  cross_df() %>%
  mutate(actiondate = as_date(actiondate))

volume <- data %>%
  filter(str_detect(name, activities_regex)) %>%
  mutate(kg = extract_numbers(string_metric, "kg")) %>%
  mutate(reps = extract_numbers(string_metric, "reps")) %>%
  filter(!is.na(kg), !is.na(reps)) %>%  
  filter(year(actiondate) == this_year) %>%
  mutate(kg = if_else(str_detect(name, "Dumbbell"), kg*2, kg) ) %>%
  mutate(volume = kg * reps) %>%
  mutate(name = str_extract(name, activities_regex)) %>% 
  arrange(actiondate) %>%
  bind_rows(dummy) %>%
  group_by(name, actiondate) %>%
  summarise(volume=sum(volume)) %>%
  mutate(cumulative_volume=cumsum(volume)) %>%

  ungroup() 


volume %>%
  filter(name != activities[3]) %>%
  mutate(cumulative_volume = cumulative_volume/1000) %>%
 
  ggplot(aes(x=actiondate, y=cumulative_volume, fill=name)) + geom_area() +
    labs(title = str_glue("Cumulative Volume for Powerlifts in {this_year}"),
       y="Volume (tonne reps)") +
    ggthemes::theme_tufte() + ggthemes::scale_fill_few()

At least the title was right…

Then on the GitHub-style activity heatmaps:

data %>%
  select(actiondate) %>%
  
  filter(year(actiondate)==this_year) %>%
  group_by(actiondate) %>%
  summarise(n=1) %>%
  mutate(week = isoweek(actiondate), weekday=wday(actiondate, label = T)) %>%
  ggplot(aes(x=weekday,y=week, fill=n)) + geom_tile() + ggthemes::theme_tufte() + coord_flip() + 
  scale_y_discrete()  + theme(legend.position = "none") +
  labs(title = "Days where I logged a workout this year")

Oh. 2021-01-01 1 is week 53 of 2020. That’s… not what I wanted.

The first fix is

  actiondate = seq(ymd("2020-01-01"), today(), by=1), #Oh, there's the hardcoded 2020

to

  actiondate = seq(floor_date(today(), "year"), today(), by=1), #Oh, there's the hardcoded 2020

The second one was easier - search and replace “isoweek” with “week”.

Lessons learned

New Years break RAPs. You think you’ve got it working, but you go back and change a thing, and forget to be as robust. Especially after a New Year, check the outputs for sanity.

Sometimes you think you’ve understood the documentation about the difference between week and isoweek and still get it wrong.

This script is a small bit of fun motivation for me, it’s easier to wait for errors to arise then to write iron-clad code. If the stakes were higher I’d be looking at the Aqua Book. (Which, to be fair, has commentary on QA being proportionate. ‘Almost None’ is proportionate for this script.)

Postscript

One proposed suggestion was “week 53 does not exist, because betwixtmass”. I want to see if the winter feasting had a noticable effect on lifting:

data %>%
  filter(str_detect(name, activities_regex)) %>%
  filter(actiondate >= ymd("2020-11-01"))  %>% #Arbitrary choice
   
  mutate(kg = extract_numbers(string_metric, "kg")) %>%
  mutate(reps = extract_numbers(string_metric, "reps")) %>%
  filter(!is.na(kg), !is.na(reps)) %>%  
  mutate(kg = if_else(str_detect(name, "Dumbbell"), kg*2, kg) ) %>% #Correction factor for dumbbells
  mutate(volume = kg * reps) %>%
  mutate(name = str_extract(name, activities_regex)) %>% 
  arrange(actiondate) %>%
  mutate( week = isoweek(actiondate)) %>%
  mutate(week = if_else(week<40, week+53, week)) %>% # Pretend week 1 2021 is week 54 of 2020. 
  group_by(week, name) %>% 
  summarise(volume=sum(volume)/1000) %>% 
  ungroup() %>% 
  mutate(betwixtmass = (week==53)) %>% 

  ggplot(aes(x=0,xend=volume,y=week,
             colour = betwixtmass)) + ggalt::geom_dumbbell() + facet_wrap("name", ncol=2) +  
  labs(x="weekly volume (tonne reps)") +
  ggthemes::theme_tufte() + ggthemes::scale_colour_few()

No major difference in workout volume in betwixtmass and general. Let’s say “at least I was consistent” :D.


  1. Around here we do Year-Month-Day, because it makes alphabetical file sorting work.↩︎