xts_to_tibble: list_to_tibble

Usage Arguments Examples

View source: R/xts_to_tibble.R

Usage

1
xts_to_tibble(x, timestamp_format)

Arguments

x
timestamp_format

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (x, timestamp_format) 
{
    performance <- FALSE
    timer <- function(x, txt) {
        message(paste(Sys.time(), txt))
        x
    }
    div <- get_precision_divisor(timestamp_format)
    result_na <- tibble::tibble(statement_id = NA, series_names = NA, 
        series_tags = NA, series_values = NA, series_partial = NA)
    while (!("results" %in% names(x))) {
        x <- purrr::flatten(x)
    }
    x <- purrr::flatten(x)
    list_of_result <- purrr::map(x, .f = function(series_ele) {
        result <- result_na
        if (!is.null(series_ele$statement_id)) {
            statement_id <- series_ele$statement_id
        }
        else {
            statement_id <- NA_integer_
        }
        if (!is.null(series_ele$series)) {
            series_names <- purrr::map(series_ele$series, "name") %>% 
                unlist() %>% if (is.null(.)) 
                NA
            else .
            series_tags <- purrr::map(series_ele$series, "tags") %>% 
                purrr::map(tibble::as_tibble)
            series_columns <- purrr::map(series_ele$series, "columns") %>% 
                purrr::map(unlist)
            series_values <- purrr::map(series_ele$series, "values") %>% 
                if (performance) 
                  timer(., "transpose data")
                else . %>% purrr::map(~purrr::transpose(.)) %>% 
                if (performance) 
                  timer(., "convert influxdb NULL to NA")
                else . %>% purrr::map(~purrr::map(., ~purrr::map(., 
                ~. %||% NA))) %>% if (performance) 
                timer(., "unlist data")
            else . %>% purrr::map(~purrr::map(., base::unlist)) %>% 
                if (performance) 
                  timer(., "unify numerics")
                else . %>% purrr::map(~purrr::map_if(., is.integer, 
                as.double)) %>% if (performance) 
                timer(., "setting column names")
            else . %>% purrr::map2(., .y = series_columns, ~purrr::set_names(., 
                nm = .y)) %>% if (performance) 
                timer(., "set POSIX-based time index")
            else . %>% purrr::map(~purrr::map_at(., .at = "time", 
                ~as.POSIXct(./div, origin = "1970-1-1", tz = "GMT")) %>% 
                tibble::as_tibble(., validate = FALSE))
            series_partial <- ifelse(is.null(series_ele$partial), 
                FALSE, TRUE)
            result <- tibble::tibble(statement_id, series_names, 
                series_tags, series_values, series_partial)
        }
        else {
            if (!is.null(series_ele$error)) {
                stop(series_ele$error, call. = FALSE)
            }
            warning("no series returned")
        }
        return(result)
    })
    return(list_of_result)
  }

crab1234/influxdbr- documentation built on Jan. 12, 2020, 6:28 p.m.