| Title: | Calculate and Track Dates and Medications During Pregnancy |
|---|---|
| Description: | Provides functionality for calculating pregnancy-related dates and tracking medications during pregnancy and fertility treatment. Calculates due dates from various starting points including last menstrual period and IVF (In Vitro Fertilisation) transfer dates, determines pregnancy progress on any given date, and identifies when specific pregnancy weeks are reached. Includes medication tracking capabilities for individuals undergoing fertility treatment or during pregnancy, allowing users to monitor remaining doses and quantities needed over specified time periods. Designed for those tracking their own pregnancies or supporting partners through the process, making use of options to personalise output messages. For details on due date calculations, see <https://www.acog.org/clinical/clinical-guidance/committee-opinion/articles/2017/05/methods-for-estimating-the-due-date>. |
| Authors: | Ella Kaye [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-7300-3718>) |
| Maintainer: | Ella Kaye <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.1.9000 |
| Built: | 2026-05-15 09:32:14 UTC |
| Source: | https://github.com/EllaKaye/pregnancy |
Calculates the estimated due date based on a start date and type. The function supports different start date types including last menstrual period (LMP), conception date, and embryo transfer dates. It also provides an estimated birth period, which spans from 37 weeks (birth period start) to 42 weeks (birth period end).
calculate_due_date( start_date, start_type = c("LMP", "conception", "transfer_day_3", "transfer_day_5", "transfer_day_6"), cycle = 28 )calculate_due_date( start_date, start_type = c("LMP", "conception", "transfer_day_3", "transfer_day_5", "transfer_day_6"), cycle = 28 )
start_date |
Date or character string representing a date, e.g. "YYYY-MM-DD".
The starting reference date. The interpretation of this date depends on the
|
start_type |
character. One of:
|
cycle |
numeric. Length of menstrual cycle in days. Only used when
|
The due date is calculated as follows:
For LMP: Ovulation is estimated as start_date + cycle - 14 days, then
266 days are added
For conception: 266 days are added to the conception date
For embryo transfers: The appropriate number of days are subtracted to get to conception date (3, 5, or 6 days), then 266 days are added
The birth period start date is 21 days before the due date (37 weeks pregnant), and the birth period end date is 14 days after the due date (42 weeks pregnant).
#' If start_date is a character string, the conversion to a Date
is handled by anytime::anydate().
Returns a Date object representing the estimated due date invisibly. Also prints informative messages showing:
The estimated due date
When the birth period begins (37 weeks)
When the birth period ends (42 weeks)
date_when() for finding dates at specific weeks of pregnancy
how_far() for calculating current progress in pregnancy
# Calculate due date from last menstrual period calculate_due_date("2025-01-31") # Calculate from conception date calculate_due_date("2025-02-14", start_type = "conception") # Calculate from day 5 embryo transfer calculate_due_date(as.Date("2025-02-19"), start_type = "transfer_day_5") # Calculate with non-standard cycle length calculate_due_date("2025-01-31", cycle = 35)# Calculate due date from last menstrual period calculate_due_date("2025-01-31") # Calculate from conception date calculate_due_date("2025-02-14", start_type = "conception") # Calculate from day 5 embryo transfer calculate_due_date(as.Date("2025-02-19"), start_type = "transfer_day_5") # Calculate with non-standard cycle length calculate_due_date("2025-01-31", cycle = 35)
Calculates the recommended date for taking a pregnancy test based on a start date and type. The function supports both urine and blood tests, with blood tests typically being viable 2 days earlier than urine tests.
calculate_test_date( start_date, start_type = c("LMP", "conception", "transfer_day_3", "transfer_day_5", "transfer_day_6"), cycle = 28, test_type = c("urine", "blood") )calculate_test_date( start_date, start_type = c("LMP", "conception", "transfer_day_3", "transfer_day_5", "transfer_day_6"), cycle = 28, test_type = c("urine", "blood") )
start_date |
Date or character string representing a date, e.g. "YYYY-MM-DD".
The starting reference date. The interpretation of this date depends on the
|
start_type |
character. One of:
|
cycle |
numeric. Length of menstrual cycle in days. Only used when
|
test_type |
character. One of:
|
The test date is calculated as follows:
First, the ovulation date is calculated (see calculate_due_date() for details)
For urine tests: 14 days are added to the ovulation date
For blood tests: 12 days are added to the ovulation date
Blood tests can typically detect pregnancy earlier than urine tests due to their greater sensitivity in detecting hCG hormone levels.
If start_date is a character string, the conversion to a Date
is handled by anytime::anydate().
Returns a Date object invisibly representing the recommended test date. Also prints informative messages showing:
The recommended date for a urine test
The recommended date for a blood test
calculate_due_date() for calculating the estimated due date
# Calculate test date from last menstrual period calculate_test_date("2025-01-31") # Calculate for blood test from conception date calculate_test_date( start_date = "5023-02-14", start_type = "conception", test_type = "blood" ) # Calculate from day 5 embryo transfer calculate_test_date( as.Date("2025-02-19"), start_type = "transfer_day_5" ) # Calculate with non-standard cycle length calculate_test_date("2025-01-31", cycle = 35)# Calculate test date from last menstrual period calculate_test_date("2025-01-31") # Calculate for blood test from conception date calculate_test_date( start_date = "5023-02-14", start_type = "conception", test_type = "blood" ) # Calculate from day 5 embryo transfer calculate_test_date( as.Date("2025-02-19"), start_type = "transfer_day_5" ) # Calculate with non-standard cycle length calculate_test_date("2025-01-31", cycle = 35)
Calculate and display date of specific pregnancy week
date_when(weeks, due_date = NULL, person = NULL, today = Sys.Date())date_when(weeks, due_date = NULL, person = NULL, today = Sys.Date())
weeks |
Numeric value indicating the number of weeks of pregnancy to calculate the date for. |
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:
|
today |
Date or character string representing a date, e.g. "YYYY-MM-DD". Represents the reference date for calculations. Default is Sys.Date(). This parameter exists primarily for testing and documentation purposes and it is unlikely to make sense for the user to need or want to change it from the default. |
The function calculates when someone will be/was a specific number of weeks pregnant based on their due date. It handles past, present and future dates appropriately in its messaging. The due date can be provided directly or set globally using options("pregnancy.due_date"). Similarly, the person being referenced can be provided directly or set globally using options("pregnancy.person").
If date_when or today is a character string, the conversion to a Date
is handled by anytime::anydate().
Invisibly returns a Date object of when the specified week of pregnancy occurs/occurred/will occur.
Prints messages to the console showing:
When the specified week of pregnancy occurs/occurred/will occur
How far in the past/future that date is from today (unless that date is the current date)
calculate_due_date() for calculating the due date
set_due_date() for setting the due date as a global option
how_far() for calculating current pregnancy progress
# Set a due date date_when(20, due_date = "2025-12-01") date_when(33, due_date = as.Date("2025-12-01"), person = "Sarah")# Set a due date date_when(20, due_date = "2025-12-01") date_when(33, due_date = as.Date("2025-12-01"), person = "Sarah")
pregnancy.due_date optionFunctions to get and set the default due date used throughout the package.
This affects calculations in various functions that determine pregnancy progress
and timing. Settings persist for the current R session only, unless added to
.Rprofile. set_due_date() sets the "pregnancy.due_date" option and get_due_date() retrieves it.
set_due_date(due_date) get_due_date()set_due_date(due_date) get_due_date()
due_date |
A Date or character string representing a date, e.g. "YYYY-MM-DD", specifying the estimated due date, or NULL to unset the option. |
Both functions invisibly return the current due date setting:
get_due_date() returns the current setting (a Date object) or NULL if not set
set_due_date() returns the due date value that was set
calculate_due_date() to calculate a due date based on other dates
how_far() and other functions that use the due date for calculations
# Store original setting (without messages) original_due_date <- getOption("pregnancy.due_date") # Check current setting get_due_date() # Set due date and check again set_due_date("2025-09-15") get_due_date() # Restore original setting (without messages) options(pregnancy.due_date = original_due_date)# Store original setting (without messages) original_due_date <- getOption("pregnancy.due_date") # Check current setting get_due_date() # Set due date and check again set_due_date("2025-09-15") get_due_date() # Restore original setting (without messages) options(pregnancy.due_date = original_due_date)
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)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
# 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# 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
A data frame with example medications that might be used during fertility treatment/first trimester.
It is an example of a data frame that might be used as the meds argument to medications_remaining().
medications medications_simplemedications medications_simple
medications is a data frame with 14 rows and 5 columns.
medications_simple is a data frame with 4 rows and 5 columns.
Name of the medication
Format of medication
Number taken per day
Date to start taking the medication
Final date on which the medication is taken. See details.
An object of class tbl_df (inherits from tbl, data.frame) with 4 rows and 5 columns.
Note that the same medication (prednisolone in this example) has several rows, first because the quantity taken per day changes, then because it needs to be taken on non-consecutive days.
medications medications_simplemedications medications_simple
Calculates and displays how many medications remain to be taken as of a specific date, based on a schedule of medications with start and stop dates. Results can be grouped either by medication name or by format (e.g., tablet, injection).
medications_remaining( meds = NULL, group = c("medication", "format"), on_date = Sys.Date(), until_date = NULL )medications_remaining( meds = NULL, group = c("medication", "format"), on_date = Sys.Date(), until_date = NULL )
meds |
Data frame containing medication schedule. Must have the following columns:
If NULL, will try to use the "pregnancy.medications" option. Required if option not set. |
group |
Character string specifying how to group the results. One of:
|
on_date |
Date or character string representing a date, e.g. "YYYY-MM-DD", specifying the date from which to calculate remaining medications. Defaults to current system date |
until_date |
Date or character string representing a date, e.g. "YYYY-MM-DD",
specifying cut-off date for remaining medications.
If NULL, defaults to the latest |
If on_date, until_date start_date or stop_date is a character vector, the conversion to a Date
is handled by anytime::anydate().
If any start_date is NA in any row, that row will not be counted in the remaining quantities.
If any stop_date is NA, it throws an error.
Returns a data frame containing remaining quantities, grouped as specified.
Assumes that the function is being called first thing in the day,
i.e. before any of on_date's medications have been taken.
The data frame has two columns:
Either 'medication' or 'format' depending on grouping
quantity: Total number of units remaining
Only medications with remaining quantities > 0 are included.
If no medications remain, a message is printed to the console indicating this, and a data frame with 0 rows is returned invisibly.
pregnancy.medications: Data frame setting default medication schedule
set_medications() for setting default medication schedule
get_medications() for retrieving current medication schedule
medications for an example medications data frame
# Define medications table #' # Create example medication schedule meds <- data.frame( medication = c("progynova", "prednisolone", "clexane"), format = c("tablet", "tablet", "injection"), quantity = c(3, 2, 1), start_date = c("2025-04-21", "2025-04-26", "2025-05-08"), stop_date = c("2025-04-30", "2025-05-07", "2025-09-05") ) # Calculate remaining medications medications_remaining(meds, on_date = "2025-04-21") medications_remaining(meds, group = "format", on_date = "2025-04-21") # Calculate medications for a specified period medications_remaining( meds = meds, on_date = "2025-04-23", until_date = "2025-04-30" ) # Set and use global medications option #' Store original medications setting (without message) original_medications <- getOption("pregnancy.medications") set_medications(pregnancy::medications) medications_remaining(on_date = as.Date("2025-05-01")) # Restore original medications setting (without message) options(pregnancy.medications = original_medications)# Define medications table #' # Create example medication schedule meds <- data.frame( medication = c("progynova", "prednisolone", "clexane"), format = c("tablet", "tablet", "injection"), quantity = c(3, 2, 1), start_date = c("2025-04-21", "2025-04-26", "2025-05-08"), stop_date = c("2025-04-30", "2025-05-07", "2025-09-05") ) # Calculate remaining medications medications_remaining(meds, on_date = "2025-04-21") medications_remaining(meds, group = "format", on_date = "2025-04-21") # Calculate medications for a specified period medications_remaining( meds = meds, on_date = "2025-04-23", until_date = "2025-04-30" ) # Set and use global medications option #' Store original medications setting (without message) original_medications <- getOption("pregnancy.medications") set_medications(pregnancy::medications) medications_remaining(on_date = as.Date("2025-05-01")) # Restore original medications setting (without message) options(pregnancy.medications = original_medications)
pregnancy.medications optionFunctions to get and set the default medications data frame used in the medications_remaining() function.
Settings persist for the current R session only, unless added to
.Rprofile. set_medications() sets the "pregnancy.medications" option and get_medications() retrieves it.
set_medications(meds) get_medications()set_medications(meds) get_medications()
meds |
Data frame containing medication schedule. Must have the following columns:
If NULL, will try to use the "pregnancy.medications" option. Required if option not set. |
Both functions invisibly return the current medications setting:
get_medications() returns the current setting (a data frame) or NULL if not set
set_medications() returns the medications data frame that was set
# Store original setting (without messages) original_medications <- getOption("pregnancy.medications") # Set the option set_medications(pregnancy::medications) # Get the option get_medications() # Restore original setting (without messages) options(pregnancy.medications = original_medications)# Store original setting (without messages) original_medications <- getOption("pregnancy.medications") # Set the option set_medications(pregnancy::medications) # Get the option get_medications() # Restore original setting (without messages) options(pregnancy.medications = original_medications)
pregnancy.person option for pregnancy-related messagesFunctions to get and set the default person used in messages throughout the package.
This affects the grammar and pronouns used in various function outputs. Settings
persist for the current R session only, unless added to .Rprofile. set_person() sets the "pregnancy.person" option and get_person() retrieves it.
set_person(person) get_person()set_person(person) get_person()
person |
The person who is pregnant, to determine the grammar for the output message. Can be:
|
Both functions invisibly return the current person setting:
get_person() returns the current setting (a character string) or NULL if not set
set_person() returns the person value that was set
how_far() and other functions that use the person setting for message formatting
# Store original setting (without messages) original_person <- getOption("pregnancy.person") # Check current setting get_person() # Set to first person (using string) set_person("I") get_person() # Set to second person (using number) set_person(2) get_person() # Set to a specific name set_person("Sarah") get_person() # Restore original setting (without messages) options(pregnancy.person = original_person)# Store original setting (without messages) original_person <- getOption("pregnancy.person") # Check current setting get_person() # Set to first person (using string) set_person("I") get_person() # Set to second person (using number) set_person(2) get_person() # Set to a specific name set_person("Sarah") get_person() # Restore original setting (without messages) options(pregnancy.person = original_person)