Guest User

Untitled

a guest
Sep 14th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. The sample data is attached here
  2.  
  3.  
  4. AutonginMake USMakename
  5. 1 Acura Acura
  6. 2 Aston Martin Aston Martin
  7. 3 Audi Audi
  8. 4 Bentley Bentley
  9. 5 BMW BMW
  10. 6 Buick Buick
  11. 7 Cadillac Cadillac
  12. 8 Chevrolet Chevrolet
  13. 9 Chrysler Chrysler
  14. 10 Dodge Dodge
  15. 11 Ford Ford
  16. 12 GMC GMC
  17. 13 Honda Honda
  18. 14 HUMMER Hummer
  19. I took the count of above autonginmake and US make and polotted..My requirement is to list this makes when clicking on corresponding regions of pie chart
  20.  
  21.  
  22. #packages needed
  23. library(plotly)
  24. library(shiny)
  25. library(DBI)
  26. library(RMySQL)
  27. #connecting db
  28. dealerinventory1<-dbConnect(RMySQL::MySQL(), user='ghhjjl',
  29. password='dfgfdgdg!',
  30. host='hfghfh',
  31. dbname='hhthhq23u')
  32.  
  33.  
  34. uscount1=dbGetQuery(dealerinventory1,
  35. 'SELECT count(distinct makename) as USmakes FROM dealer_inventory.CarQuery;')
  36.  
  37. autongincount1=dbGetQuery(dealerinventory1,
  38. 'SELECT count(distinct makename) as autonginmakes FROM dealer_inventory.car_inventory_json_lookup;')
  39.  
  40.  
  41. usandautongintable <- c(autongincount1,uscount1)
  42. usandautongintable
  43. label <- c(paste("Autongin Count: ", autongincount1),paste("US Industry Count: ", uscount1))
  44. label
  45.  
  46. unlist <- as.numeric(unlist(usandautongintable))
  47.  
  48. typeof(unlist)
  49. #table used for plotting
  50. table<- as.data.frame(usandautongintable)
  51. table
  52. #for plotting pie chart
  53. plotpie<- plot_ly(table, labels = label,values = unlist, type = "pie") %>%
  54. layout(title = 'Comparison of Makes',
  55. xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
  56. yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
  57.  
  58. plotpie
  59.  
  60. library(shiny)
  61. library(plotly)
  62.  
  63.  
  64. ui= fluidPage(
  65. plotlyOutput("plot")
  66. )
  67.  
  68.  
  69. server1<- function(input,output){
  70. output$plot=renderPlotly({
  71. plot_ly(table, labels = label,values = unlist, type = "pie") %>%
  72. layout(title = 'Comparison of Makes',
  73. xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
  74. yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
  75. })
  76. }
  77.  
  78. shinyApp(ui,server1)
  79.  
  80. library(shiny)
  81. library(plotly)
  82.  
  83. ui <- fluidPage(tagList(
  84. plotlyOutput("pie"),
  85. verbatimTextOutput("makes")
  86. ))
  87.  
  88. server <- function(input, output) {
  89. # dummy makes data
  90. usmakes <- c("ford", "acura", "bmw")
  91. autonginmakes <- c("cadillac", "hummer")
  92. usmakes_count <- length(usmakes)
  93. autonginmakes_count <- length(autonginmakes)
  94.  
  95. output$pie <- renderPlotly({
  96. plot_ly(labels=c("US", "Autongin"),
  97. values=c(usmakes_count, autonginmakes_count),
  98. key=c("us", "autongin"),
  99. type="pie")
  100. })
  101.  
  102. output$makes <- renderPrint({
  103. clicked <- event_data("plotly_click")
  104. if (is.null(clicked)) {
  105. "no selection"
  106. } else if (clicked$key == "us") {
  107. usmakes
  108. } else {
  109. autonginmakes
  110. }
  111. })
  112. }
  113.  
  114. shinyApp(ui, server)
Add Comment
Please, Sign In to add comment