From 8672b44526b443423b41a23efec812aef2d52ce1 Mon Sep 17 00:00:00 2001 From: Asma Al-Odaini Date: Fri, 29 Jan 2021 15:34:27 -0500 Subject: [PATCH] Remove test files --- app.R | 365 +++++++++++++++++++++++++++++++++++++++++++----- data/msleep.csv | 84 ----------- 2 files changed, 327 insertions(+), 122 deletions(-) delete mode 100644 data/msleep.csv diff --git a/app.R b/app.R index ac44d94..c8a753d 100644 --- a/app.R +++ b/app.R @@ -1,55 +1,344 @@ +library(tidyverse) +library(purrr) library(dash) library(dashCoreComponents) library(dashHtmlComponents) library(dashBootstrapComponents) -library(ggplot2) library(plotly) +#library(here) +#library(dashTable) -app <- Dash$new(external_stylesheets = dbcThemes$BOOTSTRAP) - -msleep2 <- readr::read_csv(here::here('data', 'msleep.csv')) - -app$layout( - dbcContainer( - list( - htmlH1('Dashr heroky deployment'), - dccGraph(id='plot-area'), - htmlDiv(id='output-area'), - htmlBr(), - htmlDiv(id='output-area2'), - htmlBr(), - dccDropdown( - id='col-select', - options = msleep2 %>% colnames %>% purrr::map(function(col) list(label = col, value = col)), - value='bodywt') +movies <- readr::read_csv(here::here("data/processed/movies.csv")) + +app <- Dash$new(external_stylesheets = dbcThemes$LUMEN) + +SIDEBAR_STYLE = list( + "position" = "fixed", + "top" = 0, + "left" = 0, + "bottom" = 0, + "width" = "20rem", + "padding" = "2rem 1rem", + "z-index" = 4000000 +) + +CONTENT_STYLE = list( + "margin-left" = "20rem", + "margin-right" = "2rem", + "padding" = "2rem 1rem", + "z-index" = -1 +) + +cards = dbcCol(list( + dbcCard(dbcCardBody(list( + htmlH6("Average box office value", className = 'card-title'), + htmlH4(id = "average-revenue", className = 'card-text') + )), + color = "primary", + outline = TRUE), + dbcCard(dbcCardBody(list( + htmlH6("Average voting", className = 'card-title'), + htmlH4(id = "average-vote", className = 'card-text') + )), + color = "primary", + outline = TRUE) +)) + +genre_graphs = htmlDiv(list(dbcRow(list( + dbcCol(dbcCard( + list(dbcCardHeader(htmlH4(id = "vote-plot-title")), + dbcCardBody(dccGraph( + id = "vote-plot", + style = list( + "border-width" = "0", + "width" = "100%", + "height" = 265 + ) + ))), + color = "success", + outline = TRUE + )), + dbcCol(dbcCard( + list(dbcCardHeader(htmlH4(id = "revenue-plot-title")), + dbcCardBody( + dccGraph( + id = "revenue-plot", + style = list( + "border-width" = "0", + "width" = "100%", + "height" = 265 + ) + ) + )), + color = "success", + outline = TRUE + )) +)))) + +studio_graphs = htmlDiv(list(dbcRow(list(dbcCol( + dbcCard( + list(dbcCardHeader(htmlH4(id = "vote-scatter-title")), + dbcCardBody( + dccGraph( + id = "vote-scatter-plot", + style = list( + "border-width" = "0", + "width" = "100%", + "height" = "400px" + ) + ) + )), + color = "info", + outline = TRUE + ) +))), +htmlBr(), +htmlBr(), +dbcRow(list(dbcCol( + dbcCard( + list(dbcCardHeader(htmlH4(id = "top-movies-title")), + dbcCardBody(dccGraph( + id = "top_movies", + style = list( + "border-width" = "0", + "width" = "100%", + "height" = "400px" + ) + ))) + , + color = "info", + outline = TRUE + ) +))))) + +content = htmlDiv( + list( + cards, + htmlBr(), + htmlBr(), + genre_graphs, + htmlBr(), + htmlBr(), + studio_graphs + ), + id = "page-content", + style = CONTENT_STYLE +) + +controls = dbcCard(list(dbcFormGroup(list( + dbcLabel("Genre"), + dccDropdown( + id = "xgenre-widget", + options = purrr::map(movies$genres %>% unique(), function(col) + list(label = col, value = col)), + value = "Horror", + clearable = FALSE + ) +)), +dbcFormGroup(list( + dbcLabel("Budget Range (US$ mil)"), + dccRangeSlider( + id = "xbudget-widget", + min = 10, + max = 300, + value = list(10, 300), + marks = list( + "10" = "10", + "100" = "100", + "200" = "200", + "300" = "300" ) ) +))), +body = TRUE, +className = "text-dark") + +sidebar = htmlDiv( + list( + htmlH2("Movie Selection", className = "display-4"), + htmlHr(), + controls, + htmlHr(), + htmlP( + "A data visualization app that allows decision makers in the streaming companies to explore a dataset of movies to determine the popular movies that they need to provide to their users", + className = "lead" + ) + ), + style = SIDEBAR_STYLE, + className = 'bg-primary text-white' ) +app$layout(htmlDiv(list(sidebar, content))) + +##### CALLBACK ##### + + +# Voting average by studio ------------------------------------------------ app$callback( - output('plot-area', 'figure'), - list(input('col-select', 'value')), - function(xcol) { - p <- ggplot(msleep2) + - aes(x = !!sym(xcol), - y = sleep_total, - color = vore, - text = name) + - geom_point() + - scale_x_log10() + - ggthemes::scale_color_tableau() - ggplotly(p) %>% layout(dragmode = 'select') - } + output('vote-plot', 'figure'), + list(input("xgenre-widget", "value"), + input("xbudget-widget", "value")), + function(xgenre, xbudget){ + filtered_movies <- movies %>% + filter(genres == xgenre, + budget %in% (unlist(xbudget)[1]:unlist(xbudget)[2])) + + vote_avg_by_studio <- filtered_movies %>% + ggplot(aes(x = as.factor(studios), y = vote_average)) + + geom_boxplot(fill="#20B2AA", color = 'turquoise4') + + labs(y = 'Vote Average') + + coord_flip() + + theme(axis.title.y = element_blank(), + axis.title.x = element_text(face = 'bold', color = 'turquoise4'), + axis.text = element_text(color = 'turquoise4')) + + ggplotly(vote_avg_by_studio + aes(text = title), tooltip = 'text') + + } + ) + +# Financials by studio ---------------------------------------------------- + app$callback( - list(output('output-area', 'children'), - output('output-area2', 'children')), - list(input('plot-area', 'selectedData'), - input('plot-area', 'hoverData')), - function(selected_data, hover_data) { - list(toString(selected_data), toString(hover_data)) - } + output('revenue-plot', 'figure'), + list(input("xgenre-widget", "value"), + input("xbudget-widget", "value")), + function(xgenre, xbudget){ + filtered_movies <- movies %>% + filter(genres == xgenre, + budget %in% (unlist(xbudget)[1]:unlist(xbudget)[2])) + + rev_by_studio <- filtered_movies %>% + ggplot(aes(x = as.factor(studios), y = revenue)) + + geom_boxplot(fill="#20B2AA", color = 'turquoise4') + + labs(y = 'Revenue (US$ mil)') + + coord_flip() + + theme(axis.title.y = element_blank(), + axis.title.x = element_text(face = 'bold', color = 'turquoise4'), + axis.text = element_text(color = 'turquoise4')) + + ggplotly(rev_by_studio + aes(text = title), tooltip = 'text') + + } + ) + +# Voting profile scattered plot ------------------------------------------- + +app$callback( + output('vote-scatter-plot', 'figure'), + list(input("xgenre-widget", "value"), + input("xbudget-widget", "value")), + function(xgenre, xbudget){ + filtered_movies <- movies %>% + filter(genres == xgenre, + budget %in% (unlist(xbudget)[1]:unlist(xbudget)[2])) + + voting_profile <- filtered_movies %>% + ggplot(aes(vote_average, vote_count)) + + geom_jitter(color = "#20B2AA", color = 'turquoise4', alpha = 0.6) + + labs(x = 'Vote Average', y = "Vote Count") + + theme( + axis.title = element_text(face = 'bold', color = 'turquoise4'), + axis.text = element_text(color = 'turquoise4')) + + ggplotly(voting_profile + aes(text = title), tooltip = 'text') + + } + +) + + +# Top film table/bar-chart ---------------------------------------------------------- + +app$callback( + output('top_movies', 'figure'), + list(input("xgenre-widget", "value"), + input("xbudget-widget", "value")), + function(xgenre, xbudget){ + filtered_movies <- movies %>% + filter(genres == xgenre, + budget %in% (unlist(xbudget)[1]:unlist(xbudget)[2])) + + top_movies_by_vote <- filtered_movies %>% + group_by(title) %>% + summarize(vote_average = mean(vote_average), + runtime = mean(runtime)) %>% + arrange(desc(vote_average)) %>% + slice(1:10) %>% + ggplot(aes(x= vote_average, y = reorder(title, vote_average), color = runtime)) + + geom_point(stat = 'identity', shape = 2, size = 5, stroke = 1) + + labs(x = "Vote Average", legend = "Runtime (mins)")+ + xlim(3.25, 4.5)+ + scale_color_gradient(low = "cadetblue1", high ="#20B2AA")+ + theme(axis.title.y = element_blank(), + axis.title = element_text(face = 'bold', color = 'turquoise4'), + axis.text = element_text(color = 'turquoise4')) + + ggplotly(top_movies_by_vote + aes(text = paste("Runtime:", runtime, "\nVote avg", vote_average)), tooltip = 'text') + + + } + +) + + + +# Avg. box office card ---------------------------------------------------- + +app$callback( + output('average-revenue', 'children'), + list(input("xgenre-widget", "value"), + input("xbudget-widget", "value")), + function(xgenre, xbudget) { + filtered_movies <- movies %>% + filter(genres == xgenre, + budget %in% (unlist(xbudget)[1]:unlist(xbudget)[2])) + average_revenue <- paste("US$", round(mean(filtered_movies$revenue),2),"mil") + return(average_revenue) + } +) + + +# Avg. voting card -------------------------------------------------------- + +app$callback( + output('average-vote', 'children'), + list(input("xgenre-widget", "value"), + input("xbudget-widget", "value")), + function(xgenre, xbudget) { + filtered_movies <- movies %>% + filter(genres == xgenre, + budget %in% (unlist(xbudget)[1]:unlist(xbudget)[2])) + average_vote <- round(mean(filtered_movies$vote_average),2) + return(average_vote) + } +) + + +# Chart Titles ------------------------------------------------------------ + +app$callback( + list(output("vote-plot-title", 'children'), + output("revenue-plot-title", 'children'), + output("vote-scatter-title", 'children'), + output("top-movies-title", 'children')), + list(input("xgenre-widget", "value"), + input("xbudget-widget", "value")), + function(xgenre, xbudget) { + return(list( + paste(xgenre,'Movies Vote Average By Studio'), + paste(xgenre,'Movies Financials By Studio'), + paste('Voting Profile For', xgenre, 'Movies'), + paste('Most Popular', xgenre,'Movies (By Vote Average)') + )) + } +) + + + + app$run_server(host = '0.0.0.0') \ No newline at end of file diff --git a/data/msleep.csv b/data/msleep.csv deleted file mode 100644 index 8446a38..0000000 --- a/data/msleep.csv +++ /dev/null @@ -1,84 +0,0 @@ -name,genus,vore,order,conservation,sleep_total,sleep_rem,sleep_cycle,awake,brainwt,bodywt -Cheetah,Acinonyx,carni,Carnivora,lc,12.1,NA,NA,11.9,NA,50 -Owl monkey,Aotus,omni,Primates,NA,17,1.8,NA,7,0.0155,0.48 -Mountain beaver,Aplodontia,herbi,Rodentia,nt,14.4,2.4,NA,9.6,NA,1.35 -Greater short-tailed shrew,Blarina,omni,Soricomorpha,lc,14.9,2.3,0.133333333,9.1,2.9e-4,0.019 -Cow,Bos,herbi,Artiodactyla,domesticated,4,0.7,0.666666667,20,0.423,600 -Three-toed sloth,Bradypus,herbi,Pilosa,NA,14.4,2.2,0.766666667,9.6,NA,3.85 -Northern fur seal,Callorhinus,carni,Carnivora,vu,8.7,1.4,0.383333333,15.3,NA,20.49 -Vesper mouse,Calomys,NA,Rodentia,NA,7,NA,NA,17,NA,0.045 -Dog,Canis,carni,Carnivora,domesticated,10.1,2.9,0.333333333,13.9,0.07,14 -Roe deer,Capreolus,herbi,Artiodactyla,lc,3,NA,NA,21,0.0982,14.8 -Goat,Capri,herbi,Artiodactyla,lc,5.3,0.6,NA,18.7,0.115,33.5 -Guinea pig,Cavis,herbi,Rodentia,domesticated,9.4,0.8,0.216666667,14.6,0.0055,0.728 -Grivet,Cercopithecus,omni,Primates,lc,10,0.7,NA,14,NA,4.75 -Chinchilla,Chinchilla,herbi,Rodentia,domesticated,12.5,1.5,0.116666667,11.5,0.0064,0.42 -Star-nosed mole,Condylura,omni,Soricomorpha,lc,10.3,2.2,NA,13.7,0.001,0.06 -African giant pouched rat,Cricetomys,omni,Rodentia,NA,8.3,2,NA,15.7,0.0066,1 -Lesser short-tailed shrew,Cryptotis,omni,Soricomorpha,lc,9.1,1.4,0.15,14.9,1.4e-4,0.005 -Long-nosed armadillo,Dasypus,carni,Cingulata,lc,17.4,3.1,0.383333333,6.6,0.0108,3.5 -Tree hyrax,Dendrohyrax,herbi,Hyracoidea,lc,5.3,0.5,NA,18.7,0.0123,2.95 -North American Opossum,Didelphis,omni,Didelphimorphia,lc,18,4.9,0.333333333,6,0.0063,1.7 -Asian elephant,Elephas,herbi,Proboscidea,en,3.9,NA,NA,20.1,4.603,2547 -Big brown bat,Eptesicus,insecti,Chiroptera,lc,19.7,3.9,0.116666667,4.3,3e-4,0.023 -Horse,Equus,herbi,Perissodactyla,domesticated,2.9,0.6,1,21.1,0.655,521 -Donkey,Equus,herbi,Perissodactyla,domesticated,3.1,0.4,NA,20.9,0.419,187 -European hedgehog,Erinaceus,omni,Erinaceomorpha,lc,10.1,3.5,0.283333333,13.9,0.0035,0.77 -Patas monkey,Erythrocebus,omni,Primates,lc,10.9,1.1,NA,13.1,0.115,10 -Western american chipmunk,Eutamias,herbi,Rodentia,NA,14.9,NA,NA,9.1,NA,0.071 -Domestic cat,Felis,carni,Carnivora,domesticated,12.5,3.2,0.416666667,11.5,0.0256,3.3 -Galago,Galago,omni,Primates,NA,9.8,1.1,0.55,14.2,0.005,0.2 -Giraffe,Giraffa,herbi,Artiodactyla,cd,1.9,0.4,NA,22.1,NA,899.995 -Pilot whale,Globicephalus,carni,Cetacea,cd,2.7,0.1,NA,21.35,NA,800 -Gray seal,Haliochoerus,carni,Carnivora,lc,6.2,1.5,NA,17.8,0.325,85 -Gray hyrax,Heterohyrax,herbi,Hyracoidea,lc,6.3,0.6,NA,17.7,0.01227,2.625 -Human,Homo,omni,Primates,NA,8,1.9,1.5,16,1.32,62 -Mongoose lemur,Lemur,herbi,Primates,vu,9.5,0.9,NA,14.5,NA,1.67 -African elephant,Loxodonta,herbi,Proboscidea,vu,3.3,NA,NA,20.7,5.712,6654 -Thick-tailed opposum,Lutreolina,carni,Didelphimorphia,lc,19.4,6.6,NA,4.6,NA,0.37 -Macaque,Macaca,omni,Primates,NA,10.1,1.2,0.75,13.9,0.179,6.8 -Mongolian gerbil,Meriones,herbi,Rodentia,lc,14.2,1.9,NA,9.8,NA,0.053 -Golden hamster,Mesocricetus,herbi,Rodentia,en,14.3,3.1,0.2,9.7,0.001,0.12 -Vole ,Microtus,herbi,Rodentia,NA,12.8,NA,NA,11.2,NA,0.035 -House mouse,Mus,herbi,Rodentia,nt,12.5,1.4,0.183333333,11.5,4e-4,0.022 -Little brown bat,Myotis,insecti,Chiroptera,NA,19.9,2,0.2,4.1,2.5e-4,0.01 -Round-tailed muskrat,Neofiber,herbi,Rodentia,nt,14.6,NA,NA,9.4,NA,0.266 -Slow loris,Nyctibeus,carni,Primates,NA,11,NA,NA,13,0.0125,1.4 -Degu,Octodon,herbi,Rodentia,lc,7.7,0.9,NA,16.3,NA,0.21 -Northern grasshopper mouse,Onychomys,carni,Rodentia,lc,14.5,NA,NA,9.5,NA,0.028 -Rabbit,Oryctolagus,herbi,Lagomorpha,domesticated,8.4,0.9,0.416666667,15.6,0.0121,2.5 -Sheep,Ovis,herbi,Artiodactyla,domesticated,3.8,0.6,NA,20.2,0.175,55.5 -Chimpanzee,Pan,omni,Primates,NA,9.7,1.4,1.416666667,14.3,0.44,52.2 -Tiger,Panthera,carni,Carnivora,en,15.8,NA,NA,8.2,NA,162.564 -Jaguar,Panthera,carni,Carnivora,nt,10.4,NA,NA,13.6,0.157,100 -Lion,Panthera,carni,Carnivora,vu,13.5,NA,NA,10.5,NA,161.499 -Baboon,Papio,omni,Primates,NA,9.4,1,0.666666667,14.6,0.18,25.235 -Desert hedgehog,Paraechinus,NA,Erinaceomorpha,lc,10.3,2.7,NA,13.7,0.0024,0.55 -Potto,Perodicticus,omni,Primates,lc,11,NA,NA,13,NA,1.1 -Deer mouse,Peromyscus,NA,Rodentia,NA,11.5,NA,NA,12.5,NA,0.021 -Phalanger,Phalanger,NA,Diprotodontia,NA,13.7,1.8,NA,10.3,0.0114,1.62 -Caspian seal,Phoca,carni,Carnivora,vu,3.5,0.4,NA,20.5,NA,86 -Common porpoise,Phocoena,carni,Cetacea,vu,5.6,NA,NA,18.45,NA,53.18 -Potoroo,Potorous,herbi,Diprotodontia,NA,11.1,1.5,NA,12.9,NA,1.1 -Giant armadillo,Priodontes,insecti,Cingulata,en,18.1,6.1,NA,5.9,0.081,60 -Rock hyrax,Procavia,NA,Hyracoidea,lc,5.4,0.5,NA,18.6,0.021,3.6 -Laboratory rat,Rattus,herbi,Rodentia,lc,13,2.4,0.183333333,11,0.0019,0.32 -African striped mouse,Rhabdomys,omni,Rodentia,NA,8.7,NA,NA,15.3,NA,0.044 -Squirrel monkey,Saimiri,omni,Primates,NA,9.6,1.4,NA,14.4,0.02,0.743 -Eastern american mole,Scalopus,insecti,Soricomorpha,lc,8.4,2.1,0.166666667,15.6,0.0012,0.075 -Cotton rat,Sigmodon,herbi,Rodentia,NA,11.3,1.1,0.15,12.7,0.00118,0.148 -Mole rat,Spalax,NA,Rodentia,NA,10.6,2.4,NA,13.4,0.003,0.122 -Arctic ground squirrel,Spermophilus,herbi,Rodentia,lc,16.6,NA,NA,7.4,0.0057,0.92 -Thirteen-lined ground squirrel,Spermophilus,herbi,Rodentia,lc,13.8,3.4,0.216666667,10.2,0.004,0.101 -Golden-mantled ground squirrel,Spermophilus,herbi,Rodentia,lc,15.9,3,NA,8.1,NA,0.205 -Musk shrew,Suncus,NA,Soricomorpha,NA,12.8,2,0.183333333,11.2,3.3e-4,0.048 -Pig,Sus,omni,Artiodactyla,domesticated,9.1,2.4,0.5,14.9,0.18,86.25 -Short-nosed echidna,Tachyglossus,insecti,Monotremata,NA,8.6,NA,NA,15.4,0.025,4.5 -Eastern american chipmunk,Tamias,herbi,Rodentia,NA,15.8,NA,NA,8.2,NA,0.112 -Brazilian tapir,Tapirus,herbi,Perissodactyla,vu,4.4,1,0.9,19.6,0.169,207.501 -Tenrec,Tenrec,omni,Afrosoricida,NA,15.6,2.3,NA,8.4,0.0026,0.9 -Tree shrew,Tupaia,omni,Scandentia,NA,8.9,2.6,0.233333333,15.1,0.0025,0.104 -Bottle-nosed dolphin,Tursiops,carni,Cetacea,NA,5.2,NA,NA,18.8,NA,173.33 -Genet,Genetta,carni,Carnivora,NA,6.3,1.3,NA,17.7,0.0175,2 -Arctic fox,Vulpes,carni,Carnivora,NA,12.5,NA,NA,11.5,0.0445,3.38 -Red fox,Vulpes,carni,Carnivora,NA,9.8,2.4,0.35,14.2,0.0504,4.23