From e58d74230eaac83600ea1949946aaa980cc578b3 Mon Sep 17 00:00:00 2001 From: Kazuharu Yanagimoto Date: Fri, 26 Apr 2024 19:24:23 +0200 Subject: [PATCH] Update of format_date --- R/utils.R | 35 ++++++++++++++++++++----------- vignettes/awesomecv/awesomecv.qmd | 2 +- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/R/utils.R b/R/utils.R index d9be06f..e63da4a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -2,9 +2,9 @@ #' #' @param data Data frame for resume entry #' @param start Start date column name. Default is "start" -#' @param end End date column name. Default is "end" -#' @param sep Separator between start and end date. Default is "-" -#' @param format Date format. Default is "%M %Y" +#' @param end Optional. End date column name. +#' @param sep Separator between start and end date. Default is " - " +#' @param date_format Date format. Default is "%M %Y" #' @param colname_date Name for the generated date column. Default is "date" #' @param replace_na Value to replace NA values. Default is "Present" #' @param sort_by Sort by "none", "start" or "end". Default is "none" @@ -15,30 +15,41 @@ #' #' @examples #' work |> -#' format_date(format = "%Y", sort_by = "start") |> +#' format_date(end = "end", date_format = "%Y", sort_by = "start") |> #' resume_entry() #' -format_date <- function(data, start = "start", end = "end", sep = "-", - format = "%M %Y", + +format_date <- function(data, + start = "start", + end = NULL, + sep = " - ", + date_format = "%M %Y", colname_date = "date", replace_na = "Present", sort_by = "none", decreasing = TRUE) { - data[[start]] <- format(data[[start]], format) - data[[end]] <- format(data[[end]], format) - # Replace NA values + data[[start]] <- format(data[[start]], date_format) data[[start]][is.na(data[[start]])] <- replace_na - data[[end]][is.na(data[[end]])] <- replace_na # Create date column - data[[colname_date]] <- paste(data[[start]], data[[end]], sep = sep) + if (is.null(end)) { + data[[colname_date]] <- data[[start]] + } else { + data[[end]] <- format(data[[end]], date_format) + data[[end]][is.na(data[[end]])] <- replace_na + data[[colname_date]] <- paste(data[[start]], data[[end]], sep = sep) + } # Sort if (sort_by == "start") { data <- data[order(data[[start]], decreasing = decreasing), ] - } else if (sort_by == "end") { + } else if (sort_by == "end" && !is.null(end)) { data <- data[order(data[[end]], decreasing = decreasing), ] + } else if (sort_by == "none") { + # Do nothing + } else { + stop("Invalid sort_by value. Use 'none', 'start' or 'end'") } return(data) diff --git a/vignettes/awesomecv/awesomecv.qmd b/vignettes/awesomecv/awesomecv.qmd index 5c7030c..5190f24 100644 --- a/vignettes/awesomecv/awesomecv.qmd +++ b/vignettes/awesomecv/awesomecv.qmd @@ -57,7 +57,7 @@ resume_entry(pub) ```{r} #| output: asis work |> - format_date(format = "%Y", sort_by = "start") |> + format_date(end = "end", date_format = "%Y", sort_by = "start") |> resume_entry() ```