'prepare xts entry in a table for hc_add_series_list
I am working with a tibble containing a column of xts as in the following example:
oneDf <- data.frame( name = "first", t = seq(now(),length=5,by = "days"), val = runif(5))
secDf <- data.frame( name = "sec",t = seq(now(),length=5,by = "days"), val = runif(5))
nested_data <- union(oneDf,secDf) %>%
group_by(name) %>%
nest() %>%
mutate(data = map(data, ~.x %>%
transmute(val, t) %>%
column_to_rownames("t") %>% as.xts()))
> nested_data
# A tibble: 2 × 2
# Groups: name [2]
name data
<chr> <list>
1 first <xts [5 × 1]>
2 sec <xts [5 × 1]>
The aim is to use hc_add_series_list to show the xts all named by the name-col. The preparation for hc_add_series (where the problem occurs) is as follows:
test <- nested_data %>%
group_by(name) %>%
do( ds = list( data = list_parse2(data.frame(row.names(.$data),coredata(.$data))))) %>%
{map2(.$name,.$ds, function(x,y){append(list(name=x),y)})}
...generates an Error due to the date-frame generation, so that I cannot use, as intended
highchart() %>%
hc_xAxis(type = "datetime") %>%
hc_add_series_list(test)
How do I have to change the data.frame(...) to get this to work? Thank you in advance,
Solution 1:[1]
As I work with nested data, the following leads to an output:
test <- nested_data %>%
mutate(ds = map(data,function(x) {
timestamps <- time(x) %>% as.POSIXct() %>% as.numeric()
values <- as.numeric(x)
list( data = list_parse2(data.frame(timestamps,values))) })) %>%
{map2(.$name,.$ds, function(x,y){append(list(name=x),y)})}
... but still Highcharter shows the wrong time coordinate (points started at Jan 20(??) with a distance of some minutes...). timestamps <- index(x) leads to the same strange behavior...
edit: solved, timestamps <- index(x) * 1000 did the job :-)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
