Learn R Programming

pregnancy (version 0.2.1)

how_far: Calculate pregnancy progress and time remaining

Description

Calculates and displays how far along a pregnancy is on a specific date, including weeks pregnant, days remaining until due date, and overall progress percentage.

Usage

how_far(on_date = Sys.Date(), due_date = NULL, person = NULL)

Value

Invisibly returns the number of days along in the pregnancy. Prints a formatted message to the console with pregnancy progress information.

Arguments

on_date

Date or character string representing a date, e.g. "YYYY-MM-DD". The date for which to calculate pregnancy progress. Defaults to current system date.

due_date

Date or character string representing a date, e.g. "YYYY-MM-DD". The expected due date. If NULL, will try to use the "pregnancy.due_date" option. Required if option not set.

person

The person who is pregnant, to determine the grammar for the output message. Can be:

  • "I", "1", "1st", "first", or numeric 1 for first person

  • "you", "2", "2nd", "second", or numeric 2 for second person

  • Any other name for third person

  • NULL: will try to use the "pregnancy.person" option. Defaults to "You" if the option is set.

Global Options

  • pregnancy.due_date: Date object setting default due date

  • pregnancy.person: Character string setting default person

Details

The function assumes a standard pregnancy length of 280 days (40 weeks) when calculating progress. It handles past, present, and future dates appropriately by adjusting message grammar. If the calculation shows more than 42 weeks of pregnancy, a different message is displayed noting this unusual duration.

The function uses the cli package for formatted message output and supports proper pluralization of weeks/days in messages.

If on_date or due_date are character strings, the conversion to a Date is handled by anytime::anydate().

See Also

set_due_date(), set_person()

Examples

Run this code
# Current progress with explicit due date
# Note that output will depend on date the function is run
how_far(due_date = "2025-12-01")

# Progress on a specific date
how_far(on_date = "2025-11-01", due_date = "2025-12-01")

# With custom person
how_far(on_date = "2025-11-01", due_date = "2025-12-01", person = "Sarah")

# Set global options
date_opt <- getOption("pregnancy.due_date") # save current option
set_due_date("2025-12-01")
how_far()
options(pregnancy.due_date = date_opt) # return original option

Run the code above in your browser using DataLab