celestialgod

rCharts tooltip download plot

Oct 1st, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.88 KB | None | 0 0
  1. library(plyr)
  2. library(dplyr)
  3. library(shiny)
  4. library(rCharts)
  5. library(magrittr)
  6. load("buublechart.RData")
  7.  
  8. ui <- shinyUI(fluidPage(
  9.  tags$head(tags$script(src = "https://code.highcharts.com/highcharts.js"),
  10.             tags$script(src = "https://code.highcharts.com/highcharts-more.js"),
  11.             tags$script(src = "https://code.highcharts.com/modules/exporting.js"),
  12.             tags$script(src = "https://code.highcharts.com/modules/heatmap.js")
  13.             ),
  14.   headerPanel('Bubble Charts Download'),
  15.   sidebarPanel(
  16.      selectInput('lob', label = 'Product Line', choices = c('Notebook','Windows Tablet', 'Android Tablet', 'Desktop'), selected=FALSE),
  17.      selectInput('cate', label = 'Category', choices = c('HDD','LCD','ADP','BAT','SPS'), selected=F),
  18.      textInput('kcpn', label ='KC Part Number', value = "", width = NULL),
  19.      actionButton("submit", label = "Submit Query"),
  20.      p("Click the button to query the bubble chart.")
  21.   ),
  22.    mainPanel(
  23.      showOutput("bubbleplot", "highcharts"),
  24.      tableOutput('table')
  25.    )
  26. ))
  27.  
  28. server <- function(input, output){
  29.    vals = reactiveValues()
  30.    vals$draw.DT = data.fr %>% filter(rep(FALSE, nrow(.)))
  31.    #browser()
  32.    observeEvent(input$submit, {
  33.      vals$draw.DT = data.fr
  34.      unique_kcpn = unique(data.fr[,"KC_PRODUCT_NUMBER"])
  35.      if ((nchar(input$kcpn) == 0) || !(input$kcpn %in% unique_kcpn))
  36.        vals$draw.DT %<>% filter(rep(FALSE, nrow(.)))
  37.      else
  38.        vals$draw.DT %<>% filter(KC_PRODUCT_NUMBER %in% input$kcpn)
  39.      vals$draw.DT %<>% filter(LOB_NAME==input$lob, STANDARD_CATEGORY==input$cate)
  40.    })
  41.    
  42.    output$bubbleplot = renderChart2({
  43.       title_name = vals$draw.DT %>% head(1) %>%
  44.         select(LOB_NAME, STANDARD_CATEGORY, KC_SUPPLIER_NAME, KC_PRODUCT_NUMBER) %>%
  45.         paste0(collapse=" ") %>% paste("FUB","PAFR","Bubble Chart", sep=" ")
  46.       if (nrow(vals$draw.DT) == 0)
  47.          title_name = "The intersection of selected attributes is empty."
  48.       vals$draw.DT %<>% mutate(x = seq(-1, by = 1, length.out = nrow(.)),
  49.         size = TOTAL_KC_FUB_QTY / max(TOTAL_KC_FUB_QTY), z = sqrt(size/pi),
  50.         y = round(100*PAFR,2), group = PROJECT_FAMILY) %>% arrange(z)
  51.       h <- Highcharts$new()
  52.       h$chart(type = "bubble", height = 800, width = 1200)
  53.       h$title(text = title_name)
  54.       groups <- sort(unique(vals$draw.DT$PROJECT_FAMILY))
  55.       DT_output = vals$draw.DT %>% select(x, y, z, group, TOTAL_KC_FUB_QTY)
  56.       d_ply(DT_output, .(group), function(dt_sub) {
  57.           h$series(data = toJSONArray2(dt_sub, json = F),
  58.             name = unique(dt_sub$group), type = "bubble",
  59.             minSize = "8", maxSize = "50")
  60.       })
  61.       h$xAxis(list(title=(list(text=""))), labels = list(enabled = FALSE),
  62.         tickLength = 0, minorTickLength = 0)
  63.       h$yAxis(list(title=(list(text="PAFR(%)"))))
  64.       h$tooltip(useHTML = T, formatter = "#! function() {
  65.       return 'PAFR:' + this.point.y + '<br/>' +
  66.          'Project: ' + this.point.group + '<br/>' +
  67.          'Total FUB Qty: ' + this.point.TOTAL_KC_FUB_QTY; } !#")
  68.       h$legend(enabled = TRUE)
  69.       h$exporting(enabled = TRUE)
  70.       h$addParams(dom = 'bubbleplot')
  71.       h$addAssets(js =
  72.         c("https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js",
  73.         "https://code.highcharts.com/highcharts.js",
  74.         "https://code.highcharts.com/highcharts-more.js",
  75.         "https://code.highcharts.com/modules/exporting.js",
  76.         "https://code.highcharts.com/modules/heatmap.js"
  77.         )
  78.       )
  79.       h
  80.    })
  81.    
  82.    output$table = renderTable({
  83.      if (nrow(vals$draw.DT) > 0)
  84.        vals$draw.DT %>% mutate(PAFR = round(PAFR*100,2)) %>%
  85.          select(LOB_NAME, PROJECT_FAMILY, KC_SUPPLIER_NAME,
  86.            KC_PRODUCT_NUMBER, PAFR, TOTAL_KC_FUB_QTY) %>%
  87.            set_names(c('Product Line', 'Project', 'Supplier','Product Number', 'PAFR(%)', 'Total FUB Qty'))
  88.    })
  89. }
  90.  
  91. shinyApp(ui, server)
Add Comment
Please, Sign In to add comment