我有一个数据框,其中包含不同事件的开始和结束时间以及标题:
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
我需要将此事件列表转换为单个 .ics 文件,其中包含可以导入到我的日历中的各种事件。
如何使用日历包的 ic_write 函数来做到这一点?帮助说 ic_write 需要一个 ical 类的对象,但我没有找到如何从数据帧创建 ical 对象。有谁知道怎么做吗?
谢谢您的帮助!
这种方法对我有用:
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")
它写入
.ics
文件,可以导入日历:
但是,当尝试使用 {calendar} 读回它时,会出错:
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
与 github issue 内联。但可以用 {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
创建于 2024-03-17,使用 reprex v2.1.0