# R/eddington.r In eddington: Compute a Cyclist's Eddington Number

#### Documented in E_reqE_sat

#' @useDynLib eddington
#' @importFrom Rcpp sourceCpp
NULL

# Function definitions ------------------------------------------------------

#' Determine if a dataset satisfies a specified Eddington number
#'
#' Indicates whether a certain Eddington number is satisfied, given the data.
#'
#' @inheritParams E_num
#' @param candidate The Eddington number to test for.
#' @return A logical vector of length 1.
#' @export
E_sat <- function(rides, candidate) {

length(rides) >= candidate && sum(rides >= candidate) >= candidate

}

#' Determine the number of additional rides required to achieve a specified
#' Eddington number
#'
#' Determine the number of additional rides required to achieve a specified
#' Eddington number.
#'
#' @inheritParams E_num
#' @param candidate The Eddington number to test for.
#' @return An integer vector of length 1. Returns \code{0L} if \emph{E} is
#' @export
E_req <- function(rides, candidate) {

max(as.integer(candidate) - sum(rides >= candidate), 0L)

}

# Custom print methods -------------------------------------------------------
#' @export
print.E_next <- function(x, ...) {

out_string <- sprintf(
"Your current Eddington Number is %i. You need %i %s of %i or greater to get to an Eddington number of %i.",
x$E, x$req,
if (x$req > 1) "rides" else "ride", x$E + 1,
x\$E + 1
)

cat(strwrap(out_string), sep = "\n")

}


## Try the eddington package in your browser

Any scripts or data that you put into this service are public.

eddington documentation built on March 24, 2020, 5:08 p.m.