celestialgod

shiny + rchart + interactive selection

Oct 1st, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.27 KB | None | 0 0
  1. ################################################################################
  2. ## gloabal.R
  3. library(rCharts)
  4. library(plyr)
  5. library(dplyr)
  6. library(magrittr)
  7. library(data.table)
  8. library(shiny)
  9. load("sample.RData")
  10.  
  11. afr_fr = afr.fr %>% tbl_df()
  12. fobqty_fr = fobqty.fr %>% tbl_df()
  13. afr_fr %<>% mutate(REPAIRED_MONTH = as.character(REPAIRED_MONTH))
  14. fobqty_fr %<>% mutate(REPAIRED_MONTH = as.character(REPAIRED_MONTH))
  15. default_choices = afr_fr %>% filter(LOB_NAME=="Desktop")
  16. default_options = default_choices %>% arrange(REPAIRED_MONTH) %>% head(1)
  17.  
  18. ## ui.R
  19. ui <- pageWithSidebar(
  20.   headerPanel('Acer QSBU Quality Analysis Tool'),
  21.   sidebarPanel(
  22.     selectInput('lob', 'Product Line', choices = sort(unique(afr_fr$LOB_NAME)),
  23.       multiple = FALSE),
  24.     selectInput('projects', 'Projects',
  25.       choices = sort(unique(default_choices$PROJECT_FAMILY)),
  26.       multiple = TRUE),
  27.     selectInput('month', 'Month',
  28.       choices = c("", sort(unique(default_choices$REPAIRED_MONTH))),
  29.       multiple = FALSE, selected = ""),
  30.     actionButton("submit", label = "Submit Query"),
  31.     actionButton("reset", label = "Reset Query"),
  32.     p("Click the button to submit the analysis criteria.")
  33.   ),
  34.   mainPanel(
  35.     showOutput("trendchart", "highcharts"),
  36.     tableOutput('table')
  37.    )
  38. )
  39.  
  40.  
  41. ## server.R
  42. server <- function(input, output, session){
  43.   vals = reactiveValues()
  44.   vals$show = FALSE
  45.   vals$reset = FALSE
  46.   vals$lob = "Desktop"
  47.   vals$projects = "empty"
  48.   vals$month = "empty"
  49.   df_initialize = function(){
  50.     vals$AFR_DT = afr_fr %>% tbl_dt()
  51.     vals$FOB_DT = fobqty_fr %>% tbl_dt()
  52.     vals$AFR_DT %<>% filter(LOB_NAME == vals$lob)
  53.     vals$FOB_DT %<>% filter(LOB_NAME == vals$lob)
  54.   }
  55.   isolate({df_initialize()})
  56.  
  57.   test_notnull_f = function(x){
  58.     out = length(x) > 0 && !is.null(x)
  59.     if (is.character(x))
  60.     {
  61.       out = out && nchar(x) > 0
  62.       out = out && x != "empty"
  63.     }
  64.     return(out)
  65.   }
  66.   df_update = function(){
  67.     print(input$projects)
  68.     print(vals$projects)
  69.     print(input$month)
  70.     print(vals$month)
  71.     if (vals$show)
  72.       vals$show = FALSE
  73.     if (!vals$reset)
  74.     {
  75.       if (input$month != vals$month || !all(input$projects %in% vals$projects))
  76.         df_initialize()
  77.       if (test_notnull_f(input$projects))
  78.       {
  79.         vals$AFR_DT %<>% filter(PROJECT_FAMILY %in% input$projects)
  80.         vals$FOB_DT %<>% filter(PROJECT_FAMILY %in% input$projects)
  81.       }
  82.       if (test_notnull_f(input$month))
  83.       {
  84.         vals$AFR_DT %<>% filter(REPAIRED_MONTH == input$month)
  85.         vals$FOB_DT %<>% filter(REPAIRED_MONTH == input$month)
  86.       }
  87.     }
  88.     vals$reset = FALSE
  89.   }
  90.  
  91.   updateInput_f = function(penal_name){
  92.     df_update()
  93.     if (penal_name == "lob")
  94.     {
  95.       updateSelectInput(session, "projects",
  96.         choice = sort(unique(vals$AFR_DT$PROJECT_FAMILY)))
  97.       updateSelectInput(session, "month",
  98.         choice = c("", sort(unique(vals$AFR_DT$REPAIRED_MONTH))), selected = "")
  99.     }
  100.     else if  (penal_name == "month")
  101.     {
  102.       updateSelectInput(session, "projects",
  103.         choice = sort(unique(c(vals$AFR_DT$PROJECT_FAMILY, input$projects))),
  104.         selected = input$projects)
  105.     }
  106.     else if (penal_name == "projects")
  107.     {
  108.       updateSelectInput(session, "month",
  109.         choice = c("", sort(unique(c(vals$AFR_DT$REPAIRED_MONTH, input$month)))),
  110.         selected = input$month)
  111.     }
  112.   }
  113.  
  114.   lob = observe({
  115.     input$lob
  116.     isolate({
  117.       if (input$lob != vals$lob)
  118.       {
  119.         vals$lob = input$lob
  120.         df_initialize()
  121.         df_update()
  122.         updateInput_f("lob")
  123.       }
  124.     })
  125.   })
  126.  
  127.   projects = observe({
  128.     if (is.null(input$projects)) return()
  129.     isolate({
  130.       updateInput_f("projects")
  131.       vals$projects = input$projects
  132.     })
  133.   })
  134.  
  135.   month = observe({
  136.     if (is.null(input$month)) return()
  137.     isolate({
  138.       updateInput_f("month")
  139.       vals$month = input$month
  140.     })
  141.   })
  142.  
  143.   goShowing = observe({
  144.     if (input$submit == 0) return()
  145.     isolate({
  146.       df_update()
  147.       if (test_notnull_f(vals$projects) && test_notnull_f(vals$month))
  148.       {
  149.         vals$show = TRUE
  150.         vals$DRAW_DT = full_join(x=vals$AFR_DT, y=vals$FOB_DT)
  151.         vals$DRAW_DT %<>% mutate(AFR = 100*12*AFR_ROLLING_12M/MM_ROLLING_12M,
  152.           PAFR = 100*12*PAFR_QTY_6M/FOB_ROLLING_M3_M8)
  153.       }
  154.     })
  155.   })
  156.  
  157.   reset = observe({
  158.     input$reset
  159.     isolate({
  160.       vals$show = FALSE
  161.       vals$reset = TRUE
  162.       vals$projects = "empty"
  163.       vals$month = "empty"
  164.       df_initialize()
  165.       updateInput_f("lob")
  166.     })
  167.   })
  168.  
  169.   output$trendchart = renderChart({
  170.     if (vals$show)
  171.     {
  172.       title_name = vals$DRAW_DT %>% head(1) %>% select(LOB_NAME) %>%
  173.         paste0(collapse=" ") %>% paste("AFR Trend", sep=" ")
  174.       title_name = "112"
  175.       trendchart = Highcharts$new()
  176.       trendchart$chart(type = "line")
  177.       trendchart$title(text = title_name)
  178.       draw_df2 = vals$DRAW_DT %>%
  179.         mutate(x = REPAIRED_MONTH, y = AFR, group = PROJECT_FAMILY) %>%
  180.         select(x, y, group)
  181.       d_ply(draw_df2, .(group), function(dt_sub) {
  182.         trendchart$series(data = toJSONArray2(dt_sub, json = F),
  183.             name = unique(dt_sub$group), type = "line")
  184.       })
  185.       trendchart$addParams(height = 1000, width = 800, dom = 'trendchart')
  186.       trendchart$xAxis(title = list(text = ""))
  187.       trendchart$yAxis(title = list(text = ""))
  188.       trendchart$exporting(enabled = T)
  189.       return(trendchart)
  190.     }
  191.     else
  192.     {
  193.       title_name = ""
  194.       trendchart = Highcharts$new()
  195.       trendchart$chart(type = "line")
  196.       trendchart$title(text = title_name)
  197.       trendchart$series(data = list(list(x = NULL, y = NULL)), name = "no_data", type = "line")
  198.       trendchart$addParams(height = 10, width = 10, dom = 'trendchart')
  199.       trendchart$legend(enabled = FALSE)
  200.       trendchart$xAxis(title = list(text = ""), lineWidth = 0)
  201.       trendchart$yAxis(title = list(text = ""))
  202.       return(trendchart)
  203.     }
  204.   })
  205.  
  206.   output$table = renderTable({
  207.     if (vals$show)
  208.       vals$DRAW_DT %>% select(LOB_NAME, SUPPLIER_NAME, PROJECT_FAMILY, AFR, PAFR,
  209.         AFR_ROLLING_12M,MM_ROLLING_12M,PAFR_QTY_6M,FOB_ROLLING_M3_M8,REPAIRED_MONTH)
  210.     else
  211.       data.frame(NULL)
  212.   })
  213. }
  214.  
  215. shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment