how_far | R Documentation |
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.
how_far(on_date = Sys.Date(), due_date = NULL, person = NULL)
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:
|
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()
.
Invisibly returns the number of days along in the pregnancy. Prints a formatted message to the console with pregnancy progress information.
pregnancy.due_date: Date object setting default due date
pregnancy.person: Character string setting default person
set_due_date()
, set_person()
# 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.