1
0
Fork 0
mirror of https://github.com/sensebox/opensensmapr synced 2025-02-22 23:23:57 +01:00
opensensmapR/R/00utils.R

39 lines
1.2 KiB
R
Raw Permalink Normal View History

2017-08-14 18:10:16 +02:00
# parses from/to params for get_measurements_ and get_boxes_
parse_dateparams = function (from, to) {
2019-02-09 22:10:25 +01:00
from = date_as_utc(from)
to = date_as_utc(to)
if (from - to > 0) stop('"from" must be earlier than "to"')
c(date_as_isostring(from), date_as_isostring(to))
}
# NOTE: cannot handle mixed vectors of POSIXlt and POSIXct
2019-02-09 22:10:25 +01:00
date_as_utc = function (date) {
time = as.POSIXct(date)
attr(time, 'tzone') = 'UTC'
time
}
# NOTE: cannot handle mixed vectors of POSIXlt and POSIXct
2017-08-14 18:10:16 +02:00
date_as_isostring = function (date) format.Date(date, format = '%FT%TZ')
2019-02-09 22:10:25 +01:00
isostring_as_date = function (x) as.POSIXct(strptime(x, format = '%FT%T', tz = 'GMT'))
#' Checks for an interactive session using interactive() and a knitr process in
#' the callstack. See https://stackoverflow.com/a/33108841
#'
#' @noRd
2018-01-15 14:51:26 +01:00
is_non_interactive = function () {
ff = sapply(sys.calls(), function(f) as.character(f[1]))
any(ff %in% c('knit2html', 'render')) || !interactive()
}
2018-10-20 11:32:22 +02:00
#' custom recursive lapply with better handling of NULL values
#' from https://stackoverflow.com/a/38950304
2018-10-20 14:52:31 +02:00
#' @noRd
2018-10-20 11:32:22 +02:00
recursive_lapply = function(x, fn) {
if (is.list(x))
lapply(x, recursive_lapply, fn)
else
fn(x)
}