6

编辑TL;DR 使用crosstalk包,我正在寻找一种方法,将使用长格式数据(线图)的图形与具有宽格式数据的交互式表格链接起来,以便表格中的每一行对应于图中的一条线。

我正在尝试将 DT 表与绘图链接。我的麻烦在于图表需要长格式数据,而表格需要宽格式。我可能已经专注于 tidyverse 做事的方式。我将尝试提供一个最小的示例来说明我正在尝试做什么以及我想获得什么。

设置:

library(tidyverse)
library(crosstalk)
library(plotly)
library(DT)

# Wide format
df_test1 <- data.frame(
  id = c("id1", "id2"),
  item1 = c(0, 4),
  item2 = c(3, 2),
  item3 = c(1, 4),
  item4 = c(3, 4),
  item5 = c(1, NA)
)

# Reshaped to long format
df_test2 <- 
  df_test1 %>%
  tidyr::pivot_longer(cols = item1:item5, names_to = "item", values_to = "value") %>%
  dplyr::mutate(item = as.factor(item)) %>%
  dplyr::mutate(value = factor(as.character(value), levels = c("0", "1", "2", "3", "4")))

我尝试了什么:

sd1 <- SharedData$new(df_test1, key = ~id)

bscols(
  ggplotly(
    sd1$origData() %>%    # should be sd1, but returns error
      # reshaping
      tidyr::pivot_longer(cols = item1:item5, names_to = "item", values_to = "value") %>%
      dplyr::mutate(item = as.factor(item)) %>%
      dplyr::mutate(value = factor(as.character(value), levels = c("0", "1", "2", "3", "4"))) %>%
      # ploting
      ggplot(., aes(x = value, y = item, group = id)) + 
      geom_path() + 
      geom_point(aes(color = value), size = 3) + 
      scale_x_discrete(position = "top", limits = c("0", "1", "2", "3", "4")), 
  tooltip = c("x", "y", "group"), height = 600, width = 300),     
  datatable(sd1)
)  

当然,这只是因为我使用sd1$origData()而不是sd1串扰功能所需的输出。sd1由于 tidyverse 动词不适用于 R6 串扰对象,因此使用会引发错误。无论如何,这提供了所需的图形和表格输出,但没有串扰功能。

我希望得到什么:

sd2 <- SharedData$new(df_test2, key = ~id)

bscols(
  ggplotly(
    # ploting
    ggplot(sd2, aes(x = value, y = item, group = id)) + 
    geom_path() + 
    geom_point(aes(color = value), size = 3) + 
    scale_x_discrete(position = "top", limits = c("0", "1", "2", "3", "4")),     
  tooltip = c("x", "y", "group"), height = 600, width = 300),    
  datatable(sd2)
) 

这适用于我想要的串扰功能的最小示例,但我需要输入DT::datatable宽格式的数据。如示例中的点和路径(标记和轨迹)需要链接到id,这对于宽格式的每一行应该是唯一的。另外,我希望找到一个解决方案,在用户单击所需的表格行之前,所有点和路径都将不可见。
我猜我在这件事上走错了路,可能需要做一些我没有想到的事情。我现在读到,在 2021 年,plotly API 可以使用宽数据 格式,但还没有找到任何例子来说明如何在 R 中实现这一点。

任何帮助将不胜感激。

4

0 回答 0