Skip to content

Instantly share code, notes, and snippets.

@ColinFay
Last active November 8, 2018 09:13
Show Gist options
  • Save ColinFay/2d737c7c9387ff815ea5da0f792cade1 to your computer and use it in GitHub Desktop.
Save ColinFay/2d737c7c9387ff815ea5da0f792cade1 to your computer and use it in GitHub Desktop.
df to dygraph?
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()
@dmypstl
Copy link

dmypstl commented Nov 8, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment