R/update_log.R

#' Update running log to track running data
#' @param units 'english' or 'metric'.
#' @param name User's name. Should be in character format
#' @param date Date of run. Should be in "mm/dd/yyyy" format.
#' @param age User's age. Should be in numeric format.
#' @param gender User's gender. Should be in character format, and should either be 'male' or 'female'.
#' @param height User's height. Should be in numeric format.
#' @param weight User's weight. Should be in numeric format.
#' @param time The amount of time the user ran the distance in. Measured in minutes. Should be in character format.
#' @param distance The distance the user ran. Measured in miles. Should be in numeric format.
#' @param heartrate Optional parameter - user's heartrate during workout. Should be in numeric format.
#' @return Updated dataframe with runner's info.
#' @import lubridate
#' @examples
#' update_log("english", "09/30/2016", "Anna", 18, "female", 57, 128, 10, 1)
#' update_log("metric", "09/30/2016", "Jon", 19, "male", 57, 128, 10, 1, 72)
#' @export

update_log <- function(units = "english", date, name, age, gender, height, weight,
                       time, distance, heartrate = NA){

  if (class(name) != "character"){
    stop("Name must be a character!")
  }
  else{
    log <- readRDS(paste(name, ".rds", sep = ""))
  }

  if ((tolower(units) != "english" && tolower(units) != "metric") || class(date) != "character" ||
      class(age) != "numeric" || class(gender) != "character" || class(height) != "numeric" ||
      class(weight) != "numeric" || class(time) != "numeric" || class(distance) != "numeric" ||
      (class(heartrate) != "numeric" && !is.na(heartrate))) {
    stop("Incorrect class")
  }
  else {
    date <- mdy(date)

    tolower(name)
    tolower(gender)

    if(!is.na(heartrate)){
      c <- calories_burned(age, heartrate, weight, time, gender, units)
    }
    else {
      c <- NA
    }

    if (gender != "male" && gender != "female"){
      stop("gender input must be 'male' or 'female'")
    }
    else {
      newrow <- data.frame(Date = date,
                           Age = age,
                           Gender = gender,
                           Height = height,
                           Weight = weight,
                           Time = time,
                           Distance = distance,
                           Heart.Rate = heartrate,
                           Calories.Burned = c,
                           stringsAsFactors = FALSE)
      log <- rbind(log, newrow)
      saveRDS(log, paste(name, ".rds", sep = ""))
      View(log)
    }
  }
}
armaanshah96/runner documentation built on May 10, 2019, 1:39 p.m.