Why are we doing this? What are we doing?


Lets imagine we are organising a workshop and we have a bunch of dates, periods, places and teachers where the workshop will occur.
Lets suppose we want to put it available to our students in a google calendar, so that if they use google calendar they can import the events they are interested to their own personal calendar, or simply see which events they are available to attend.  We could either make a calendar by adding manually each event in google calendar and make it publicly available or we can try to make it in r, export into a ics file and import it in the google calendar all at once.
This is what we will try to make in this small report.


# Let’s try then

We will use the package calendar that writes and reads ics files.

# install.packages("calendar")
library(dplyr)
library(calendar)

Webpage of package calendar
Lovelace page on calendars

Now, the ics files have a weird structure, like (this is just one event):

calendar::ical_example
##  [1] "BEGIN:VCALENDAR"                                  
##  [2] "PRODID:-//Google Inc//Google Calendar 70.9054//EN"
##  [3] "VERSION:2.0"                                      
##  [4] "CALSCALE:GREGORIAN"                               
##  [5] "METHOD:PUBLISH"                                   
##  [6] "X-WR-CALNAME:atf-test"                            
##  [7] "X-WR-TIMEZONE:Europe/London"                      
##  [8] "BEGIN:VEVENT"                                     
##  [9] "DTSTART:20180809T160000Z"                         
## [10] "DTEND:20180809T163000Z"                           
## [11] "DTSTAMP:20180810T094100Z"                         
## [12] "UID:1119ejg4vug5758527atjcrqj3@google.com"        
## [13] "CREATED:20180807T133712Z"                         
## [14] "DESCRIPTION:\\n"                                  
## [15] "LAST-MODIFIED:20180807T133712Z"                   
## [16] "LOCATION:"                                        
## [17] "SEQUENCE:0"                                       
## [18] "STATUS:CONFIRMED"                                 
## [19] "SUMMARY:ical programming mission"                 
## [20] "TRANSP:OPAQUE"                                    
## [21] "END:VEVENT"                                       
## [22] "END:VCALENDAR"

To pass this into something nice in r, like a tible (or a dataframe):

(ic <- ical(calendar::ical_example))
## # A tibble: 1 x 12
##   DTSTART             DTEND               DTSTAMP UID   CREATED DESCRIPTION
##   <dttm>              <dttm>              <chr>   <chr> <chr>   <chr>      
## 1 2018-08-09 16:00:00 2018-08-09 16:30:00 201808… 1119… 201808… "\\n"      
## # … with 6 more variables: `LAST-MODIFIED` <chr>, LOCATION <chr>,
## #   SEQUENCE <chr>, STATUS <chr>, SUMMARY <chr>, TRANSP <chr>
ic %>% data.frame()
##               DTSTART               DTEND          DTSTAMP
## 1 2018-08-09 16:00:00 2018-08-09 16:30:00 20180810T094100Z
##                                     UID          CREATED DESCRIPTION
## 1 1119ejg4vug5758527atjcrqj3@google.com 20180807T133712Z         \\n
##      LAST.MODIFIED LOCATION SEQUENCE    STATUS                  SUMMARY TRANSP
## 1 20180807T133712Z                 0 CONFIRMED ical programming mission OPAQUE
# to export the file:
# ic_write(ic, file.path(getwd(), "ic_prova2.ics"))

# to read the file afterwards:
# ics_df <- ic_read("/prova_desenho.ics")

# Now an example with multiple lines, that was exported directly from google calendar:
# to read ics files, exported from google calendar directly:
# ics_df <- ic_read("C:/Users/lioca/Downloads/prova_desenho.ics")
# head(ics_df %>% data.frame())

Note that in each line we have one event.
So let’s try to do something similar now, using our data.

# Lovelace's example (full 5 days trip)
s = as.POSIXct("2020-01-20")
e = s + 60^2 * 24 *5
event = ic_event(start = s, end = e , summary = "Research trip")
event
## # A tibble: 1 x 4
##   UID                         DTSTART             DTEND               SUMMARY   
##   <chr>                       <dttm>              <dttm>              <chr>     
## 1 ical-cca02936-a374-46ad-b4… 2020-01-20 00:00:00 2020-01-25 00:00:00 Research …
# now, go to google and import it... and tchan-ran!


# Include hours (not complete day events as the previous) and description/location:
s = as.POSIXct("2020-1-21 10:00")
e = s + 60^2*7
event = ic_event(start = s, 
                 end = e , 
                 # this is the title of the event
                 summary = "Mar em 4 estações com o João Catarino",
                 
                 # this is to add extra fields
                 more_properties = TRUE,
                 event_properties = list(
                   # description of the event, in this case I add a webpage with formular and details on the inscription
                   "DESCRIPTION" = "https://forms.gle/cuhMPLFRzfe585QS6",
                   # location of the event
                   "LOCATION:"="Antigo Posto de Vigilância e Defesa da Entrada do Porto de Lisboa (PVDEPL), R. Piedade Franco Rodrigues 2, 2780-383 Oeiras, Portugal"))
# ic_write(event, file.path(getwd(), "ic_prova2.ics"))
# import in google calendar and go.
event %>% data.frame()
##                                         UID             DTSTART
## 1 ical-b86406cf-1443-4d70-9a7b-fba1d19e8972 2020-01-21 10:00:00
##                 DTEND                               SUMMARY
## 1 2020-01-21 17:00:00 Mar em 4 estações com o João Catarino
##                           DESCRIPTION
## 1 https://forms.gle/cuhMPLFRzfe585QS6
##                                                                                                                               LOCATION
## 1 Antigo Posto de Vigilância e Defesa da Entrada do Porto de Lisboa (PVDEPL), R. Piedade Franco Rodrigues 2, 2780-383 Oeiras, Portugal
####
## Multiple events?
####

# Include hours (not complete day events as the previous) and description/location:
s = as.POSIXct("2020-2-29 10:00")
e = s + 60^2*7
event1 = ic_event(start = s, 
                 end = e , 
                 # this is the title of the event
                 summary = "Mar em 4 estações com o João Catarino")

event2 = ic_event(start = s, 
                 end = e+60^2 , 
                 # this is the title of the event
                 summary = "Mar em 4 estações 2")

event <- rbind(event1, event2)
# ic_write(event, file.path(getwd(), "ic_prova2.ics"))
# import in google calendar and go.
event %>% data.frame()
##                                         UID             DTSTART
## 1 ical-f6886687-3389-4415-88ff-ff226d247b62 2020-02-29 10:00:00
## 2 ical-f327f015-bbc7-47d9-8b56-0413d931e0df 2020-02-29 10:00:00
##                 DTEND                               SUMMARY
## 1 2020-02-29 17:00:00 Mar em 4 estações com o João Catarino
## 2 2020-02-29 18:00:00                   Mar em 4 estações 2
#############################################################
# https://github.com/ATFutures/calendar/issues/36

# Multiple events_ from a data.frame
s = as.POSIXct(c("2020-2-29 10:00", 
                 "2020-3-29 10:00",
                 "2020-4-29 10:00"))

event1 = data.frame(DTSTART = s, 
                    DTEND = s + 60^2*7, 
                    SUMMARY = "PROVA",
                    LOCATION = c("Lisbon","Evora","Oporto"),
                    stringsAsFactors = FALSE)
event1 <- event1 %>%
  mutate(UID = replicate(nrow(event1), ic_guid()))
event <- ical(event1)
event
## # A tibble: 3 x 5
##   DTSTART             DTEND               SUMMARY LOCATION UID                  
##   <dttm>              <dttm>              <chr>   <chr>    <chr>                
## 1 2020-02-29 10:00:00 2020-02-29 17:00:00 PROVA   Lisbon   ical-2d882764-5aad-4…
## 2 2020-03-29 10:00:00 2020-03-29 17:00:00 PROVA   Evora    ical-6e6a2cf4-1ae0-4…
## 3 2020-04-29 10:00:00 2020-04-29 17:00:00 PROVA   Oporto   ical-4e7ae065-e1a3-4…
# ic_write(event1, file.path(getwd(), "ic_prova2.ics"))

To import to google calendar, we go to definitions (right upper corner of the calendar), then import/export, select the file and choose to which calendar you are importing to.

TO-DO


(I think we cannot do these things, although it would be nice. I tried export events with such features, but the ics code does not change.)
- How to change event colour?
- How to add a nice icon?

Good luck and have fun.