celestialgod

customize tooltip

Sep 25th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.77 KB | None | 0 0
  1. #### global.R
  2. library(rCharts)
  3. options(RCHART_WIDTH = 600)
  4. library(shiny)
  5. library(data.table)
  6. library(plyr)
  7. library(dplyr)
  8. library(magrittr)
  9. DT = fread("sample.csv")
  10. unique_kcpn = unique(DT$KC_PRODUCT_NUMBER)
  11. bubbleplot_f = function(DT)
  12. {
  13.   title_name = DT %>% '['(1,) %>%
  14.     select(LOB_NAME, STANDARD_CATEGORY, KC_SUPPLIER_NAME, KC_PRODUCT_NUMBER) %>%
  15.     paste0(collapse=" ") %>% paste("FUB","PAFR","Bubble Chart", sep=" ")
  16.   if (nrow(DT) == 0)
  17.     title_name = "The intersection of selected attributes is empty."
  18.   DT %<>% mutate(x = seq(-1, by = 1, length.out = nrow(.)),
  19.     size = TOTAL_KC_FUB_QTY / max(TOTAL_KC_FUB_QTY), radius = sqrt(size/pi),
  20.     y = PAFR, group = PROJECT_FAMILY) %>% arrange(radius)
  21.   h <- Highcharts$new()
  22.   h$chart(type = "bubble")
  23.   h$title(text = title_name)
  24.   groups <- sort(unique(DT$PROJECT_FAMILY))
  25.   DT_output = DT %>% select(x, y, radius, group, TOTAL_KC_FUB_QTY)
  26.   d_ply(DT_output, .(group), function(dt_sub) {
  27.     h$series(data = toJSONArray2(dt_sub, json = F),
  28.         name = unique(dt_sub$group), type = "bubble",
  29.         marker = list(radius = dt_sub$radius))
  30.   })
  31.   h$xAxis(list(title=(list(text=""))))
  32.   h$yAxis(list(title=(list(text="PAFR"))))
  33.   h$tooltip(useHTML = T, formatter = "#! function() { return 'PAFR:' +
  34.  this.point.y + '\t' + 'PROJECT_FAMILY: ' + this.point.group + '\t' +
  35.  'TOTAL_KC_FUB_QTY: ' + this.point.TOTAL_KC_FUB_QTY; } !#")
  36.   h$exporting(enabled = T)
  37.   h$addParams(height = 700, dom = 'bubbleplot')
  38.   h
  39. }
  40.  
  41. #### ui.R
  42. ui <- pageWithSidebar(
  43. headerPanel('Bubble Charts Download'),
  44. sidebarPanel(
  45. selectInput('lob', label = 'Product Line', choices = c('Notebook','Windows Tablet', 'Android Tablet', 'Desktop')),
  46. selectInput('cate', label = 'Category', choices = c('HDD','LCD','ADP','BAT','SPS')),
  47. textInput('kcpn', label ='KC Part Number', value = "", width = NULL),
  48. actionButton("submit", label = "Submit Query"),
  49. p("Click the button to query the bubble chart.")
  50. ),
  51. mainPanel(
  52. showOutput("bubbleplot", "highcharts"),
  53. tableOutput('table')
  54. )
  55. )
  56.  
  57. #### Server.R
  58. server <- function(input, output){
  59. vals = reactiveValues()
  60. vals$draw.DT = DT %>% filter(rep(FALSE, nrow(.)))
  61. observeEvent(input$submit, {
  62.   vals$draw.DT = DT
  63.   if ((nchar(input$kcpn) == 0) || !(input$kcpn %in% unique_kcpn))
  64.     vals$draw.DT %<>% filter(rep(FALSE, nrow(.)))
  65.   else
  66.     vals$draw.DT %<>% filter(KC_PRODUCT_NUMBER %in% input$kcpn)
  67.   vals$draw.DT %<>% filter(LOB_NAME==input$lob, STANDARD_CATEGORY==input$cate)
  68.   print(vals$draw.DT)
  69. })
  70. output$bubbleplot = renderChart({
  71.   bubbleplot_f(vals$draw.DT)
  72. })
  73. output$table = renderTable({
  74.   if (nrow(vals$draw.DT) > 0)
  75.     vals$draw.DT %>% select(LOB_NAME, PROJECT_FAMILY, KC_PRODUCT_NUMBER, PAFR, TOTAL_KC_FUB_QTY)
  76.   })
  77. }
  78.  
  79. # KH.01KB8.024
  80. shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment