在echarts4r中使用循环或映射添加多个e_mark_line或lapply

Add multiple e_mark_line in echarts4r using a loop or map or lapply

我想使用 lapplypmap 设置多个 e_mark_line

library(tidyverse)
library(lubridate)
library(echarts4r)

set.seed(1)
size = 24 * 20

time <-
  seq(
    from = as.POSIXct("2021-01-01 00:00", tz = "UTC"),
    length.out = size,
    by = "hour"
  )

white.noise <- rnorm(n = size)

test_df <- tibble(time, value = cumsum(white.noise))

remarks <-
  tibble(time = sample(time, 10),
         comment = paste("test", sample(1:100, 10)))

test_df %>%
  e_charts(x = time) %>%
  e_line(serie = value) %>%
  e_legend_select(1) %>%
  e_datazoom(type = "slider") %>%
  e_theme("dark") %>%
  e_tooltip(trigger = "axis") %>% 
  e_mark_line(data = list(xAxis=remarks$time[1] ), title = remarks$comment[1]) %>% 
  e_mark_line(data = list(xAxis=remarks$time[2] ), title = remarks$comment[2]) %>% 
  e_mark_line(data = list(xAxis=remarks$time[3] ), title = remarks$comment[3]) 

我尝试使用以下代码使用 pmap 添加多个 e_mark_line,但它不起作用:

test_df %>%
  e_charts(x = time) %>%
  e_line(serie = value) %>%
  e_legend_select(1) %>%
  e_datazoom(type = "slider") %>%
  e_theme("dark") %>%
  e_tooltip(trigger = "axis") %>% 
  {pmap(remarks,~e_mark_line(data=list(xAxis=..1),title==..2))}

不确定是否可以通过 pmap 实现。对我来说,实现你想要的结果的更自然的方法是使用 purrr::reduce2 而不是 pmap 像这样:

library(echarts4r)
library(magrittr)
library(purrr)


test_df %>%
  e_charts(x = time) %>%
  e_line(serie = value) %>%
  e_legend_select(1) %>%
  e_datazoom(type = "slider") %>%
  e_theme("dark") %>%
  e_tooltip(trigger = "axis") %>%
  reduce2(
    .x = remarks$time, .y = remarks$comment,
    .f = function(x, y, z) e_mark_line(x, data = list(xAxis = y), title = z),
    .init = .
  )

数据

set.seed(1)

size <- 24 * 20

time <- seq(
  from = as.POSIXct("2021-01-01 00:00", tz = "UTC"),
  length.out = size,
  by = "hour"
)

white.noise <- rnorm(n = size)

test_df <- data.frame(time, value = cumsum(white.noise))

remarks <- data.frame(
  time = sample(time, 10),
  comment = paste("test", sample(1:100, 10))
)