From 64dd50ce2807a5f0ff1705a159fb556d34f0c2b4 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Fri, 1 Jul 2022 13:29:10 +0100 Subject: [PATCH] refactor: streamline pad functions Remove stringr dependency to satisfy R CMD checks --- NAMESPACE | 1 + R/plot_flex_index.R | 2 +- R/plot_hourly_pat.R | 3 +-- R/supporting_functions.R | 16 ++++++++++++++ R/workpatterns_area.R | 8 ------- R/workpatterns_hclust.R | 8 ------- R/workpatterns_rank.R | 2 +- man/pad2.Rd | 17 ++++++++++++++ man/plot_hourly_pat.Rd | 48 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 85 insertions(+), 20 deletions(-) create mode 100644 man/pad2.Rd create mode 100644 man/plot_hourly_pat.Rd diff --git a/NAMESPACE b/NAMESPACE index 006691a9..ea1ca6a2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -147,6 +147,7 @@ export(one2one_summary) export(one2one_trend) export(p2p_data_sim) export(p_test) +export(pad2) export(pairwise_count) export(period_change) export(personas_hclust) diff --git a/R/plot_flex_index.R b/R/plot_flex_index.R index 0c5346bb..abbb81e0 100644 --- a/R/plot_flex_index.R +++ b/R/plot_flex_index.R @@ -151,7 +151,7 @@ plot_flex_index <- function(data, ) ## 00, 01, 02, etc. - hours_col <- stringr::str_pad(seq(0,23), width = 2, pad = 0) + hours_col <- pad2(x = seq(0,23)) # Use `mutate()` method # Will get 10 IDs, not 10 rows diff --git a/R/plot_hourly_pat.R b/R/plot_hourly_pat.R index 07064929..2befacdd 100644 --- a/R/plot_hourly_pat.R +++ b/R/plot_hourly_pat.R @@ -22,7 +22,6 @@ #' @param legend_text String to be used in the bottom legend label. #' #' @param rows Number of rows to show in plot. -#' @noRd #' #' @export @@ -41,7 +40,7 @@ plot_hourly_pat <- function( ){ ## 00, 01, 02, etc. - hours_col <- stringr::str_pad(seq(0,23), width = 2, pad = 0) + hours_col <- pad2(x = seq(0,23)) data %>% utils::head(rows) %>% diff --git a/R/supporting_functions.R b/R/supporting_functions.R index 7fbddb96..c2bcc31e 100644 --- a/R/supporting_functions.R +++ b/R/supporting_functions.R @@ -251,3 +251,19 @@ wrap_text <- function(x, threshold = 15){ x = x ) } + +#' @title +#' Create the two-digit zero-padded format +#' +#' @param x numeric value or vector with maximum two characters. +#' +#' @return +#' Numeric value containing two-digit zero-padded values. +#' +#' @export + +pad2 <- function(x){ + x <- as.character(x) + + ifelse(nchar(x) == 1, paste0("0", x), x) +} diff --git a/R/workpatterns_area.R b/R/workpatterns_area.R index 1a265733..4bfd1fde 100644 --- a/R/workpatterns_area.R +++ b/R/workpatterns_area.R @@ -191,14 +191,6 @@ workpatterns_area <- function(data, mutate(Signals = sub(pattern = "_\\d.+", replacement = "", x = Signals)) %>% spread(Signals, Value) - ## Create the two-digit zero-padded format - ## Used in `scale_x_continuous()` - pad2 <- function(x){ - x <- as.character(x) - - ifelse(nchar(x) == 1, paste0("0", x), x) - } - ## Return if(return == "data"){ diff --git a/R/workpatterns_hclust.R b/R/workpatterns_hclust.R index 85d8c20c..b9a743b4 100644 --- a/R/workpatterns_hclust.R +++ b/R/workpatterns_hclust.R @@ -295,14 +295,6 @@ plot_signal_clust <- function(data, mutate_at("Hours", ~sub(pattern = paste0(sig_label, "_"), replacement = "", x = .)) %>% mutate_at("Hours", ~sub(pattern = "_.+", replacement = "", x = .)) - ## Create the two-digit zero-padded format - ## Used in `scale_x_continuous()` - pad2 <- function(x){ - ifelse(nchar(x) == 1, - paste0(0, x), - x) - } - ## bar plot output_bar <- plot_data %>% diff --git a/R/workpatterns_rank.R b/R/workpatterns_rank.R index 9eeece77..0b225cfb 100644 --- a/R/workpatterns_rank.R +++ b/R/workpatterns_rank.R @@ -171,7 +171,7 @@ workpatterns_rank <- function(data, by = num_cols] ## 00, 01, 02, etc. - hours_col <- stringr::str_pad(seq(0,23), width = 2, pad = 0) + hours_col <- pad2(x = seq(0,23)) # Wide table showing proportion of signals by hour # Ranked descending by `WeekCount` diff --git a/man/pad2.Rd b/man/pad2.Rd new file mode 100644 index 00000000..f0052bc9 --- /dev/null +++ b/man/pad2.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/supporting_functions.R +\name{pad2} +\alias{pad2} +\title{Create the two-digit zero-padded format} +\usage{ +pad2(x) +} +\arguments{ +\item{x}{numeric value or vector with maximum two characters.} +} +\value{ +Numeric value containing two-digit zero-padded values. +} +\description{ +Create the two-digit zero-padded format +} diff --git a/man/plot_hourly_pat.Rd b/man/plot_hourly_pat.Rd new file mode 100644 index 00000000..82c3f6c6 --- /dev/null +++ b/man/plot_hourly_pat.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_hourly_pat.R +\name{plot_hourly_pat} +\alias{plot_hourly_pat} +\title{Internal function for plotting the hourly activity patterns.} +\usage{ +plot_hourly_pat( + data, + start_hour, + end_hour, + legend, + legend_label, + legend_text = "Observed activity", + rows, + title, + subtitle, + caption, + ylab = paste("Top", rows, "activity patterns") +) +} +\arguments{ +\item{data}{Data frame containing three columns: +\itemize{ +\item \code{patternRank} +\item \code{Hours} +\item \code{Freq} +}} + +\item{start_hour}{Numeric value to specify expected start hour.} + +\item{end_hour}{Numeric value to specify expected end hour.} + +\item{legend}{Data frame containing the columns: +\itemize{ +\item \code{patternRank} +\item Any column to be used in the grey label box, supplied to \code{legend_label} +}} + +\item{legend_label}{String specifying column to display in the grey label +box} + +\item{legend_text}{String to be used in the bottom legend label.} + +\item{rows}{Number of rows to show in plot.} +} +\description{ +This is used within \code{plot_flex_index()} and \code{workpatterns_rank()}. +}