0

I want to create calendars from tabular and computed data in R. This works well using package toastui, especially as it can be embedded in web pages generated with RMarkdown or Quarto. Now, I would also like to export it in ics/ical format. Is there a common way or another package in R to get something like this?

library("toastui")
library("tibble")
library("lubridate")

## table of events
events <- tribble(
  ~title, ~start, ~end, ~category,
  "Guest talk",   "2023-03-07 15:00", "2023-03-07 17:00", "time",
  "Field Course", "2023-03-01 00:00", "2023-03-03 00:00", "allday",
  "Holidays",     "2023-03-27 00:00", "2023-03-31 00:00", "allday") |>
mutate(start = as.POSIXct(start), end=as.POSIXct(end))

## computational dates
regular <- tibble(
  title = "Weekly meeting",
  start = seq(as.POSIXct("2023-03-10 10:00 "), 
              as.POSIXct("2023-04-01 10:00 "), by= "1 week"),
  end   = seq(as.POSIXct("2023-03-10 12:00 "), 
              as.POSIXct("2023-04-01 12:00 "), by= "1 week "),
  category = "time"
)
  

## create toastui calendar
bind_rows(events, regular) |> 
calendar(useDetailPopup = TRUE, navigation = TRUE, defaultDate = "2023-03-16") 

example calendar

tpetzoldt
  • 5,338
  • 2
  • 12
  • 29
  • 1
    The closest I've found is [`ical`](https://cran.r-project.org/web/packages/ical/index.html) ([git repo](https://github.com/petermeissner/ical)) but that's for parsing files, not exporting to them. The last commit was almost 3y ago, so it's unlikely under much work, but perhaps the low-activity is due to "working smoothly". You might reach out to the authors to consider the other direction. – r2evans Mar 16 '23 at 21:49
  • 1
    Good point ... I have found this already ;-) but I prefer to create the raw data in tabular and not in ical format. In the past, I used colored schedules with `knitr::kable`, and a "real" calendar with **toastui** is even better, except that both have no exchange format. – tpetzoldt Mar 16 '23 at 22:00

1 Answers1

0

After some reading in the ical docs, I eventually used parts of a previous SO post that showed how to export a single event. With this background, essential information of the calendar data frame can be exported with:

#library("uuid")

format_event <- function(start, end, summary, tz = "GMT") {
  template <-"BEGIN:VEVENT\nUID:%s\nDTSTAMP:%s\nDTSTART:%s\nDTEND:%s\nSUMMARY:%s\nEND:VEVENT"
  sprintf(template, uuid::UUIDgenerate(),
    format(Sys.time(), "%Y%m%dT%H%M%SZ", tz = tz),
    format(start, "%Y%m%dT%H%M%SZ", tz = tz),
    format(end, "%Y%m%dT%H%M%SZ", tz = tz),
    summary)
}

export_calendar <- function(df, file, tz = "GMT") {
  header <- "BEGIN:VCALENDAR\nPRODID:-//MyMeetings/ical //EN\nVERSION:2.0\nCALSCALE:GREGORIAN"
  footer <- "END:VCALENDAR"
  f <- file(file)
  open(f, "w")
  writeLines(header, con = f)
  invisible(lapply(1:nrow(df), \(i) {
    ic_char <- format_event(start = df$start[i], end = df$end[i],
                               summary = df$title[i], tz = tz)
    writeLines(ic_char, con = f)
  }))
  writeLines(footer, con = f)
  close(f)
}

### MAIN ###
export_calendar(df, file = "mycal.ics", tz = "CET")

This works for now and the iCalendar Validator showed no errors, but generalized and improved versions are still welcome.

tpetzoldt
  • 5,338
  • 2
  • 12
  • 29