Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The sample data is attached here
- AutonginMake USMakename
- 1 Acura Acura
- 2 Aston Martin Aston Martin
- 3 Audi Audi
- 4 Bentley Bentley
- 5 BMW BMW
- 6 Buick Buick
- 7 Cadillac Cadillac
- 8 Chevrolet Chevrolet
- 9 Chrysler Chrysler
- 10 Dodge Dodge
- 11 Ford Ford
- 12 GMC GMC
- 13 Honda Honda
- 14 HUMMER Hummer
- 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
- #packages needed
- library(plotly)
- library(shiny)
- library(DBI)
- library(RMySQL)
- #connecting db
- dealerinventory1<-dbConnect(RMySQL::MySQL(), user='ghhjjl',
- password='dfgfdgdg!',
- host='hfghfh',
- dbname='hhthhq23u')
- uscount1=dbGetQuery(dealerinventory1,
- 'SELECT count(distinct makename) as USmakes FROM dealer_inventory.CarQuery;')
- autongincount1=dbGetQuery(dealerinventory1,
- 'SELECT count(distinct makename) as autonginmakes FROM dealer_inventory.car_inventory_json_lookup;')
- usandautongintable <- c(autongincount1,uscount1)
- usandautongintable
- label <- c(paste("Autongin Count: ", autongincount1),paste("US Industry Count: ", uscount1))
- label
- unlist <- as.numeric(unlist(usandautongintable))
- typeof(unlist)
- #table used for plotting
- table<- as.data.frame(usandautongintable)
- table
- #for plotting pie chart
- plotpie<- plot_ly(table, labels = label,values = unlist, type = "pie") %>%
- layout(title = 'Comparison of Makes',
- xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
- yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
- plotpie
- library(shiny)
- library(plotly)
- ui= fluidPage(
- plotlyOutput("plot")
- )
- server1<- function(input,output){
- output$plot=renderPlotly({
- plot_ly(table, labels = label,values = unlist, type = "pie") %>%
- layout(title = 'Comparison of Makes',
- xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
- yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
- })
- }
- shinyApp(ui,server1)
- library(shiny)
- library(plotly)
- ui <- fluidPage(tagList(
- plotlyOutput("pie"),
- verbatimTextOutput("makes")
- ))
- server <- function(input, output) {
- # dummy makes data
- usmakes <- c("ford", "acura", "bmw")
- autonginmakes <- c("cadillac", "hummer")
- usmakes_count <- length(usmakes)
- autonginmakes_count <- length(autonginmakes)
- output$pie <- renderPlotly({
- plot_ly(labels=c("US", "Autongin"),
- values=c(usmakes_count, autonginmakes_count),
- key=c("us", "autongin"),
- type="pie")
- })
- output$makes <- renderPrint({
- clicked <- event_data("plotly_click")
- if (is.null(clicked)) {
- "no selection"
- } else if (clicked$key == "us") {
- usmakes
- } else {
- autonginmakes
- }
- })
- }
- shinyApp(ui, server)
Add Comment
Please, Sign In to add comment