Highcharter Unlimited使用R向下钻取 [英] Highcharter Unlimited Drill down using R

查看:89
本文介绍了Highcharter Unlimited使用R向下钻取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了



输出第二级点击1988年:



问题:
我应该是能够单击供应商 eric_allman 并进行深入分析,但是我没有。我希望能够一直追溯到修订版本(如果存在)。这只是我的应用的新功能的原型。但是,高章程并不能使其变得简单或有效。我的数据集有将近400万个观测值。



我什至正在考虑使用 D3,如果我无法使用R创建JSON文件,则可以在中创建。但是,在python中创建JSON文件并非易事,但可行。我目前正在使用python代码作为备份。



感谢您的帮助和任何建议

解决方案

在发布此问题之后,很有趣的是,我决定进行更多的研究,因为大多数研究都向我指出了如何在javascript中进行操作的示例。 Google搜索将我带到此SO响应, a href = https://stackoverflow.com/users/5836932/k-rohde> @K。罗德(Rohde)
这篇文章来自 2015 ,我真的很感谢他如何解释这两种不同的方法。



对于那些有兴趣了解向下钻取工作原理的人来说,转到我的闪亮应用。在页面单击可视化 c之后,然后深入研究供应商CVE受影响的产品版本和修订 并尝试出来。同样,如果没有 @K,我将无法完成。罗德(Rohde)写出来。


I looked at this SO question, that SO question. I followed this github method check out ISSUES to help get ideas on how to solve this.

Step 1: Copy the following into text editor and save as test.csv file:

My datasource is NVD JSON FEEDS which I processed and cleaned and created this dataset with the first 25 rows shown here.

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,,

Step 2: Copy and paste following code into R and run it:

My codes follow this SO question especially the discussion by @NinjaElvis. I think it is possible to create 3 levels or more. Just doing more research and figuring out.

############################
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')
      )
    )
  )

Output First Level:

Output Second Level Clicking on 1988:

ISSUES: I should be able to click on vendor eric_allman and get a drill down, however I am not. I want to be able to drill down all the way to Revision if it exists. This is just prototypying new functionality for my app to get it working. However, highcharter does not make this easy or efficient. My dataset has almost 4 million observations. That will be the next struggle how to handle that.

I am even considering using D3 by creating a JSON file in python if I cannot do it using R. However, creating the JSON file in python is not trivial but doable. I am currently working on python code as a back up.

Thank you for the help and any suggestions

解决方案

Interestingly enough after posting this question, i decided to do more research, since most of the research was pointing me to example of how to do it in javascript. Google search took me to this SO response by @K. Rohde This post is from 2015 and I really appreciate how he explained the two different approaches. I ended up using hybrid approach borrowing from both.

For those interested in seeing how the drill down is working, go to my shiny app. Once you on the page Click on Visualizations, then "Drill Down For Vendor CVE Affected Products Versions and Revisions"and try it out. Again I would not have done it without @K. Rohde write up.

这篇关于Highcharter Unlimited使用R向下钻取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆