-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
67 changed files
with
10,903 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
## load packages --------------------------------------------------------------- | ||
library(shiny) | ||
library(tidyverse) | ||
|
||
## read data ------------------------------------------------------------------- | ||
countries_data <- read_csv("/Users/gulinan/Desktop/Introduction to Data Science with R/Week_13-14/Week13_Lecture_Notes/source_files/data/countries_1998_2011.csv") | ||
|
||
## subset data | ||
countries_data_2011 <- countries_data %>% | ||
filter(year == 2011) | ||
|
||
|
||
## UI ########################################################################## | ||
ui <- fluidPage( | ||
|
||
sidebarLayout(#position = "right", | ||
|
||
## define inputs in sidebar ------------------------------- | ||
sidebarPanel( | ||
|
||
## select variable for scatter plot x-axis -------------- | ||
## label: widget label. | ||
selectInput(inputId = "x_axis", label = "X axis", | ||
choices = c("human_development_index", "corruption_perception_index", | ||
"population", "life_exp", "gdp_per_capita"), | ||
selected = "human_development_index"), | ||
|
||
## select variable for scatter plot y-axis --------------- | ||
selectInput(inputId = "y_axis", label = "Y axis", | ||
choices = c("human_development_index", "corruption_perception_index", | ||
"population", "life_exp", "gdp_per_capita"), | ||
selected = "corruption_perception_index") | ||
), | ||
|
||
## Show output in main panel ----------------------------------------------- | ||
mainPanel( | ||
## show plot | ||
## outputId:output variable to read the plot from. | ||
plotOutput(outputId = "countries_scatter") | ||
) | ||
) | ||
) | ||
|
||
## SERVER ###################################################################### | ||
server <- function(input, output) { | ||
|
||
## input and output are list-type objects. list elements will be named by me. | ||
## create a scatter plot. | ||
## output is a reserved word. | ||
## countries_scatter is given by me. | ||
## input has two parts: x_axis and y_axis | ||
## renderPlot({expr}): Renders a reactive plot that is suitable for assigning to an output slot. | ||
## expr: An expression that generates a plot. | ||
|
||
output$countries_scatter <- renderPlot({ | ||
#x_axis and y_axis variables will be selected by the user through input widgets. | ||
#input is a reserved word. The x_axis and y_axis names are given by me. | ||
#keep the variables as strings.that's why we are using aes_string(). | ||
ggplot(data = countries_data_2011, | ||
aes_string(x = input$x_axis, y = input$y_axis)) + | ||
#https://stackoverflow.com/questions/63734097/why-do-i-have-to-use-aes-string-with-ggplot-in-shiny | ||
#When you take user input in shiny you get variables as string. | ||
#Hence, by using aes_string you "tell" R/Shiny that I am passing column names | ||
#as string but treat them as columns from the dataframe and not as string. | ||
geom_point(aes_string(color="continent")) + | ||
theme_minimal() | ||
|
||
}) | ||
} | ||
|
||
## Run the application ######################################################### | ||
shinyApp(ui = ui, server = server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
## load packages --------------------------------------------------------------- | ||
library(shiny) | ||
library(tidyverse) | ||
|
||
## read data ------------------------------------------------------------------- | ||
countries_data <- read_csv("/Users/gulinan/Desktop/Introduction to Data Science with R/Week_13-14/Week13_Lecture_Notes/source_files/data/countries_1998_2011.csv") | ||
|
||
## subset data | ||
countries_data_2011 <- countries_data %>% | ||
filter(year == 2011) | ||
|
||
|
||
## UI ########################################################################## | ||
ui <- fluidPage( | ||
|
||
sidebarLayout(#position = "right", | ||
|
||
## define inputs in sidebar ------------------------------- | ||
sidebarPanel( | ||
|
||
## select variable for scatter plot x-axis -------------- | ||
## label: widget label. | ||
selectInput(inputId = "x_axis", label = "X axis", | ||
choices = c("human_development_index", "corruption_perception_index", | ||
"population", "life_exp", "gdp_per_capita"), | ||
selected = "human_development_index"), | ||
|
||
## select variable for scatter plot y-axis --------------- | ||
selectInput(inputId = "y_axis", label = "Y axis", | ||
choices = c("human_development_index", "corruption_perception_index", | ||
"population", "life_exp", "gdp_per_capita"), | ||
selected = "corruption_perception_index"), | ||
|
||
## select variable for point size --------------- | ||
selectInput(inputId = "point_size", label = "Point Size", | ||
choices = c("population", "life_exp", "gdp_per_capita"), | ||
selected = "population") | ||
), | ||
|
||
## Show output in main panel ----------------------------------------------- | ||
mainPanel( | ||
## show plot | ||
## outputId:output variable to read the plot from. | ||
plotOutput(outputId = "countries_scatter") | ||
) | ||
) | ||
) | ||
|
||
## SERVER ###################################################################### | ||
server <- function(input, output) { | ||
|
||
#add a third element to input list. | ||
output$countries_scatter <- renderPlot({ | ||
ggplot(data = countries_data_2011, | ||
aes_string(x = input$x_axis, y = input$y_axis)) + | ||
geom_point(aes_string(color="continent", size=input$point_size) ) + | ||
#point size depends on a variable. | ||
theme_minimal() | ||
}) | ||
} | ||
|
||
## Run the application ######################################################### | ||
shinyApp(ui = ui, server = server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
## load packages --------------------------------------------------------------- | ||
library(shiny) | ||
library(tidyverse) | ||
|
||
## read data ------------------------------------------------------------------- | ||
countries_data <- read_csv("/Users/gulinan/Desktop/Introduction to Data Science with R/Week_13-14/Week13_Lecture_Notes/source_files/data/countries_1998_2011.csv") | ||
|
||
## subset data | ||
countries_data_2011 <- countries_data %>% | ||
filter(year == 2011) | ||
|
||
|
||
## UI ########################################################################## | ||
ui <- fluidPage( | ||
|
||
sidebarLayout(#position = "right", | ||
|
||
## define inputs in sidebar ------------------------------- | ||
sidebarPanel( | ||
|
||
## select variable for scatter plot x-axis -------------- | ||
## label: widget label. | ||
selectInput(inputId = "x_axis", label = "X axis", | ||
choices = c("human_development_index", "corruption_perception_index", | ||
"population", "life_exp", "gdp_per_capita"), | ||
selected = "human_development_index"), | ||
|
||
## select variable for scatter plot y-axis --------------- | ||
selectInput(inputId = "y_axis", label = "Y axis", | ||
choices = c("human_development_index", "corruption_perception_index", | ||
"population", "life_exp", "gdp_per_capita"), | ||
selected = "corruption_perception_index"), | ||
|
||
## select variable for point size --------------- | ||
selectInput(inputId = "size", label = "Point Size", | ||
choices = c("population", "life_exp", "gdp_per_capita"), | ||
selected = "population"), | ||
|
||
## set transparency level for points in the scatter plot ---------------- | ||
sliderInput(inputId ="transvalue", label = "Transparency", | ||
min = 0, max = 1, value = 0.8) | ||
|
||
), | ||
|
||
## Show output in main panel ----------------------------------------------- | ||
mainPanel( | ||
## show plot | ||
## outputId:output variable to read the plot from. | ||
plotOutput(outputId = "countries_scatter") | ||
) | ||
) | ||
) | ||
|
||
## SERVER ###################################################################### | ||
server <- function(input, output) { | ||
|
||
|
||
output$countries_scatter <- renderPlot({ | ||
ggplot(data = countries_data_2011, | ||
aes_string(x = input$x_axis, y = input$y_axis)) + | ||
geom_point(aes_string(color="continent", size=input$size), alpha = input$transvalue) + | ||
#note that i am directly changing the characteristics of point. it is out of aes(). | ||
#i mean its value does not depend on a variable, that is why out of aes(). | ||
theme_minimal() | ||
}) | ||
} | ||
|
||
## Run the application ######################################################### | ||
shinyApp(ui = ui, server = server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
## load packages --------------------------------------------------------------- | ||
library(shiny) | ||
library(tidyverse) | ||
library(DT) | ||
|
||
## read data ------------------------------------------------------------------- | ||
countries_data <- read_csv("/Users/gulinan/Desktop/Introduction to Data Science with R/Week_13-14/Week13_Lecture_Notes/source_files/data/countries_1998_2011.csv") | ||
|
||
## subset data | ||
countries_data_2011 <- countries_data %>% | ||
filter(year == 2011) | ||
|
||
|
||
## UI ########################################################################## | ||
ui <- fluidPage( | ||
|
||
sidebarLayout(#position = "right", | ||
|
||
## define inputs in sidebar ------------------------------- | ||
sidebarPanel( | ||
|
||
## select variable for scatter plot x-axis -------------- | ||
## label: widget label. | ||
selectInput(inputId = "x_axis", label = "X axis", | ||
choices = c("human_development_index", "corruption_perception_index", | ||
"population", "life_exp", "gdp_per_capita"), | ||
selected = "human_development_index"), | ||
|
||
## select variable for scatter plot y-axis --------------- | ||
selectInput(inputId = "y_axis", label = "Y axis", | ||
choices = c("human_development_index", "corruption_perception_index", | ||
"population", "life_exp", "gdp_per_capita"), | ||
selected = "corruption_perception_index"), | ||
|
||
## select variable for point size --------------- | ||
selectInput(inputId = "size", label = "Point Size", | ||
choices = c("population", "life_exp", "gdp_per_capita"), | ||
selected = "population"), | ||
|
||
## set transparency level for points in the scatter plot ---------------- | ||
sliderInput(inputId ="transvalue", label = "Transparency", | ||
min = 0, max = 1, value = 0.8) | ||
|
||
), | ||
|
||
## Show output in main panel ----------------------------------------------- | ||
mainPanel( | ||
## show plot | ||
## outputId:output variable to read the plot from. | ||
plotOutput(outputId = "countries_scatter"), | ||
|
||
# show table | ||
dataTableOutput(outputId ="table") | ||
|
||
) | ||
|
||
) | ||
|
||
|
||
) | ||
|
||
## SERVER ###################################################################### | ||
server <- function(input, output) { | ||
|
||
#now the output list two components. one is plot, the other one is table. | ||
output$countries_scatter <- renderPlot({ | ||
ggplot(data = countries_data_2011, | ||
aes_string(x = input$x_axis, y = input$y_axis)) + | ||
geom_point(aes_string(color="continent", size=input$size), | ||
alpha = input$transvalue) + | ||
theme_minimal() | ||
|
||
}) | ||
|
||
|
||
output$table <- renderDataTable({ | ||
countries_data_2011 | ||
}) | ||
|
||
|
||
} | ||
|
||
## Run the application ######################################################### | ||
shinyApp(ui = ui, server = server) |
Oops, something went wrong.