Last active
November 8, 2018 09:13
-
-
Save ColinFay/2d737c7c9387ff815ea5da0f792cade1 to your computer and use it in GitHub Desktop.
df to dygraph?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dygraphs) | |
library(magrittr) | |
df <- data.frame( | |
hour = seq( as.POSIXct("2019-01-01"), as.POSIXct("2019-01-02"), length.out = 48 ), | |
num = 1:48, | |
freq = rep(1:2, times = 24) | |
) | |
# Doesn't work | |
df %>% | |
dygraph() | |
# Works but not very clean | |
df %>% | |
xts::xts(order.by = .$hour, frequency = .$freq) %>% | |
dygraph() | |
# Is there something like: | |
df %>% | |
to_dg(order_by = hour, frequency = frequency) %>% | |
dygraph() | |
# Quick coding : | |
to_dg <- function( | |
df, order_by, | |
freq = NULL, | |
unique = TRUE, | |
tzone = Sys.getenv("TZ"), | |
... | |
){ | |
order_by <- df[[deparse(substitute(order_by))]] | |
if (!is.null(freq)) { | |
freq <- df[[deparse(substitute(freq))]] | |
} | |
xts::xts( | |
x = df, | |
order.by = order_by, | |
frequency = freq, | |
unique = unique, | |
tzone = tzone, | |
... | |
) | |
} | |
df %>% | |
to_dg(order_by = hour, freq = frequency) %>% | |
dygraph() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rstudio/dygraphs#17