Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R package to export ICS?

Tags:

r

icalendar

Does anyone know of an R function to export dates and labels to an ics calendar format?

I've googled and searched SO but nothing is obvious, but can't believe someone hasn't already done this...

like image 248
bjw Avatar asked Oct 16 '25 20:10

bjw


1 Answers

The iCalendar specification is pretty straightforward. Extending the following after reading that link and keeping it handy as a reference should be trivial (and I use that word deliberately vs lightly):

#' Create a minimal iCalendar VEVENT
#' 
#' @param start,end start and end times of the event. This will be converted to
#'        GMT from whatever time zone it currently is.
#' @param summary a summary of the event. This is the "title" you see in calendars.
#' @param domain something that will help the generated UUID be even more unique and
#'        is generally good practice to use your org's domain name
#' @return atomic character vector ready for `writeLines()`
create_ical <- function(start, end, summary, domain="example.com") {
  
  require(uuid, quietly = TRUE, warn.conflicts = FALSE)
  
  sprintf(
    "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//rstats//NONSGML v1.0//EN
BEGIN:VEVENT
UID:%s@%s
DTSTAMP:%s
DTSTART:%s
DTEND:%s
SUMMARY:%s
END:VEVENT
END:VCALENDAR
", uuid::UUIDgenerate(),
    domain, 
    format(Sys.time(), "%Y%m%dT%H%M%SZ", tz="GMT"), 
    format(start, "%Y%m%dT%H%M%SZ", tz="GMT"), 
    format(end, "%Y%m%dT%H%M%SZ", tz="GMT"), 
    summary
)
  
}

Usage:

create_ical(
  as.POSIXct("2018-01-30 13:00:00", origin="1970-01-01 00:00:00"),
  as.POSIXct("2018-01-30 14:00:00", origin="1970-01-01 00:00:00"),
  "A good description of the event",
  "somedom.org"
) -> ics_event

cat(ics_event)
## BEGIN:VCALENDAR
## VERSION:2.0
## PRODID:-//rstats//NONSGML v1.0//EN
## BEGIN:VEVENT
## UID:[email protected]
## DTSTAMP:20180116T123051Z
## DTSTART:20180130T180000Z
## DTEND:20180130T190000Z
## SUMMARY:A good description of the event
## END:VEVENT
## END:VCALENDAR

writeLines(ics_event, "ics_event.ics")
like image 134
hrbrmstr Avatar answered Oct 18 '25 11:10

hrbrmstr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!