dfe-analytical-services / dfeR

Common R tasks in the Department for Education (DfE)
https://dfe-analytical-services.github.io/dfeR/
GNU General Public License v3.0
8 stars 2 forks source link

Add format readable function #60

Closed cjrace closed 3 months ago

cjrace commented 5 months ago

Is your feature request related to a problem? Please describe. We have some functions we use regularly to prettily format numbers that should come into here.

Describe the solution you'd like A suite of functions, likely pretty_*() that use our fmtRdb() and format_time_prettily() functions to give a function for easily formatting numbers ready for presentation.

Describe alternatives you've considered One big function, but that would be less readable and more difficult to write in a robust way.

Additional context Our team have this code scattered across repositories. I've tried to add in comments, but there'll likely be more about.

cjrace commented 5 months ago

Found the present time neatly function, will want reviewing and making sure it's generalisable.

# Function to present time neatly ----
present_time_neatly <- function(start_time, end_time) {

  # function designed to be used with Sys.time()

  raw_time <- round(as.numeric(end_time - start_time), 1)

  pretty_time <- if (raw_time < 119) {
    paste0(raw_time, " seconds")
  } else {
    if (raw_time < 7139) {
      pretty_minutes <- raw_time %/% 60
      pretty_seconds <- round(raw_time %% 60)

      paste0(pretty_minutes, " minutes ", pretty_seconds, " seconds")
    } else {
      pretty_hours <- raw_time %/% 3600
      pretty_minutes <- raw_time %/% 60 - pretty_hours * 60
      pretty_seconds <- round(raw_time %% 60)

      paste0(pretty_hours, " hours ", pretty_minutes, " minutes ", pretty_seconds, " seconds")
    }
  }
}
cjrace commented 5 months ago

Also found this related function on file size, could be combined with the above, or done separately as a suite of functions like so?

pretty_number()
pretty_time()
pretty_money()
pretty_file_size()

Links in with #64

present_file_size <- function(filesize) {
  if (is.null(filesize)) {} else {
    if (round(filesize / 1024 / 1024 / 1024, 2) >= 1) {
      return(paste0(round(filesize / 1024 / 1024 / 1024, 2), " GB"))
    } else {
      if (round(filesize / 1024 / 1024, 2) < 1) {
        return(paste0(round(filesize / 1024, 2), " Bytes"))
      } else {
        return(paste0(round(filesize / 1024 / 1024, 2), " MB"))
      }
    }
  }
}
cjrace commented 5 months ago

Another one to potentially add - pretty_percentage(). Might be worth having a version just for formatting, and then another version for creating percentages neatly.

percentage <- function(numerator, denominator){
  output <- numerator*100/denominator
  output[denominator==0 & numerator==0] <- 0
  return(format(output, nsmall=2, digits=4))
}
cjrace commented 5 months ago
fmtRdble <- function(value, prFx = "", sfFx = "", nsmall = 0, digits = 3) {
  value <- as.numeric(value)
  if (tolower(prFx) %in% c("sterling", "pounds")) {
    prFx <- "\U00a3"
  }
  if (is.finite(value)) {
    if (prFx %in% c("-", "+", "+/-")) {
      if (value >= 0) {
        prFx <- "+"
      } else {
        prFx <- "-"
      }
    }
    if (value >= 1.e9) {
      paste0(prFx, format(value / 1.e9, big.mark = ",", nsmall = nsmall, digits = digits), " billion", sfFx)
    } else if (value >= 1.e6) {
      paste0(prFx, format(value / 1.e6, big.mark = ",", nsmall = nsmall, digits = digits), " million", sfFx)
    } else {
      paste0(prFx, format(value, big.mark = ",", nsmall = nsmall, digits = digits), sfFx)
    }
  } else {
    paste0(prFx, "[data unavailable for this combination of geography/time period]", sfFx)
  }
}
cjrace commented 5 months ago

Have a look at making automatic links using the @family option in roxygen2