diff --git a/demo-2/README.md b/demo-2/README.md
new file mode 100644
index 0000000..7317419
--- /dev/null
+++ b/demo-2/README.md
@@ -0,0 +1,2 @@
+# Demo-2
+This is a demo of how a shiny app is structure, which uses a simple `TextInput` element to transfer information from the UI to R.
diff --git a/demo-2/server.R b/demo-2/server.R
new file mode 100644
index 0000000..148eecc
--- /dev/null
+++ b/demo-2/server.R
@@ -0,0 +1,9 @@
+### Use input to create a string
+shinyServer(function(input, output) {
+
+ # You can access the value of the widget with input$select, e.g.
+ output$userText <- renderText({
+ return(paste0('The user typed: ', input$text))
+ })
+
+})
\ No newline at end of file
diff --git a/demo-2/ui.R b/demo-2/ui.R
new file mode 100644
index 0000000..40d6eb0
--- /dev/null
+++ b/demo-2/ui.R
@@ -0,0 +1,10 @@
+# Demo 2: Simple TextInput element
+shinyUI(fluidPage(
+
+ # Create a text input element
+ textInput("text", label = h3("Text input"), value = "Enter text..."),
+
+ # Show output$userText
+ textOutput('userText')
+
+))
\ No newline at end of file
diff --git a/demo-3/README.md b/demo-3/README.md
new file mode 100644
index 0000000..abeb8bc
--- /dev/null
+++ b/demo-3/README.md
@@ -0,0 +1,2 @@
+# Demo-3
+This is a demo of how a shiny app is structure, which uses a simple `RadioInput` element to transfer information from the UI to R.
diff --git a/demo-3/server.R b/demo-3/server.R
new file mode 100644
index 0000000..791f064
--- /dev/null
+++ b/demo-3/server.R
@@ -0,0 +1,11 @@
+### Use input to create a string
+
+### Use input to create a histogram
+shinyServer(function(input, output) {
+
+ # Reder a histogram of a given color
+ output$histogram <- renderPlot({
+ x <- rnorm(1000)
+ return(hist(x, col = input$color))
+ })
+})
\ No newline at end of file
diff --git a/demo-3/ui.R b/demo-3/ui.R
new file mode 100644
index 0000000..bcfa018
--- /dev/null
+++ b/demo-3/ui.R
@@ -0,0 +1,9 @@
+# Demo 3: Simple RadioInput element
+shinyUI(fluidPage(
+
+ # Radio buttons
+ radioButtons("color", label = "Color",
+ choices = list("Green" = 'green', "Blue" = 'blue'),
+ selected = 'green'),
+ plotOutput('histogram')
+))
\ No newline at end of file
diff --git a/exercise-1/exercise.R b/exercise-1/exercise.R
index 9f25f34..ff6244e 100644
--- a/exercise-1/exercise.R
+++ b/exercise-1/exercise.R
@@ -1,11 +1,12 @@
# Exercise 1: Loading functions
# Set your directory
-setwd('~/Documents/info-201/m14-shiny/exercise-1/')
+setwd('~/Documents/INFO201/m14-shiny/exercise-1/')
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
# Load your buildMap.R Script
-
+source('./scripts/buildMap.R')
# Use your BuildMap function to draw a map of the data
+BuildMap(df)
diff --git a/exercise-1/scripts/buildMap.R b/exercise-1/scripts/buildMap.R
index 85c7bf0..f2f6786 100644
--- a/exercise-1/scripts/buildMap.R
+++ b/exercise-1/scripts/buildMap.R
@@ -7,10 +7,30 @@ library(plotly)
# Try parameterize a few options, such as the title
# I suggest: https://plot.ly/r/bubble-maps/
BuildMap <- function(data) {
+ library(plotly)
+ df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
+ df$q <- with(df, cut(pop, quantile(pop)))
+ levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile")
+ df$q <- as.ordered(df$q)
+ g <- list(
+ scope = 'usa',
+ projection = list(type = 'albers usa'),
+ showland = TRUE,
+ landcolor = toRGB("gray85"),
+ subunitwidth = 1,
+ countrywidth = 1,
+ subunitcolor = toRGB("white"),
+ countrycolor = toRGB("white")
+ )
-
-
+ p <- plot_geo(df, locationmode = 'USA-states', sizes = c(1, 250)) %>%
+ add_markers(
+ x = ~lon, y = ~lat, size = ~pop, color = ~q, hoverinfo = "text",
+ text = ~paste(df$name, "
", df$pop/1e6, " million")
+ ) %>%
+ layout(title = '2014 US city populations
(Click legend to toggle)', geo = g)
+ return (p)
}
diff --git a/exercise-3/README.md b/exercise-3/README.md
new file mode 100644
index 0000000..abbe60f
--- /dev/null
+++ b/exercise-3/README.md
@@ -0,0 +1,16 @@
+# Exercise-3
+In this exercise, you'll practice building an interactive Plotly map in a Shiny application.
+
+As in previous exercises, you should fork and clone this repository, then follow the instructions below.
+
+## server.R
+Your `server.R` file already loads the data you need, as well as scripts for building a map. Inside your `shinyServer`, you should do the following:
+
+- Replace the static input `'population'` with a dynamic value that comes from your UI
+
+## ui.R
+Your `ui.R` file already has a `tabPanel` built displaying your map. In this section, you should add another `tabPanel` for your scatter-plot by doing the following:
+
+- Add a `selectInput` (with a proper id) that allows you to select a variable to map (`population`, `votes`, or `ratio`)
+
+Also, make sure you look at the `BuildMap` file and **understand how it is using variables to make a dynamic application**.
diff --git a/exercise-3/data/electoral_college.csv b/exercise-3/data/electoral_college.csv
new file mode 100644
index 0000000..40419b8
--- /dev/null
+++ b/exercise-3/data/electoral_college.csv
@@ -0,0 +1 @@
+state,votes,population
Alabama,9,4858979
Alaska,3,738432
Arizona,11,6828065
Arkansas,6,2978204
California,55,39144818
Colorado,9,5456574
Connecticut,7,3590886
Delaware,3,945934
Florida,29,20271272
Georgia,16,10214860
Hawaii,4,1431603
Idaho,4,1654930
Illinois,20,12859995
Indiana,11,6619680
Iowa,6,3123899
Kansas,6,2911641
Kentucky,8,4425092
Louisiana,8,4670724
Maine,4,1329328
Maryland,10,6006401
Massachusetts,11,6794422
Michigan,16,9922576
Minnesota,10,5489594
Mississippi,6,2992333
Missouri,10,6083672
Montana,3,1032949
Nebraska,5,1896190
Nevada,6,2890845
New Hampshire,4,1330608
New Jersey,14,8958013
New Mexico,5,2085109
New York,29,19795791
North Carolina,15,10042802
North Dakota,3,756927
Ohio,18,11613423
Oklahoma,7,3911338
Oregon,7,4028977
Pennsylvania,20,12802503
Rhode Island,4,1056298
South Carolina,9,4896146
South Dakota,3,858469
Tennessee,11,6600299
Texas,38,27469114
Utah,6,2995919
Vermont,3,626042
Virginia,13,8382993
Washington,12,7170351
District of Columbia,3,672228
West Virginia,5,1844128
Wisconsin,10,5771337
Wyoming,3,586107
\ No newline at end of file
diff --git a/exercise-3/data/state_codes.csv b/exercise-3/data/state_codes.csv
new file mode 100644
index 0000000..4c51756
--- /dev/null
+++ b/exercise-3/data/state_codes.csv
@@ -0,0 +1 @@
+code,state
AL,Alabama
AK,Alaska
AZ,Arizona
AR,Arkansas
CA,California
CO,Colorado
CT,Connecticut
DE,Delaware
FL,Florida
GA,Georgia
HI,Hawaii
ID,Idaho
IL,Illinois
IN,Indiana
IA,Iowa
KS,Kansas
KY,Kentucky
LA,Louisiana
ME,Maine
MD,Maryland
MA,Massachusetts
MI,Michigan
MN,Minnesota
MS,Mississippi
MO,Missouri
MT,Montana
NE,Nebraska
NV,Nevada
NH,New Hampshire
NJ,New Jersey
NM,New Mexico
NY,New York
NC,North Carolina
ND,North Dakota
OH,Ohio
OK,Oklahoma
OR,Oregon
PA,Pennsylvania
RI,Rhode Island
SC,South Carolina
SD,South Dakota
TN,Tennessee
TX,Texas
UT,Utah
VT,Vermont
VA,Virginia
WA,Washington
WV,West Virginia
WI,Wisconsin
WY,Wyoming
\ No newline at end of file
diff --git a/exercise-3/scripts/buildMap.R b/exercise-3/scripts/buildMap.R
new file mode 100644
index 0000000..3bcc562
--- /dev/null
+++ b/exercise-3/scripts/buildMap.R
@@ -0,0 +1,35 @@
+# BuildMap file: function that returns a plotly map
+library(plotly)
+library(stringr)
+
+# BuildMap function: fill this in with a function that returns a map:
+# Derived from: https://plot.ly/r/choropleth-maps/
+
+BuildMap <- function(data, map.var) {
+ # give state boundaries a white border
+ l <- list(color = toRGB("white"), width = 2)
+
+ # specify some map projection/options
+ g <- list(
+ scope = 'usa',
+ projection = list(type = 'albers usa'),
+ showlakes = TRUE,
+ lakecolor = toRGB('white')
+ )
+
+ # Make equation for map color / text
+ var.equation <- paste0('~', map.var)
+
+ # Plot
+ p <- plot_geo(data, locationmode = 'USA-states') %>%
+ add_trace(
+ z = eval(parse(text = var.equation)), text = ~state, locations = ~code,
+ color = eval(parse(text = var.equation)), colors = 'Purples'
+ ) %>%
+ colorbar(title = "Color Title") %>%
+ layout(
+ title = str_to_title(map.var),
+ geo = g
+ )
+ return(p)
+}
diff --git a/exercise-3/server.R b/exercise-3/server.R
new file mode 100644
index 0000000..a435e5d
--- /dev/null
+++ b/exercise-3/server.R
@@ -0,0 +1,23 @@
+# server.R
+library(dplyr)
+
+# Read in data
+setwd('~/Documents/info-201/m14-shiny/exercise-3/')
+source('./scripts/buildMap.R')
+df <- read.csv('./data/electoral_college.csv', stringsAsFactors = FALSE)
+state.codes <- read.csv('./data/state_codes.csv', stringsAsFactors = FALSE)
+
+# Join together state.codes and df
+joined.data <- left_join(df, state.codes, by="state")
+
+# Compute the electoral votes per 100K people in each state
+joined.data <- joined.data %>% mutate(ratio = votes/population * 100000)
+
+# Start shinyServer
+shinyServer(function(input, output) {
+
+ # Render a plotly object that returns your map
+ output$map <- renderPlotly({
+ return(BuildMap(joined.data, 'population'))
+ })
+})
\ No newline at end of file
diff --git a/exercise-3/ui.R b/exercise-3/ui.R
new file mode 100644
index 0000000..e6435f8
--- /dev/null
+++ b/exercise-3/ui.R
@@ -0,0 +1,9 @@
+# ui.R
+shinyUI(fluidPage(
+ mainPanel(
+ # Add a selectInput (with a proper id) that allows you to select a variable to map
+
+ # Use plotlyOutput to show your map
+ plotlyOutput('map')
+ )
+))
\ No newline at end of file
diff --git a/exercise-4/README.md b/exercise-4/README.md
new file mode 100644
index 0000000..d74c267
--- /dev/null
+++ b/exercise-4/README.md
@@ -0,0 +1,24 @@
+# Exercise-4
+In this exercise, you'll practice building an a multi-tab Shiny application. The final product should look like this:
+
+
+
+The Map panel is already built for you to model. Your scatter panel should enable users to search for a state in the scatterplot.
+
+As in previous exercises, you should fork and clone this repository, then follow the instructions below.
+
+## server.R
+Your `server.R` file already loads the data you need, as well as scripts for building a map and a scatter plot. Inside your `shinyServer`, you should do the following:
+
+- Create a `scatter` property on your `output` object. That property should be a `renderPlotly` object that returns a scatterplot (`BuildScatter`)
+- Make sure to pass your data and search string (i.e., `input$search`) to your `BuildScatter` function.
+
+## ui.R
+Your `ui.R` file already has a `tabPanel` built displaying your map. In this section, you should add another `tabPanel` for your scatter-plot by doing the following:
+
+- Create a `tabPanel` to show your scatter plot
+- Add a `titlePanel` to your tab
+- Create a `sidebarLayout` for this tab (page)
+- Create a `sidebarPanel` for your controls
+- In your `sidebarPanel`, make a `textInput` widget for searching for a state in your scatter plot
+- Create a `mainPanel`, in which you should display your plotly Scatter plot
diff --git a/exercise-4/data/electoral_college.csv b/exercise-4/data/electoral_college.csv
new file mode 100644
index 0000000..40419b8
--- /dev/null
+++ b/exercise-4/data/electoral_college.csv
@@ -0,0 +1 @@
+state,votes,population
Alabama,9,4858979
Alaska,3,738432
Arizona,11,6828065
Arkansas,6,2978204
California,55,39144818
Colorado,9,5456574
Connecticut,7,3590886
Delaware,3,945934
Florida,29,20271272
Georgia,16,10214860
Hawaii,4,1431603
Idaho,4,1654930
Illinois,20,12859995
Indiana,11,6619680
Iowa,6,3123899
Kansas,6,2911641
Kentucky,8,4425092
Louisiana,8,4670724
Maine,4,1329328
Maryland,10,6006401
Massachusetts,11,6794422
Michigan,16,9922576
Minnesota,10,5489594
Mississippi,6,2992333
Missouri,10,6083672
Montana,3,1032949
Nebraska,5,1896190
Nevada,6,2890845
New Hampshire,4,1330608
New Jersey,14,8958013
New Mexico,5,2085109
New York,29,19795791
North Carolina,15,10042802
North Dakota,3,756927
Ohio,18,11613423
Oklahoma,7,3911338
Oregon,7,4028977
Pennsylvania,20,12802503
Rhode Island,4,1056298
South Carolina,9,4896146
South Dakota,3,858469
Tennessee,11,6600299
Texas,38,27469114
Utah,6,2995919
Vermont,3,626042
Virginia,13,8382993
Washington,12,7170351
District of Columbia,3,672228
West Virginia,5,1844128
Wisconsin,10,5771337
Wyoming,3,586107
\ No newline at end of file
diff --git a/exercise-4/data/state_codes.csv b/exercise-4/data/state_codes.csv
new file mode 100644
index 0000000..4c51756
--- /dev/null
+++ b/exercise-4/data/state_codes.csv
@@ -0,0 +1 @@
+code,state
AL,Alabama
AK,Alaska
AZ,Arizona
AR,Arkansas
CA,California
CO,Colorado
CT,Connecticut
DE,Delaware
FL,Florida
GA,Georgia
HI,Hawaii
ID,Idaho
IL,Illinois
IN,Indiana
IA,Iowa
KS,Kansas
KY,Kentucky
LA,Louisiana
ME,Maine
MD,Maryland
MA,Massachusetts
MI,Michigan
MN,Minnesota
MS,Mississippi
MO,Missouri
MT,Montana
NE,Nebraska
NV,Nevada
NH,New Hampshire
NJ,New Jersey
NM,New Mexico
NY,New York
NC,North Carolina
ND,North Dakota
OH,Ohio
OK,Oklahoma
OR,Oregon
PA,Pennsylvania
RI,Rhode Island
SC,South Carolina
SD,South Dakota
TN,Tennessee
TX,Texas
UT,Utah
VT,Vermont
VA,Virginia
WA,Washington
WV,West Virginia
WI,Wisconsin
WY,Wyoming
\ No newline at end of file
diff --git a/exercise-4/imgs/scatter.png b/exercise-4/imgs/scatter.png
new file mode 100644
index 0000000..fafdf56
Binary files /dev/null and b/exercise-4/imgs/scatter.png differ
diff --git a/exercise-4/scripts/buildMap.R b/exercise-4/scripts/buildMap.R
new file mode 100644
index 0000000..3bcc562
--- /dev/null
+++ b/exercise-4/scripts/buildMap.R
@@ -0,0 +1,35 @@
+# BuildMap file: function that returns a plotly map
+library(plotly)
+library(stringr)
+
+# BuildMap function: fill this in with a function that returns a map:
+# Derived from: https://plot.ly/r/choropleth-maps/
+
+BuildMap <- function(data, map.var) {
+ # give state boundaries a white border
+ l <- list(color = toRGB("white"), width = 2)
+
+ # specify some map projection/options
+ g <- list(
+ scope = 'usa',
+ projection = list(type = 'albers usa'),
+ showlakes = TRUE,
+ lakecolor = toRGB('white')
+ )
+
+ # Make equation for map color / text
+ var.equation <- paste0('~', map.var)
+
+ # Plot
+ p <- plot_geo(data, locationmode = 'USA-states') %>%
+ add_trace(
+ z = eval(parse(text = var.equation)), text = ~state, locations = ~code,
+ color = eval(parse(text = var.equation)), colors = 'Purples'
+ ) %>%
+ colorbar(title = "Color Title") %>%
+ layout(
+ title = str_to_title(map.var),
+ geo = g
+ )
+ return(p)
+}
diff --git a/exercise-4/scripts/buildScatter.R b/exercise-4/scripts/buildScatter.R
new file mode 100644
index 0000000..c532f3b
--- /dev/null
+++ b/exercise-4/scripts/buildScatter.R
@@ -0,0 +1,27 @@
+# BuildMap file: function that returns a plotly map
+library(plotly)
+library(stringr)
+### Build Scatter ###
+BuildScatter <- function(data, search = '', xvar = 'population', yvar = 'votes') {
+ # Filter down to state, then graph
+
+ # Get x and y max
+ xmax <- max(data[,xvar]) * 1.5
+ ymax <- max(data[,yvar]) * 1.5
+ x.equation <- paste0('~', xvar)
+ y.equation <- paste0('~', yvar)
+ data <- data %>%
+ filter(grepl(search, state))
+
+ plot_ly(data=data, x = eval(parse(text = x.equation)),
+ y = eval(parse(text = y.equation)),
+ mode='markers',
+ marker = list(
+ opacity = .4,
+ size = 10
+ )) %>%
+ layout(xaxis = list(range = c(0, xmax), title = xvar),
+ yaxis = list(range = c(0, ymax), title = yvar)
+ ) %>%
+ return()
+}
\ No newline at end of file
diff --git a/exercise-4/server.R b/exercise-4/server.R
new file mode 100644
index 0000000..7b8c863
--- /dev/null
+++ b/exercise-4/server.R
@@ -0,0 +1,32 @@
+# server.R
+library(dplyr)
+library(plotly)
+library(shiny)
+
+# Read in data
+# setwd('~/Documents/info-201/m14-shiny/exercise-4/')
+source('./scripts/buildMap.R')
+source('./scripts/buildScatter.R')
+df <- read.csv('./data/electoral_college.csv', stringsAsFactors = FALSE)
+state.codes <- read.csv('./data/state_codes.csv', stringsAsFactors = FALSE)
+
+# Join together state.codes and df
+joined.data <- left_join(df, state.codes, by="state")
+
+# Compute the electoral votes per 100K people in each state
+joined.data <- joined.data %>% mutate(ratio = votes/population * 100000)
+
+# Start shinyServer
+shinyServer(function(input, output) {
+
+ # Render a plotly object that returns your map
+ output$map <- renderPlotly({
+ return(BuildMap(joined.data, input$mapvar))
+ })
+
+ # Create a `scatter` property on your `output` object. That property shoudl be a `renderPlotly` object that returns a scatterplot (`BuildScatter`)
+ output$scatter <- renderPlotly({
+ return(BuildScatter(joined.data, input$text))
+ })
+
+})
\ No newline at end of file
diff --git a/exercise-4/ui.R b/exercise-4/ui.R
new file mode 100644
index 0000000..d7c066d
--- /dev/null
+++ b/exercise-4/ui.R
@@ -0,0 +1,48 @@
+# ui.R
+library(shiny)
+library(plotly)
+shinyUI(navbarPage('Electoral College',
+ # Create a tab panel for your map
+ tabPanel('Map',
+ titlePanel('Electoral College Votes'),
+ # Create sidebar layout
+ sidebarLayout(
+
+ # Side panel for controls
+ sidebarPanel(
+
+ # Input to select variable to map
+ selectInput('mapvar', label = 'Variable to Map', choices = list("Population" = 'population', 'Electoral Votes' = 'votes', 'Votes / Population' = 'ratio'))
+ ),
+
+ # Main panel: display plotly map
+ mainPanel(
+ plotlyOutput('map')
+ )
+ )
+ ),
+
+ # Create a tabPanel to show your scatter plot
+ tabPanel('Scatter',
+ # Add a titlePanel to your tab
+ titlePanel("Electoral College Votes"),
+
+ # Create a sidebar layout for this tab (page)
+ sidebarLayout(
+
+ # Create a sidebarPanel for your controls
+ sidebarPanel(
+
+ # Make a textInput widget for searching for a state in your scatter plot
+ textInput('text', label = "State to Map", value = "")
+
+ ),
+
+ # Create a main panel, in which you should display your plotly Scatter plot
+ mainPanel(
+ plotlyOutput('scatter')
+ )
+
+ )
+ )
+))