diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..896d63c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM plotly/heroku-docker-r:3.6.2_heroku18 + +# on build, copy application files +COPY . /app/ + +# for installing additional dependencies etc. +RUN if [ -f '/app/onbuild' ]; then bash /app/onbuild; fi; + +# look for /app/apt-packages and if it exists, install the packages contained +RUN if [ -f '/app/apt-packages' ]; then apt-get update -q && cat apt-packages | xargs apt-get -qy install && rm -rf /var/lib/apt/lists/*; fi; + +# look for /app/init.R and if it exists, execute it +RUN if [ -f '/app/init.R' ]; then /usr/bin/R --no-init-file --no-save --quiet --slave -f /app/init.R; fi; + +# here app.R needs to match the name of the file which contains your app +CMD cd /app && /usr/bin/R --no-save -f /app/app.R diff --git a/src/app.R b/app.R similarity index 69% rename from src/app.R rename to app.R index e3c4bbd..35dc2e3 100644 --- a/src/app.R +++ b/app.R @@ -1,5 +1,5 @@ # Author: Farnaz & Racquelle -# Date: March 26, 2020 +# Date: April 7, 2020 "This script is the main file that creates a Dash app. @@ -24,15 +24,17 @@ data <- suiciderates %>% # BOXPLOT (PLOT 1) make_plot1= function(xaxis="Generation X"){ + generation_key= DropdownKey$label[DropdownKey$value==xaxis] boxplot = data %>% filter(generation==xaxis) %>% ggplot() + - geom_violin(aes(x=generation, y=suicides_no, fill=generation)) + + geom_violin(aes(x=sex, y=suicides_no, fill=sex)) + theme_bw(12) + - xlab("Generation") + + theme(legend.position = "none")+ + theme(axis.title.x = element_blank()) + ylab("Number of Suicides")+ - labs(fill="Generation") + - ggtitle("Number of Suicides for each generation (1985-2016)") + + scale_y_continuous(labels = scales::comma_format())+ + ggtitle(paste0("Number of suicides in ", generation_key," based on sex (1985-2016)")) + theme(plot.title = element_text(hjust = 0.5)) ggplotly(boxplot, tooltip=c("text")) @@ -46,20 +48,23 @@ Dropdown <- dccDropdown( options = map(1:nrow(DropdownKey), function(i){ list(label=DropdownKey$label[i], value=DropdownKey$value[i]) }), - value = "Generation X" + value = "Generation X", + clearable= FALSE ) ## HISTOGRAM (PLOT 2) make_plot2= function(xaxis="Canada"){ + country_key= DropdownKey1$label[DropdownKey1$value==xaxis] histogram = data %>% filter(country==xaxis) %>% ggplot() + - geom_boxplot(aes(x=country, y=suicides_no, fill=country)) + + geom_boxplot(aes(x=sex, y=suicides_no, fill=sex)) + theme_bw(12) + - xlab("Country") + + theme(legend.position = "none")+ ylab("Number of Suicides") + - labs(fill="Country") + - ggtitle("Number of Suicides for each country (1985-2016)") + + theme(axis.title.x = element_blank()) + + scale_y_continuous(labels = scales::comma_format()) + + ggtitle(paste0("Number of suicides in ", country_key," based on sex (1985-2016)")) + theme(plot.title = element_text(hjust = 0.5)) ggplotly(histogram, tooltip=c("text")) @@ -73,17 +78,19 @@ Dropdown1 <- dccDropdown( options = map(1:nrow(DropdownKey1), function(i){ list(label=DropdownKey1$label[i], value=DropdownKey1$value[i]) }), - value = "Canada" + value = "Canada", + clearable=FALSE ) ## LINE GRAPH (PLOT 3) make_plot3= function(){ - line1 = ggplot(data, aes(year, gdp_per_capita...., fill=country)) + - geom_jitter() + - xlab("Year") + + line1 = data %>% + ggplot(aes(x=year, y=gdp_per_capita....)) + + geom_line(aes(colour=country)) + ylab("GDP per capita") + + xlab("Year") + + guides(colour="none") + scale_y_continuous(labels = scales::dollar_format()) + - labs(fill="Country") + theme_minimal(12) + ggtitle("Change of GDP per capita over time") + theme(plot.title = element_text(hjust = 0.5)) @@ -96,23 +103,16 @@ header1 <- htmlH1("Do socioeconomic factors play a role in suicides in North Am header3 <- htmlH3("By: Racquelle and Farnaz") desc_usage <- dccMarkdown(" -**Welcome to our Dashboard** +**Welcome to our dashboard!** In this app you can find information on how *socio-economic factors* play an important role in the *number of suicide rates* in different countries and how they change between 1985 to 2016. There are 3 plots that show how suicide rates change according to changes in socio-economic factors. The violin plot provides information on how Generation can affect number of suicides. -The box plot provides information on how number of suicides change according to country. - -The jitter plot is very informative in interpreting the trend of GDP per capita vs year in different countries (Canada, Mexico, United States). This dashboard has dropdown menus for the violin plot and box plot that will essentially allow the user to choose another variable rather than what is shown on the plots. +The box plot provides information on how number of suicides change according to country. The jitter plot is very informative in interpreting the trend of GDP per capita vs year in different countries (Canada, Mexico, United States). This dashboard has dropdown menus for the violin plot and box plot that will essentially allow the user to choose another variable rather than what is shown on the plots. You can find the link to the suicide dataset [here](https://raw.githubusercontent.com/STAT547-UBC-2019-20/data_sets/master/suiciderates_clean.csv). - -**Usage** - -David is a sociologist who is interested in the assessment and prevention of suicide as part of his major. -He is wondering how different socio-economic variables can affect suicide rates. -He is also curious to know if those socio-economic factors intersect together. -One day he is searching on the web and he finds our app that provides information on how socio-economic variables interact with the number of suicides. ") +image1 = dccMarkdown("![] (https://raw.githubusercontent.com/STAT547-UBC-2019-20/group_13/master/images/old%20(2).png)") +image2= dccMarkdown("![] (https://raw.githubusercontent.com/STAT547-UBC-2019-20/group_13/master/images/north-america%20(1).png)") ## Create Dash instance app <- Dash$new() @@ -137,7 +137,7 @@ div_header2 = htmlDiv( ), style = list( backgroundColor = '#F5F5F5', - textAlign = 'left', + textAlign = 'center', color = 'black', margin = 2, marginTop = 0 @@ -147,26 +147,41 @@ div_header2 = htmlDiv( # Side bar with dropdowns div_siderbar = htmlDiv( - list(htmlLabel('Generation'), - htmlBr(), + list(image1, Dropdown, htmlBr(), htmlBr(), - htmlLabel('Country'), htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + htmlBr(), + image2, Dropdown1 ), style = list('background-color' = '#FFEBEE', 'textAlign'='center', 'color'= 'black', - 'padding' = 10, - 'flex-basis' = '20%') + 'padding' = '15', + 'flex-basis' = '17%') ) # Plots div_main <- htmlDiv( list(dccGraph(id='Violin', figure = make_plot1()), + htmlBr(), + htmlBr(), dccGraph(id='Boxplot', figure = make_plot2()), + htmlBr(), + htmlBr(), dccGraph(id='Scatter', figure = make_plot3()) ) ) @@ -184,7 +199,7 @@ app$layout( 'justify-content'='space-around') ) ) - + ## APP CALLBACKS @@ -205,4 +220,4 @@ app$callback( }) ## Run app -app$run_server(debug=TRUE) +app$run_server(debug=TRUE) \ No newline at end of file diff --git a/app.json b/app.json new file mode 100755 index 0000000..9ce9ce1 --- /dev/null +++ b/app.json @@ -0,0 +1,9 @@ +{ + "name": "Suicides in North America between 1985-2016", + "description": "Do socioeconomic factors play a role in suicides?", + "keywords": [ + "heroku", + "R deployment", + "Dash R" +], +} diff --git a/apt-packages b/apt-packages new file mode 100755 index 0000000..187885d --- /dev/null +++ b/apt-packages @@ -0,0 +1,3 @@ +libcurl4-openssl-dev +libxml2-dev +libv8-3.14-dev diff --git a/heroku.yml b/heroku.yml new file mode 100755 index 0000000..2b8f79b --- /dev/null +++ b/heroku.yml @@ -0,0 +1,3 @@ +build: + docker: + web: Dockerfile \ No newline at end of file diff --git a/init.R b/init.R new file mode 100755 index 0000000..a3f59cf --- /dev/null +++ b/init.R @@ -0,0 +1,22 @@ +# R script to run author supplied code, typically used to install additional R packages +# contains placeholders which are inserted by the compile script +# NOTE: this script is executed in the chroot context; check paths! + +r <- getOption('repos') +r['CRAN'] <- 'http://cloud.r-project.org' +options(repos=r) + +# ====================================================================== + +# packages go here +install.packages('remotes') + +remotes::install_github('plotly/dashR', upgrade=TRUE) +install.packages('dash') +install.packages('here') +install.packages('dashCoreComponents') +install.packages('dashHtmlComponents') +install.packages('tidyverse') +install.packages('plotly') +install.packages('ggplot2') +install.packages('dplyr') diff --git a/src/linear_regression.R b/src/linear_regression.R index 52aeef9..551f461 100644 --- a/src/linear_regression.R +++ b/src/linear_regression.R @@ -32,7 +32,7 @@ main <- function(url_to_read){ theme_minimal(20) + xlab("GDP for year ($)") + ylab("Number of Suicides") + - scale_x_continuous(labels = scales::comma_format()) + scale_x_continuous(labels = scales::comma_format()) + ggsave(here("images", "suicides_regression.png"), width = 15, height = 10, device="png") suicides_regression