medications_remaining | R Documentation |
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
)
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.