Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# History files
.Rhistory
.Rapp.history

# Session Data files
.RData

# Example code in package build process
*-Ex.R

# Output files from R CMD build
/*.tar.gz

# Output files from R CMD check
/*.Rcheck/

# RStudio files
.Rproj.user/

# produced vignettes
vignettes/*.html
vignettes/*.pdf

# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
.httr-oauth

# knitr and R markdown default cache directories
/*_cache/
/cache/

# Temporary files created by R markdown
*.utf8.md
*.knit.md

*.Rproj
.Rhistory
.RData
.Ruserdata
.Rproj.user
13 changes: 13 additions & 0 deletions CalgaryCrime.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
148 changes: 148 additions & 0 deletions Shiny-Intergration/LoadData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#Fix for rjava issue in linux
#https://stackoverflow.com/questions/28462302/libjvm-so-cannot-open-shared-object-file-no-such-file-or-directory

library(xlsx)
library(dplyr)
library(tidyr)
library(readr)
library(rgdal)

f_dowle3 = function(DT) {

# or by number (slightly faster than by name) :
for (j in seq_len(ncol(DT)))
set(DT,which(is.na(DT[[j]])),j,0)
}

xlsx_dir <- "Shiny-Intergration/RawData"
working_dir <- getwd()

list_file_xls <-
list.files(
path = working_dir,
pattern = "xls$",
recursive = TRUE,
full.names = TRUE
)
list_file_csv <-
list.files(
path = paste0(working_dir, '/', 'RawData'),
pattern = "csv$",
recursive = TRUE,
full.names = TRUE
)
list.files(
path = paste0(working_dir, '/', 'RawData'),
pattern = "shp$",
recursive = TRUE,
full.names = TRUE
)
shapefiles <-
list.files(
path = paste0(working_dir, '/', 'RawData'),
pattern = "shp$",
recursive = TRUE,
full.names = TRUE
)


#for now just grab the first item in the list
#but this could be migrated to grab the latest

#this is really slow with na.strings
#https://cran.r-project.org/web/packages/XLConnect/vignettes/XLConnect.pdf
#might be a valid replacement
CalgaryCrime <-
read.xlsx(
file = list_file_xls[1],
sheetIndex = 2,
startRow = 2,
na.strings = c(""),
stringsAsFactors = FALSE
)
colnames(CalgaryCrime)[1:2] <- c("Category", "Community")
CalgaryCrime[is.na(CalgaryCrime)] <- 0

#run a for loop across the columnsize to fix the names
names <- colnames(CalgaryCrime)
names <- names[3:length(names)]

intialYear <- 2012
j <- 1


for (i in 1:length(names)) {
if (j < 10) {
names[i] <- paste0('0', j, '/01/', intialYear)

} else{
names[i] <- paste0(j, '/01/', intialYear)
}

if (j == 12) {
intialYear <- intialYear + 1
j <- 1
} else{
j <- (j + 1)
}

}

tmpnames <- colnames(CalgaryCrime)
tmpnames[3:ncol(CalgaryCrime)] <- names
colnames(CalgaryCrime) <- tmpnames


#Applying data minp based on the work of Marc Boulet & Calgary R User Group
CalgaryCrime <-
select(CalgaryCrime,-71:-75) # remove unused columns
CalgaryCrimeTidy <-
CalgaryCrime %>% gather(Date, Cases, 3:70) # move data columns into one column
CalgaryCrimeTidy$Date <-
as.Date(CalgaryCrimeTidy$Date, format = "%m/%d/%Y") # convert Date column to date format

CalgaryCensus <- read_csv(list_file_csv[1])
CalgaryCensus$AvgPop <-
rowMeans(subset(CalgaryCensus, select = c(2:6))) # calc 5 yr pop average
CalgaryData <-
left_join(CalgaryCrimeTidy, CalgaryCensus, by = "Community") # add pop data to crime data

#map to a data.table and clean up
CalgaryData <- data.table(CalgaryData)
CalgaryData <- CalgaryData[Category != "0" | Community != "0"]
# remove na's
f_dowle3(CalgaryData)


CatTotal <- CalgaryData %>%
group_by(Category) %>%
summarise(TotalByCategory = sum(Cases)) %>%
arrange(desc(TotalByCategory))

CatTotal <- data.table(CatTotal)

CasesTotal <- CalgaryData %>%
group_by(Community) %>%
summarise(TotalByCommunity = sum(Cases)) %>%
arrange(desc(TotalByCommunity))

CasesTotal <- data.table(CasesTotal)


boundries <-
readOGR(shapefiles[1],
layer = 'geo_export_48183217-cea2-47fe-bb74-c513d0cfd1fe',
GDAL1_integer64_policy = TRUE)

community <- append(c(""), unique(CalgaryData$Community))
crimeTypes <- append(c("") , unique(CalgaryData$Category))
dateRange <- append(c("") , unique(as.character(CalgaryData[,Date])))




#clean up
remove(list = c("i", "j", "intialYear", "names", "tmpnames"))



Binary file not shown.
Loading