Create ics file from dataframe

31 Views Asked by At

I have a dataframe with the beginning and end times, and title, of different events in columns :

2024-03-11 10:00:00 2024-03-11 11:00:00 Event1
2024-03-13 10:00:00 2024-03-13 11:00:00 Event2
2024-03-15 10:00:00 2024-03-15 11:00:00 Event3

I need to convert this list of events into a single .ics file with various events that I can import into my calendar.

How can I use the ic_write function of calendar package to do that ? The help says ic_write requires an object of class ical, but I didn't find how to create an ical object from a dataframe. Does anyone know how to do it ?

Thank you for your help !

1

There are 1 best solutions below

0
Grzegorz Sapijaszko On

This approach works for me:

df <- data.frame(
  start = c("2024-03-11 10:00:00", "2024-03-13 10:00:00", "2024-03-15 10:00:00"),
  end = c("2024-03-11 11:00:00", "2024-03-13 11:00:00", "2024-03-15 11:00:00"),
  event = c("Event1", "Event2", "Event3")
)

d <- df |>
  dplyr::rowwise() |>
  dplyr::mutate(
    c = calendar::ic_event(start_time = start, end_time = end, summary = event)
  ) |>
  subset(select = c(c))

d$c |>
  calendar::ical() |>
  calendar::ic_write(file = "ala.ics")

It writes .ics file which can be imported into calendar:

However, when trying to read it back using {calendar} it errors out:

calendar::ic_read("ala.ics")
#> Error in if (!is.na(x) & !x == "NA" & !grepl("^\\d{8}T\\d{6}Z?$", x)) {: the condition has length > 1

which is inline with github issue. But it can be parsed with {ical}:

ical::ical_parse("ala.ics")
#> $uid
#> [1] "ical-abf31d22-09e0-40f9-9e0e-eee6c0a6c9f5"
#> [2] "ical-302ac750-4654-43e8-8cbd-28c7050a5763"
#> [3] "ical-6ed08869-ffc1-47c4-b492-3f2fd471438c"
#> 
#> $summary
#> [1] "Event1" "Event2" "Event3"
#> 
#> $start
#> [1] "2024-03-11 10:00:00 CET" "2024-03-13 10:00:00 CET"
#> [3] "2024-03-15 10:00:00 CET"
#> 
#> $end
#> [1] "2024-03-11 11:00:00 CET" "2024-03-13 11:00:00 CET"
#> [3] "2024-03-15 11:00:00 CET"
#> 
#> $description
#> [1] NA NA NA
#> 
#> $`last-modified`
#> [1] "1970-01-01 01:00:00 CET" "1970-01-01 01:00:00 CET"
#> [3] "1970-01-01 01:00:00 CET"
#> 
#> $status
#> [1] NA NA NA

Created on 2024-03-17 with reprex v2.1.0