diff --git a/demo-1/server.R b/demo-1/server.R index 0f957b2..0f4c96c 100644 --- a/demo-1/server.R +++ b/demo-1/server.R @@ -19,4 +19,4 @@ shinyServer(function(input, output) { hist(x, breaks = bins, col = 'darkgray', border = 'white') }) -}) \ No newline at end of file +}) diff --git a/demo-1/ui.R b/demo-1/ui.R index 6d146d5..cac687c 100644 --- a/demo-1/ui.R +++ b/demo-1/ui.R @@ -22,4 +22,4 @@ shinyUI(fluidPage( plotOutput("distPlot") ) ) -)) \ No newline at end of file +)) diff --git a/exercise-1/exercise.R b/exercise-1/exercise.R index 66c7fac..048eeaa 100644 --- a/exercise-1/exercise.R +++ b/exercise-1/exercise.R @@ -1,9 +1,11 @@ # Exercise 1: Loading functions # Set your directory - +setwd('~/OneDrive/Documents/INFO 201/m18-shiny/exercise-1') # Source your BuildScatter.r script, exposing your BuildScatter function - +source('scripts/BuildScatter.R') # Use your BuildScatter function to draw a well labeled ggplot scatterplot of the iris data +plot <- scatterplot(iris, 'Sepal.Length', 'Petal.Length', 'Species', 'Iris Dataset', 'Sepal Length', 'Petal Length') +plot diff --git a/exercise-1/scripts/BuildScatter.R b/exercise-1/scripts/BuildScatter.R index 8ce59c6..07dd977 100644 --- a/exercise-1/scripts/BuildScatter.R +++ b/exercise-1/scripts/BuildScatter.R @@ -8,3 +8,11 @@ # - Title of the plot (set a default of "Title") # - Label for the x axis (set a default of "X Title") # - Label for the y axis (set a default of "Y Title") + +scatterplot <- function(df, xaxis, yaxis, color, ptitle = "Title", xlabel = "X Title", ylabel = "Y Title") { + library(ggplot2) + plot <- ggplot(df) + + geom_point(mapping = aes(x = df[, xaxis], y = df[, yaxis], color = df[, color])) + + labs(x = xlabel, y = ylabel, title = ptitle, color = color) + return (plot) +} diff --git a/exercise-3/server.R b/exercise-3/server.R index a435e5d..2e0d1fa 100644 --- a/exercise-3/server.R +++ b/exercise-3/server.R @@ -1,8 +1,10 @@ # server.R library(dplyr) +library(plotly) +library(shiny) # Read in data -setwd('~/Documents/info-201/m14-shiny/exercise-3/') +#setwd('~/OneDrive/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) @@ -18,6 +20,6 @@ shinyServer(function(input, output) { # Render a plotly object that returns your map output$map <- renderPlotly({ - return(BuildMap(joined.data, 'population')) + return(BuildMap(joined.data, input$var)) }) }) \ No newline at end of file diff --git a/exercise-3/ui.R b/exercise-3/ui.R index e6435f8..8b008f7 100644 --- a/exercise-3/ui.R +++ b/exercise-3/ui.R @@ -2,6 +2,8 @@ shinyUI(fluidPage( mainPanel( # Add a selectInput (with a proper id) that allows you to select a variable to map + selectInput('var', label = h3('Variable'), + choices = list('Population' = 'population', 'Votes' = 'votes', 'Ratio' = 'ratio')), # Use plotlyOutput to show your map plotlyOutput('map') diff --git a/exercise-4/server.R b/exercise-4/server.R index 4223979..ead6b27 100644 --- a/exercise-4/server.R +++ b/exercise-4/server.R @@ -2,7 +2,7 @@ library(dplyr) # Read in data -setwd('~/Documents/info-201/m14-shiny/exercise-4/') +#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) @@ -23,5 +23,7 @@ shinyServer(function(input, output) { }) # 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$state)) + }) }) \ No newline at end of file diff --git a/exercise-4/ui.R b/exercise-4/ui.R index 198313f..cfc8aaa 100644 --- a/exercise-4/ui.R +++ b/exercise-4/ui.R @@ -20,22 +20,27 @@ shinyUI(navbarPage('Electoral College', plotlyOutput('map') ) ) - ) + ), # Create a tabPanel to show your scatter plot - + tabPanel('ScatterPlot', # Add a titlePanel to your tab - + titlePanel('State'), # 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('state', label = 'Select State') + ), # Create a main panel, in which you should display your plotly Scatter plot - + mainPanel( + plotlyOutput('scatter') + ) + ) + ) ))