Search code examples
rdplyricalendartidyverse

How to import ical .ics file in R


I would like to import a .ics file into R, however, when I try to do so like...

sneak_cal <- read.delim("iCal-TribeEvents.ics", sep = ":", header=FALSE, stringsAsFactors = FALSE, strip.white = TRUE, na.strings = "")

...I end up splitting the character strings of website (belonging to the X-ORIGINAL-URL or the UID field) too, which is undesirable

ie https and //www.kicksonfire.com

The ultimate goal is to get the data into a tidy format where each row represents a single VEVENT, which I think would be represented by a unique UID, without any loss of information (such as the URL)

Is there another approach that is recommended, such as pre-defining the fields that are expected as the key and matching the value or empty space to that key? Since the .ics file has the same expected fields each time, it seems like it might make sense to use those fields as a template to read in the data, but I can not figure out how to do it.


Solution

  • Here's an example

    x <- readLines("https://www.kicksonfire.com/releases/?ical=1&tribe_display=list", warn = FALSE)
    stopifnot(!any(grepl("^\\s+", x))) # disregarding value fields that have linefeeds for the sake of simplicity 
    keyval <- do.call(rbind, regmatches(x, regexpr(":", x, fixed = TRUE), invert = TRUE))
    keyval <- keyval[which.max(keyval[,1]=="BEGIN" & keyval[,2]=="VEVENT"):tail(which(keyval[,1]=="END" & keyval[,2]=="VEVENT"), 1),]
    keyval <- cbind.data.frame(keyval, id=cumsum(keyval[,1]=="BEGIN" & keyval[,2]=="VEVENT"))
    df <- reshape(keyval, timevar="1", idvar="id", direction = "wide")
    head(df[,c(3,4,9)])
    #    2.DTSTART;VALUE=DATE 2.DTEND;VALUE=DATE                              2.SUMMARY
    # 1              20170422           20170423         Air Jordan 11 Low GS Blue Moon
    # 14             20170422           20170423     Air Jordan 5 Premium Pure Platinum
    # 27             20170427           20170428              Nike Air VaporMax Asphalt
    # 40             20170427           20170428                 Nike Air VaporMax Oreo
    # 53             20170427           20170428  Nike WMNS Air VaporMax White Ice Blue
    # 66             20170427           20170428 wings+horns x adidas NMD R2 Light Grey