new function to convert ds to ical format

This commit is contained in:
Andreas Gammelgaard Damsbo 2024-02-05 14:46:02 +01:00
parent bd29de23c9
commit 28dc2ba09e
2 changed files with 106 additions and 0 deletions

59
R/ds2ical.R Normal file
View File

@ -0,0 +1,59 @@
utils::globalVariables(c("DTSTART"))
#' Convert data set to ical file
#'
#' @param data data set
#' @param start event start column
#' @param location event location column
#' @param event.length use lubridate functions to generate "Period" class
#' element (default is lubridate::hours(2))
#' @param summary.glue.string character string to pass to glue::glue() for event
#' name (summary). Can take any column from data set.
#' @param description.glue.string character string to pass to glue::glue() for
#' event description. Can take any column from data set.
#'
#' @return tibble of class "ical"
#' @export
#'
#' @examples
#' df <- dplyr::tibble(start = c(Sys.time(), Sys.time() + lubridate::days(2)),
#' id = c("1", 3), assessor = "A", location = "111", note = c(NA, "OBS"))
#' df |> ds2ical(start, location)
#' df |> ds2ical(start, location,
#' summary.glue.string = "ID {id} [{assessor}] {note}")
#' # Export .ics file: (not run)
#' ical <- df |> ds2ical(start, location, description.glue.string = "{note}")
#' # ical |> calendar::ic_write(file=here::here("calendar.ics"))
ds2ical <- function(data,
start,
location,
summary.glue.string = "ID {id} [{assessor}]",
description.glue.string = NULL,
event.length = lubridate::hours(2)) {
ds <- data |>
dplyr::transmute(
SUMMARY = glue::glue(summary.glue.string, .na = ""),
DTSTART = lubridate::ymd_hms({{ start }}, tz = "CET"),
DTEND = DTSTART + event.length,
LOCATION = {{ location }}
)
if (!is.null(description.glue.string)){
ds <- dplyr::tibble(ds,
dplyr::transmute(data,
DESCRIPTION = glue::glue(
description.glue.string,
.na = ""
)
)
)
}
ds |>
(\(x){
x |>
dplyr::mutate(UID = replicate(nrow(x), calendar::ic_guid()))
})() |>
dplyr::filter(!is.na(DTSTART)) |>
calendar::ical()
}

47
man/ds2ical.Rd Normal file
View File

@ -0,0 +1,47 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ds2ical.R
\name{ds2ical}
\alias{ds2ical}
\title{Convert data set to ical file}
\usage{
ds2ical(
data,
start,
location,
summary.glue.string = "ID {id} [{assessor}]",
description.glue.string = NULL,
event.length = lubridate::hours(2)
)
}
\arguments{
\item{data}{data set}
\item{start}{event start column}
\item{location}{event location column}
\item{summary.glue.string}{character string to pass to glue::glue() for event
name (summary). Can take any column from data set.}
\item{description.glue.string}{character string to pass to glue::glue() for
event description. Can take any column from data set.}
\item{event.length}{use lubridate functions to generate "Period" class
element (default is lubridate::hours(2))}
}
\value{
tibble of class "ical"
}
\description{
Convert data set to ical file
}
\examples{
df <- dplyr::tibble(start = c(Sys.time(), Sys.time() + lubridate::days(2)),
id = c("1", 3), assessor = "A", location = "111", note = c(NA, "OBS"))
df |> ds2ical(start, location)
df |> ds2ical(start, location,
summary.glue.string = "ID {id} [{assessor}] {note}")
# Export .ics file: (not run)
ical <- df |> ds2ical(start, location, description.glue.string = "{note}")
# ical |> calendar::ic_write(file=here::here("calendar.ics"))
}