Skip to content

Commit

Permalink
week 13
Browse files Browse the repository at this point in the history
  • Loading branch information
gulinan committed Jan 1, 2022
1 parent 8c6c9e7 commit 3489d08
Show file tree
Hide file tree
Showing 67 changed files with 10,903 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
13 changes: 13 additions & 0 deletions Week_13_14_Lecture_Notes.Rproj
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 added cheat_sheets/.DS_Store
Binary file not shown.
Binary file added cheat_sheets/shiny.pdf
Binary file not shown.
72 changes: 72 additions & 0 deletions countries-01/app.R
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)
63 changes: 63 additions & 0 deletions countries-02/app.R
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)
69 changes: 69 additions & 0 deletions countries-03/app.R
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)
84 changes: 84 additions & 0 deletions countries-04/app.R
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)
Loading

0 comments on commit 3489d08

Please sign in to comment.