Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. ## app.R ##
  2. library(shinydashboard)
  3. library(dplyr)
  4. library(ggplot2)
  5. library(ggthemes)
  6. library(data.table)
  7. library(leaflet)
  8. library(htmltools)
  9. library(DT)
  10. library(rsconnect)
  11. library(gridExtra)
  12. library(maptools)
  13. library(sp)
  14. library(rgeos)
  15.  
  16. # setwd("~/NYC Data Science Academy/NYParkingDashboard/")
  17. source("global.R")
  18. # source("~/NYC Data Science Academy/NYParkingDashboard/global.R")
  19. # source("~/NYC Data Science Academy/NYParkingDashboard/helpers.R")
  20.  
  21. # dbname = "~/NYC Data Science Academy/NYParkingDashboard/nyparking.sqlite"
  22. # tblname = "nyparking"
  23. #
  24. # dbname_map = "~/NYC Data Science Academy/NYParkingDashboard/Police_Precincts.sqlite"
  25. # tblname_map = "ogrgeojson"
  26.  
  27.  
  28. ui <- dashboardPage(
  29.  
  30.  
  31. dashboardHeader(title = "NYC Parking Tickets"),
  32. dashboardSidebar(
  33. sidebarMenu(
  34. menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
  35. # menuItem("Summary", tabName = "bar_graph", icon = icon("th")),
  36. menuItem("Map", tabName = "map", icon = icon("map")),
  37. menuItem("Data", tabName = "data", icon = icon("database"))
  38. ),
  39. # selectizeInput(
  40. # selectizeInput("selected",
  41. # "Select Item to Display",
  42. # choices = unique(nyparking_data[, Item]))
  43. # )
  44. selectizeInput(inputId = "fiscal_year",
  45. label = "Fiscal Year",
  46. width = 140,
  47. choices = unique(na.omit(nyparking_data[, `Report Year`]))
  48. )),
  49. ## Body content
  50. dashboardBody(
  51. tabItems(
  52. # First tab content
  53. # tabItem(tabName = "dashboard",
  54. # fluidRow(
  55. # box(plotOutput("plot1", height = 250)),
  56. #
  57. # box(
  58. # title = "Controls",
  59. # sliderInput("slider", "Number of observations:", 1, 100, 50)
  60. # )
  61. # )
  62. # ),
  63. # Bar Graph tab content
  64. tabItem(tabName = "dashboard",
  65. fluidRow(box(plotOutput("number",
  66. height = 300),
  67. plotOutput(
  68. "fine_plot",height=300#300
  69. )
  70. ),
  71. box(
  72. title = "Controls",
  73.  
  74. selectizeInput(inputId = "item",
  75. label = "Ticket Data by Group",
  76.  
  77. choices = unique(nyparking_data[, Item]))
  78. )
  79. )),
  80.  
  81. # Map tab content
  82. tabItem(tabName = "map",
  83. fluidRow(
  84. box(
  85. leafletOutput("map",
  86. width="100%"),
  87. width=600)
  88.  
  89. )),
  90.  
  91. # Data tab content
  92. tabItem(tabName = "data",
  93. fluidRow(box(DT::dataTableOutput("table"),
  94. width = 12)))
  95.  
  96. ))
  97. )
  98.  
  99.  
  100.  
  101. server <- function(session,input, output) {
  102.  
  103. # conn <- dbConnector(session,dbname = dbname)
  104.  
  105.  
  106. output$table<-DT::renderDataTable({
  107. datatable(nyparking_data,rownames=FALSE)%>%
  108. formatStyle(input$item,
  109. background="Skyblue",
  110. fontWeight="bold")
  111. })
  112.  
  113. ##### Using dataframe and not SQLite
  114. group_data <- reactive({
  115. if (input$item=="Report Year"){
  116. filter(nyparking_data,Item==input$item)%>%
  117. arrange(desc(n))%>%
  118. head(7)
  119.  
  120. }else{
  121. filter(nyparking_data,Item==input$item&
  122. `Fiscal Year`==input$fiscal_year)%>%
  123. arrange(desc(n))%>%
  124. head(7)
  125. }
  126.  
  127. })
  128.  
  129. # This part uses SQLite to get datatable
  130. # parking_db <- reactive(dbGetData(conn = conn,
  131. # tblname = tblname,
  132. # item=input$item,
  133. # fiscal_year=input$fiscal_year)
  134. # )
  135.  
  136.  
  137. #### Output Bar Graph using GGplot
  138. #### Grouped by various factors
  139. output$number <- renderPlot(
  140.  
  141. ##### SQLite output
  142. # parking_db()%>%
  143. #####
  144. ##### Data Frame
  145. # count_plot<-
  146. group_data() %>%
  147. ggplot(aes(x = get(input$item), y = n)) +
  148. geom_col(fill = "skyblue",na.rm=TRUE) +
  149. xlab(input$item) +
  150. ylab("Num of Tickets") +
  151. ggtitle("Number of Tickets")+
  152. theme_economist()
  153.  
  154.  
  155. )
  156.  
  157. output$fine_plot<- renderPlot(
  158. group_data() %>%
  159. ggplot(aes(x = get(input$item), y = total_fine)) +
  160. geom_col(fill = "#d95f0e",na.rm=TRUE) +
  161. xlab(input$item) +
  162. ylab("Total Fine Amount") +
  163. ggtitle("Total Fine($)")+
  164. theme_economist()
  165.  
  166.  
  167.  
  168. )
  169.  
  170. #### Draw map by using leaflet
  171. #### Maps out Precinct and color by
  172. #### number of tickets in the precinct
  173.  
  174. choose_map <- reactive({
  175.  
  176. filter(precinct_map_df,`Fiscal Year`==input$fiscal_year)%>%
  177. select(n,total_fine,precinct)
  178. })
  179.  
  180.  
  181.  
  182. ?row.names
  183. ?SpatialPolygonsDataFrame
  184. output$map<- renderLeaflet({
  185. #
  186. # row.names(choose_map())=row.names(precinct_map)
  187. # chosen_map = SpatialPolygonsDataFrame(precinct_map,choose_map())
  188. #
  189. # chosen_map <- filter(precinct_map_df,`Fiscal Year`==2015)%>%
  190. # select(n,total_fine,precinct)
  191. chosen_map<-choose_map()
  192. row.names(chosen_map)=row.names(precinct_map)
  193. chosen_map = SpatialPolygonsDataFrame(precinct_map,chosen_map)
  194.  
  195.  
  196. bins <- c(0, 50000, 70000, 100000, 150000, 200000, 300000, Inf)
  197. pal <- colorBin("YlOrRd", domain = as.numeric(chosen_map$n), bins = bins)
  198.  
  199.  
  200. hover_text<-mapply(function(x, y, z) {
  201. htmltools::HTML(sprintf("%s <br> %s <br> %s",
  202. htmlEscape(x),
  203. htmlEscape(y),
  204. htmlEscape(z)))},
  205. paste0("Precint: ",as.character(chosen_map$precinct)),
  206. paste0("Num of Tickets: ",as.character(
  207. formatC(chosen_map$n, format="d", big.mark=","))),
  208. paste0("Dollar Amount of Tickets: ","$",as.character(
  209. formatC(chosen_map$total_fine, format="d", big.mark=","))
  210. ),
  211. SIMPLIFY = F)
  212.  
  213. leaflet(chosen_map) %>%
  214. setView(-74, 40.75, 10) %>%
  215. addProviderTiles("MapBox", options = providerTileOptions(
  216. id = "mapbox.light",
  217. accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN')))%>%
  218. addTiles() %>%
  219. addPolygons(fillColor = ~pal(as.numeric(chosen_map$n)),
  220. weight = 2,
  221. opacity = 1,
  222. color = "white",
  223. dashArray = "3",
  224. fillOpacity = 0.7,
  225. highlight = highlightOptions(
  226. weight = 5,
  227. color = "#666",
  228. dashArray = "",
  229. fillOpacity = 0.7,
  230. bringToFront = TRUE),
  231. label = lapply(as.character(hover_text),HTML),
  232. labelOptions = labelOptions(
  233. style = list("font-weight" = "normal", padding = "3px 8px"),
  234. textsize = "15px",
  235. direction = "auto"))
  236.  
  237. })
  238. }
  239.  
  240. shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement