Title: | Create, Read, Write, and Work with 'iCalendar' Files, Calendars and Scheduling Data |
---|---|
Description: | Provides function to create, read, write, and work with 'iCalendar' files (which typically have '.ics' or '.ical' extensions), and the scheduling data, calendars and timelines of people, organisations and other entities that they represent. 'iCalendar' is an open standard for exchanging calendar and scheduling information between users and computers, described at <https://icalendar.org/>. |
Authors: | Robin Lovelace [aut, cre] , Layik Hama [aut] , Ollie Lloyd [ctb] , Franco Scarafia [ctb] , Serkan Korkmaz [ctb] |
Maintainer: | Robin Lovelace <[email protected]> |
License: | Apache License (>= 2.0) |
Version: | 0.2.0 |
Built: | 2024-11-18 04:54:10 UTC |
Source: | https://github.com/atfutures/calendar |
iCalendar is an open standard for
"exchanging calendar and scheduling information between users and computers".
Files adhering to this standard are save as .ics
files.
The ical
package is for interacting with such files
Maintainer: Robin Lovelace [email protected] (ORCID)
Authors:
Layik Hama [email protected] (ORCID)
Other contributors:
Ollie Lloyd [email protected] (ORCID) [contributor]
Franco Scarafia [email protected] (ORCID) [contributor]
Serkan Korkmaz [email protected] (ORCID) [contributor]
Useful links:
Report bugs at https://github.com/ATFutures/calendar/issues
Convenient datetime formats
formats
formats
An object of class list
of length 8.
formats formats <- list( "ddmmyy"="%d%m%y", "ddmmyyyy"="%d%m%Y", "ddmmyyyy hh"="%d%m%Y %H", "ddmmyyyy hhmm"="%d%m%Y %H%M", "yyyy-mm-dd"="%Y-%m-%d", "yyyy-mm-dd hh:mm"="%Y-%m-%d %H:%M", "yyyy-mm-dd hh"="%Y-%m-%d %H", "yy-mm-dd"="%y-%m-%d" ) # usethis::use_data(datetime_formats)
formats formats <- list( "ddmmyy"="%d%m%y", "ddmmyyyy"="%d%m%Y", "ddmmyyyy hh"="%d%m%Y %H", "ddmmyyyy hhmm"="%d%m%Y %H%M", "yyyy-mm-dd"="%Y-%m-%d", "yyyy-mm-dd hh:mm"="%Y-%m-%d %H:%M", "yyyy-mm-dd hh"="%Y-%m-%d %H", "yy-mm-dd"="%y-%m-%d" ) # usethis::use_data(datetime_formats)
This file is documented at https://www.gov.uk/government/publications/open-standards-for-government/exchange-of-calendar-events
holidays
holidays
An object of class character
of length 1.
# dataset was stored as follows: u = "https://www.gov.uk/bank-holidays/england-and-wales.ics" # download.file(u, "inst/extdata/england-and-wales.ics") # holidays = "holidays"
# dataset was stored as follows: u = "https://www.gov.uk/bank-holidays/england-and-wales.ics" # download.file(u, "inst/extdata/england-and-wales.ics") # holidays = "holidays"
Extract attributes from ical text
ic_attributes_vec( x = NULL, ic_attributes = c(BEGIN = "VCALENDAR", PRODID = "ATFutures/calendar", VERSION = "2.0", CALSCALE = "GREGORIAN", METHOD = "PUBLISH") )
ic_attributes_vec( x = NULL, ic_attributes = c(BEGIN = "VCALENDAR", PRODID = "ATFutures/calendar", VERSION = "2.0", CALSCALE = "GREGORIAN", METHOD = "PUBLISH") )
x |
Lines read-in in from an iCal file |
ic_attributes |
Calendar attributes, e.g. as provided by |
ic_attributes_vec() # default attributes (can be changed) ic_attributes_vec(ical_example)
ic_attributes_vec() # default attributes (can be changed) ic_attributes_vec(ical_example)
Based on: https://stackoverflow.com/questions/17308551/
ic_bind_list(x)
ic_bind_list(x)
x |
list of named vectors |
ic_bind_list(list(ic_vector(ical_example))) ics_file <- system.file("extdata", "england-and-wales.ics", package = "ical") ics_raw = readLines(ics_file) x <- lapply(ic_list(ics_raw), function(x) { ic_vector(x) }) ic_df <- ic_bind_list(x) head(ic_df) x <- lapply(ical_outlook, function(x) { ic_vector(x) }) ic_bind_list(x)
ic_bind_list(list(ic_vector(ical_example))) ics_file <- system.file("extdata", "england-and-wales.ics", package = "ical") ics_raw = readLines(ics_file) x <- lapply(ic_list(ics_raw), function(x) { ic_vector(x) }) ic_df <- ic_bind_list(x) head(ic_df) x <- lapply(ical_outlook, function(x) { ic_vector(x) }) ic_bind_list(x)
Convert datetime object to character string
ic_char_datetime(x, zulu = FALSE)
ic_char_datetime(x, zulu = FALSE)
x |
datetime object |
zulu |
is the datetime in Zulu time?
|
x <- ic_datetime("20180809T160000") ic_char_datetime(x) == "20180809T160000" x <- ic_datetime("20180809T160000Z") ic_char_datetime(x, zulu = TRUE) == "20180809T160000Z" ic_char_date(as.Date("1985-12-26"))
x <- ic_datetime("20180809T160000") ic_char_datetime(x) == "20180809T160000" x <- ic_datetime("20180809T160000Z") ic_char_datetime(x, zulu = TRUE) == "20180809T160000Z" ic_char_date(as.Date("1985-12-26"))
Convert ical object to character strings of events
ic_char_event(ic, zulu = FALSE)
ic_char_event(ic, zulu = FALSE)
ic |
object of class |
zulu |
is the datetime in Zulu time?
|
ic <- ical(ical_example) ic_char_event(ic) ic_char_event(ic[c(1, 1), ])
ic <- ical(ical_example) ic_char_event(ic) ic_char_event(ic[c(1, 1), ])
Convert ical object to character strings of attributes
ic_character(ic, zulu = FALSE)
ic_character(ic, zulu = FALSE)
ic |
object of class |
zulu |
is the datetime in Zulu time?
|
ic <- ical(ical_example) ic_character(ic) identical(ical_example, ic_character(ic))
ic <- ical(ical_example) ic_character(ic) identical(ical_example, ic_character(ic))
Returns a data frame
ic_dataframe(x)
ic_dataframe(x)
x |
Lines read-in in from an iCal file |
ic_dataframe(ical_example) ic_dataframe(ical_outlook) ics_file <- system.file("extdata", "england-and-wales.ics", package = "calendar") x = readLines(ics_file) x_df = ic_dataframe(x) head(x_df) x = data.frame(x_df) x_df2 = ic_dataframe(x) identical(x, x_df2)
ic_dataframe(ical_example) ic_dataframe(ical_outlook) ics_file <- system.file("extdata", "england-and-wales.ics", package = "calendar") x = readLines(ics_file) x_df = ic_dataframe(x) head(x_df) x = data.frame(x_df) x_df2 = ic_dataframe(x) identical(x, x_df2)
Convert ical date into R date
ic_date(x)
ic_date(x)
x |
Lines read-in in from an iCal file |
ic_date("20120103")
ic_date("20120103")
Convert ical datetime into R datetime Z at the end of an ical stamp stands of Zulu time https://en.wikipedia.org/wiki/Coordinated_Universal_Time#Time_zones which is UTC = GMT https://greenwichmeantime.com/info/zulu/
ic_datetime(x)
ic_datetime(x)
x |
Lines read-in in from an iCal file |
ic_datetime("20180809T160000Z") ic_date("20120103")
ic_datetime("20180809T160000Z") ic_date("20120103")
Create an ical event using either POSIXct type or character type with format parameters.
ic_event( uid = ic_guid(), start_time = as.POSIXct(round.POSIXt(Sys.time(), units = "hours")), end_time = 1, format = "%Y-%m-%d %H:%M", summary = "ical event", more_properties = FALSE, event_properties = calendar::properties )
ic_event( uid = ic_guid(), start_time = as.POSIXct(round.POSIXt(Sys.time(), units = "hours")), end_time = 1, format = "%Y-%m-%d %H:%M", summary = "ical event", more_properties = FALSE, event_properties = calendar::properties )
uid |
the unique id of the event, by default generated by |
start_time |
start time, by default the start time plus one hour |
end_time |
a number representing the number of hours
after |
format |
required if start_time and end_time are vectors and are not of datetime format
"%Y-%m-%d %H:%M", you can use |
summary |
short outline of the event |
more_properties |
add placeholder columns for properties in addition to |
event_properties |
named vector of additional properties to include. By default
These include names stored in the data object |
in case of start_time and end_time being character values, a format must be provided.
object of class ics
ic_event() s <- lubridate::ymd_h("2019-01-01 00") ic_event(start_time = s, end_time = 3) # also accepts this format by default, thanks to lubridate::ymd_h: ic_event(start_time = "2019-01-01 00") ic_event(more_properties = TRUE) ic_event(start_time = "18-10-12", end_time = "18-10-13", format = calendar::formats$`yy-mm-dd`)
ic_event() s <- lubridate::ymd_h("2019-01-01 00") ic_event(start_time = s, end_time = 3) # also accepts this format by default, thanks to lubridate::ymd_h: ic_event(start_time = "2019-01-01 00") ic_event(more_properties = TRUE) ic_event(start_time = "18-10-12", end_time = "18-10-13", format = calendar::formats$`yy-mm-dd`)
Return formatted data from iCal fields
ic_extract(x, pattern)
ic_extract(x, pattern)
x |
Lines read-in in from an iCal file |
pattern |
A text string to search from (an ical field) |
ic_extract(ical_example, "DTSTART")
ic_extract(ical_example, "DTSTART")
Return info from iCal files, return raw unprocessed text.
ic_extract_raw(x, pattern)
ic_extract_raw(x, pattern)
x |
Lines read-in in from an iCal file |
pattern |
A text string to search from (an ical field) |
pattern = "TSTAMP" ic_extract_raw(ical_example, pattern)
pattern = "TSTAMP" ic_extract_raw(ical_example, pattern)
Find contents of iCal fields
ic_find(x, pattern)
ic_find(x, pattern)
x |
Lines read-in in from an iCal file |
pattern |
A text string to search from (an ical field) |
pattern = "DTSTAMP" ic_find(ical_example, pattern)
pattern = "DTSTAMP" ic_find(ical_example, pattern)
Provided without any testing. Slight improvement from SO question
ic_guid()
ic_guid()
ic_guid()
ic_guid()
This function breaks-up the iCalendar object into a list.
By default it breaks it into events, where the number of events
is the number of BEGIN:VEVENT
event initiation lines
(assumes all events start and end with :VEVENT
),
as per the specification (see ic_spec()
).
ic_list(x, pattern = ":VEVENT", include_pattern = FALSE)
ic_list(x, pattern = ":VEVENT", include_pattern = FALSE)
x |
Lines read-in in from an iCal file |
pattern |
A text string to search from (an ical field) |
include_pattern |
should the pattern be included
in the output? |
ic_list(ical_example) ics_file <- system.file("extdata", "england-and-wales.ics", package = "ical") x = readLines(ics_file) ics_list = ic_list(x) ics_list[1:2] ic_list(x, include_pattern = TRUE)
ic_list(ical_example) ics_file <- system.file("extdata", "england-and-wales.ics", package = "ical") x = readLines(ics_file) ics_list = ic_list(x) ics_list[1:2] ic_list(x, include_pattern = TRUE)
Read ics file
ic_read(file)
ic_read(file)
file |
ics file to read |
object of class ics
f <- system.file("extdata", "england-and-wales.ics", package = "calendar") ics_df = ic_read(f) head(ics_df)
f <- system.file("extdata", "england-and-wales.ics", package = "calendar") ics_df = ic_read(f) head(ics_df)
This function shows the spec underlying iCal files.
ic_spec(mode = "view", spec_url = "https://tools.ietf.org/rfc/rfc5545.txt")
ic_spec(mode = "view", spec_url = "https://tools.ietf.org/rfc/rfc5545.txt")
mode |
character string specifying whether to look at the spec ( |
spec_url |
the location of the latest version of the spec, from tools.ietf.org/rfc/rfc5545.txt by default |
This is designed to be a helper function for creating data frames ical lists.
ic_vector(x, pattern = "[A-Z]-?[A-Z]")
ic_vector(x, pattern = "[A-Z]-?[A-Z]")
x |
Lines read-in in from an iCal file |
pattern |
A text string to search from (an ical field) |
x = ical_example[18:19] ic_vector(x)
x = ical_example[18:19] ic_vector(x)
Write ics file
ic_write(ic, file, zulu = FALSE)
ic_write(ic, file, zulu = FALSE)
ic |
object of class |
file |
ics file to write |
zulu |
is the datetime in Zulu time?
|
ic <- ical(ical_example) ic_write(ic, file.path(tempdir(), "ic.ics")) f <- system.file("extdata", "example.ics", package = "calendar") identical(readLines(file.path(tempdir(), "ic.ics")), readLines(f)) f <- system.file("extdata", "england-and-wales.ics", package = "calendar") ics_df <- ic_read(f) ic_write(ics_df, file.path(tempdir(), "ic.ics")) # test similarity between files with diff tool like meld - from shell: # meld ic.ics inst/extdata/england-and-wales.ics
ic <- ical(ical_example) ic_write(ic, file.path(tempdir(), "ic.ics")) f <- system.file("extdata", "example.ics", package = "calendar") identical(readLines(file.path(tempdir(), "ic.ics")), readLines(f)) f <- system.file("extdata", "england-and-wales.ics", package = "calendar") ics_df <- ic_read(f) ic_write(ics_df, file.path(tempdir(), "ic.ics")) # test similarity between files with diff tool like meld - from shell: # meld ic.ics inst/extdata/england-and-wales.ics
Create object of class ical
ical(x, ic_attributes = NULL)
ical(x, ic_attributes = NULL)
x |
Lines read-in in from an iCal file |
ic_attributes |
Calendar attributes, e.g. as provided by |
# ical from .ics characters: class(ical_example) ic <- ical(ical_example) attributes(ic) class(ic) # ical from data frame: ic_df <- data.frame(ic) ic2 <- ical(ic_df) class(ic2) attributes(ic2)
# ical from .ics characters: class(ical_example) ic <- ical(ical_example) attributes(ic) class(ic) # ical from data frame: ic_df <- data.frame(ic) ic2 <- ical(ic_df) class(ic2) attributes(ic2)
See https://calendar.google.com/calendar/ical/9sl1qu3qh2vdnq26bjgvtnos94%40group.calendar.google.com/private-85cbe5d781da1b7efc91e01032cfc264/basic.ics for the file
ical_example
ical_example
An object of class character
of length 22.
# download.file("long_url", "inst/extdata/example.ics") ical_example = readLines(system.file("extdata", "example.ics", package = "ical")) # usethis::use_data(ical_example)
# download.file("long_url", "inst/extdata/example.ics") ical_example = readLines(system.file("extdata", "example.ics", package = "ical")) # usethis::use_data(ical_example)
See here.
ical_outlook
ical_outlook
An object of class list
of length 2.
# ical_outlook_raw <- readLines(long_url) # ical_outlook_list <- ic_list(ical_outlook_raw) # ical_outlook <- ical_outlook_list[1:2] # ical_outlook[[2]][c(1:38)] <- gsub("a|e|i|o|f|l|t|n|b", "a", ical_outlook[[2]][c(1:38)]) # ical_outlook[[2]] <- ical_outlook[[2]][c(1, 5, 35, 36:55)] # usethis::use_data(ical_outlook)
# ical_outlook_raw <- readLines(long_url) # ical_outlook_list <- ic_list(ical_outlook_raw) # ical_outlook <- ical_outlook_list[1:2] # ical_outlook[[2]][c(1:38)] <- gsub("a|e|i|o|f|l|t|n|b", "a", ical_outlook[[2]][c(1:38)]) # ical_outlook[[2]] <- ical_outlook[[2]][c(1, 5, 35, 36:55)] # usethis::use_data(ical_outlook)
The key 'properties' that are allowed in ical files
properties
properties
An object of class character
of length 54.
# doc = ic_spec(mode = "read") # key_locations = grepl(pattern = " \| [A-Z]", x = doc) # summary(key_locations) # doc_key = doc[key_locations] # # regexplain::regex_gadget(text = doc_key) # explore # key_properties = grepl(pattern = "3.7|3.8", x = doc) # doc_properties = doc[key_locations & key_properties] # # regexplain::regex_gadget(text = doc_properties) # explore # properties = regmatches(doc_properties, regexpr('[A-Z][A-Z]+', doc_properties)) # usethis::use_data(properties)
# doc = ic_spec(mode = "read") # key_locations = grepl(pattern = " \| [A-Z]", x = doc) # summary(key_locations) # doc_key = doc[key_locations] # # regexplain::regex_gadget(text = doc_key) # explore # key_properties = grepl(pattern = "3.7|3.8", x = doc) # doc_properties = doc[key_locations & key_properties] # # regexplain::regex_gadget(text = doc_properties) # explore # properties = regmatches(doc_properties, regexpr('[A-Z][A-Z]+', doc_properties)) # usethis::use_data(properties)
The key 'properties' that are allowed in ical files
properties_core
properties_core
An object of class list
of length 4.
properties_core = list( "UID"="UID", "DTSTART"="DTSTART", "DTEND"="DTEND", "SUMMARY"="SUMMARY" )
properties_core = list( "UID"="UID", "DTSTART"="DTSTART", "DTEND"="DTEND", "SUMMARY"="SUMMARY" )
ical default VCALENDAR properties in one line vectors.
properties_ical
properties_ical
An object of class character
of length 5.
# properties_ical <- ical_example[1:5] # properties_ical # properties_ical[2] <- "PRODID:-//ATFutures/ical //EN" # usethis::use_data(properties_ical)
# properties_ical <- ical_example[1:5] # properties_ical # properties_ical[2] <- "PRODID:-//ATFutures/ical //EN" # usethis::use_data(properties_ical)