R语言 如何以编程方式添加雷达图?

vatpfxk5  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(122)

目标

在闪亮的应用程序中获取数据并显示雷达图。数据可能有多个列名不同的列(因为用户名不同)。

示例

下面是一个简单的例子,我在雷达图中放置了3列:

library(echarts4r)
library(tibble)

data_long <- tibble::tibble(
  Construct = c("C", "A", "B"),
  radar1 = c(10, 50, 100),
  calcu2 = c(50, 90, 5),
  foker = c(15, 35, 75)
)

data_long |>
  e_charts(Construct) |>
  e_radar(radar1, max = 100) |>
  e_radar(calcu2) |>
  e_radar(foker)

字符串
我面临的挑战是,除了“Construct”之外的列名将是用户名。所以,我事先不知道列名。e_radar函数需要列名。我如何在不知道列的数量和名称的情况下以编程方式构建雷达图?

xmd2e60i

xmd2e60i1#

也许你可以做一个通用的e_radar函数。你可以用e_radar_()代替e_radar()

library(echarts4r)
library(tibble)

e_radar_unknown_columns <- function(df = NULL){
  
  radarPlot <- df |>
    e_charts(Construct)
  
  for (i in 2:length(df)){
    radarPlot <- radarPlot |>
      e_radar_(names(df)[i], max = 100)
  }
  
  return(radarPlot)  
}

data_long <- tibble::tibble(
  Construct = c("C", "A", "B"),
  oter = c(10, 50, 100),
  calcu2 = c(50, 90, 5),
  foker = c(15, 35, 75)
)

data_long2 <- tibble::tibble(
  Construct = c("C", "A", "B"),
  radar1 = c(10, 50, 100),
  calcu2 = c(1, 100, 55),
  foker = c(50, 40, 90)
)

data_long3 <- tibble::tibble(
  Construct = letters[1:10],
  name1 = sample(1:100, 10),
  name2 = sample(1:100, 10),
  name3 = sample(1:100, 10),
  name4 = sample(1:100, 10),
  name5 = sample(1:100, 10),
  name6 = sample(1:100, 10),
  name7 = sample(1:100, 10),
  name8 = sample(1:100, 10),
  name9 = sample(1:100, 10)
)

e_radar_unknown_columns(data_long)
e_radar_unknown_columns(data_long2)
e_radar_unknown_columns(data_long3)

字符串

相关问题