Highcharter Unlimited使用R向下钻取

时间:2019-03-28 08:25:38

标签: r data.table r-highcharter

我看着this SO questionthat SO question。我关注了this github method,请查看ISSUES,以帮助您了解如何解决此问题。

步骤1:将以下内容复制到文本编辑器中,并另存为 test.csv 文件:

我的数据源是NVD JSON FEEDS,我处理并清理并创建了此数据集,其中显示了前25行。

Year,Vendor name,Product name,CVE,Major,Minor,Build,Revision
1988,eric_allman,sendmail,CVE-1999-0095,5,5.58,,
1988,ftp,ftp,CVE-1999-0082,*,,,
1988,ftpcd,ftpcd,CVE-1999-0082,*,,,
1989,bsd,bsd,CVE-1999-1471,4,4.2,,
1989,bsd,bsd,CVE-1999-1471,4,4.3,,
1989,sun,sunos,CVE-1999-1122,4,4.0,,
1989,sun,sunos,CVE-1999-1122,4,4.0,4.0.1,
1989,sun,sunos,CVE-1999-1122,4,4.0,4.0.3,
1989,sun,sunos,CVE-1999-1467,4,4.0,,
1989,sun,sunos,CVE-1999-1467,4,4.0,4.0.1,
1989,sun,sunos,CVE-1999-1467,4,4.0,4.0.2,
1989,sun,sunos,CVE-1999-1467,4,4.0,4.0.3,
1989,sun,sunos,CVE-1999-1467,4,4.0,4.0.3c,
1990,digital,vms,CVE-1999-1057,5,5.3,,
1990,freebsd,freebsd,CVE-2000-0388,3,3.0,,
1990,freebsd,freebsd,CVE-2000-0388,3,3.1,,
1990,freebsd,freebsd,CVE-2000-0388,3,3.2,,
1990,freebsd,freebsd,CVE-2000-0388,3,3.3,,
1990,freebsd,freebsd,CVE-2000-0388,3,3.4,,
1990,hp,apollo_domain_os,CVE-1999-1115,sr10,sr10.2,,
1990,hp,apollo_domain_os,CVE-1999-1115,sr10,sr10.3,,
1990,next,nex,CVE-1999-1392,1,1.0a,,
1990,next,next,CVE-1999-1198,2,2.0,,
1990,next,next,CVE-1999-1391,1,1.0,,
1990,next,next,CVE-1999-1391,1,1.0a,,

第2步:将以下代码复制并粘贴到R中并运行它:

我的代码遵循this SO question,尤其是@NinjaElvis的讨论。我认为可以创建3个或更多级别。只是做更多的研究并弄清楚。

############################
suppressPackageStartupMessages(library("highcharter"))
library("dplyr")
library("purrr")
library("data.table")

second_el_to_numeric <- function(ls){

  map(ls, function(x){
    x[[2]] <- as.numeric(x[[2]])
    x
  })

}

cve_affected_product <- fread("test.csv")

# LAYER ONE YEAR DRILLDOWN VIEW #########################
Year <- cve_affected_product[,c(1:2)]
Year <- unique(Year[,list(Year,`Vendor name`)])
Year <- Year[,c(1)][,count:=1]
Year <- setDT(aggregate(.~ Year ,data=Year,FUN=sum))
#setorder(Year, Year, `Vendor name`)
years_df <- tibble(
  name = c(Year$Year),
  y = c(Year$count),
  drilldown = tolower(paste(name,'id'))
)

ds <- list_parse(years_df)
names(ds) <- NULL

# Vendor View HC ###########
hc <- highchart() %>%
  hc_chart(type = "column") %>%
  hc_title(text = "Basic drilldown") %>%
  hc_xAxis(type = "category") %>%
  hc_yAxis(visible = FALSE,reversed = FALSE) %>%
  hc_legend(enabled = FALSE) %>%
  hc_plotOptions(
    series = list(
      boderWidth = 0,
      dataLabels = list(enabled = TRUE)
    )
  ) %>%
  hc_add_series(
    name = "Vendors",
    colorByPoint = TRUE,
    data = ds
  )


# LAYER TWO VENDOR DRILLDOWN VIEW #########################
Vendor <- cve_affected_product[,c(1:3)]
Vendor <- unique(Vendor[,list(Year,`Vendor name`,`Product name`)])
Vendor <- Vendor[,c(1:2)][,count:=1]
Vendor <- setDT(aggregate(.~ Year+`Vendor name` ,data=Vendor,FUN=sum))
setorder(Vendor, Year, `Vendor name`)

years <- as.character(unique(Vendor$Year))

for(i in 1:length(years)){
  tempdf <- Vendor[Vendor$Year==years[i],]
  dfname <- paste("df",years[i],sep="")
  dsname <- paste("ds",years[i],sep="")

  X <- tibble(
    name = c(tempdf$`Vendor name`),
    y = c(tempdf$count),
    drilldown = tolower(paste(name,'id'))
  )


  Y <- second_el_to_numeric(list_parse2(assign(dfname,X)))

  assign(dsname,Y)


}


# Vendor View HC ###########
hc <- hc %>%
  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(
        id = "1988 id",
        data = ds1988,
        colorByPoint = TRUE,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "1989 id",
        data = ds1989,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "1990 id",
        data = ds1990,
        keys = list('name','y','drilldown')
      )#,
    )
  )

# LAYER THREE PRODUCT DRILLDOWN VIEW #########################
Product <- cve_affected_product[,c(1:4)]
Product <- unique(Product[,list(Year,`Vendor name`,`Product name`, CVE)])
Product <- Product[,c(1:3)][,count:=1]
Product <- setDT(aggregate(.~ Year+`Vendor name`+`Product name` ,data=Product,FUN=sum))
setorder(Product, Year, `Vendor name`,`Product name`)

vendors <- as.character(unique(Product$`Vendor name`))

for(i in 1:length(vendors)){
  tempdf <- Product[Product$`Vendor name`==vendors[i],]
  dfname <- paste("df",vendors[i],sep="")
  dsname <- paste("ds",vendors[i],sep="")

  X <- tibble(
    name = c(tempdf$`Product name`),
    y = c(tempdf$count),
    drilldown = tolower(paste(name,'id'))
  )


  Y <- second_el_to_numeric(list_parse2(assign(dfname,X)))

  assign(dsname,Y)

}
# Product View HC ###########
hc <- hc %>%
  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(
        id = "eric_allman id",
        data = dseric_allman,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "ftp id",
        data = dsftp,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "ftpcd id",
        data = dsftpcd,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "bsd id",
        data = dsbsd,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "sun id",
        data = dssun,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "digital id",
        data = dsdigital,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "freebsd id",
        data = dsfreebsd,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "hp id",
        data = dshp,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "next id",
        data = dsnext,
        keys = list('name','y','drilldown')
      )#,
    )
  )

# LAYER FOUR CVE DRILLDOWN VIEW #########################
Product_CVE <- cve_affected_product[,c(1:5)]
Product_CVE <- unique(Product_CVE[,list(Year,`Vendor name`,`Product name`, CVE, Major)])
Product_CVE <- Product_CVE[,c(1:4)][,count:=1]
Product_CVE <- setDT(aggregate(.~ Year+`Vendor name`+`Product name`+CVE ,data=Product_CVE,FUN=sum))
setorder(Product_CVE, Year, `Vendor name`,`Product name`, CVE)

products <- as.character(unique(Product_CVE$`Product name`))

for(i in 1:length(products)){
  tempdf <- Product_CVE[Product_CVE$`Product name`==products[i],]
  ifelse(tempdf$`Vendor name`==tempdf$`Product name`,
         dfname <- paste("df_",products[i],sep=""),
         dfname <- paste("df",products[i],sep=""))

    ifelse(tempdf$`Vendor name`==tempdf$`Product name`,
         dsname <- paste("ds_",products[i],sep=""),
         dsname <- paste("ds",products[i],sep=""))

  X <- tibble(
    name = gsub("-", "", c(tempdf$CVE)),
    y = c(tempdf$count),
    drilldown = tolower(paste(name,'id'))
  )

  Y <- second_el_to_numeric(list_parse2(assign(dfname,X)))

    assign(dsname,Y)

}

# CVE View HC ###########
hc <- hc %>%
  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(
        id = "sendmail id",
        data = dssendmail,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "ftp id",
        data = ds_ftp,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "ftpcd id",
        data = ds_ftpcd,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "bsd id",
        data = ds_bsd,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "sunos id",
        data = dssunos,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "vms id",
        data = dsvms,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "freebsd id",
        data = ds_freebsd,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "apollo_domain_os id",
        data = dsapollo_domain_os,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "nex id",
        data = dsnex,
        keys = list('name','y','drilldown')
      ),
      list(
        id = "next id",
        data = ds_next,
        keys = list('name','y','drilldown')
      )
    )
  )

输出第一级: enter image description here

输出第二级单击1988: enter image description here

问题: 我应该可以单击供应商 eric_allman 并进行深入研究,但是我没有。我希望能够一直追溯到修订版本(如果存在)。这只是new functionality for my app的原型才能使其工作。但是,高章程并不能使其变得简单或有效。我的数据集有将近400万个观测值。这将是下一个如何解决的难题。

如果我不能使用R来做,我什至考虑使用D3 by creating a JSON file in python。但是,在python中创建JSON文件并非易事,但可行。我目前正在研究python代码作为备份。

感谢您的帮助和任何建议

1 个答案:

答案 0 :(得分:0)

发布此问题后,我很感兴趣,因此我决定进行更多研究,因为大多数研究都向我指出了如何使用javascript进行操作的示例。 Google搜索将我带到this SO response@K. Rohde 这篇文章来自2015,我非常感谢他如何解释这两种不同的方法。我最终使用了两者的混合方法。

对于有兴趣了解向下钻取如何工作的用户,go to my shiny app。在页面Click on Visualizations上,然后在"Drill Down For Vendor CVE Affected Products Versions and Revisions"上尝试一下。同样,如果没有@K. Rohde写完,我就不会做。