In the app.R file, use this code for the global section of the app…
# load packageslibrary(shiny)library(readr)library(ggplot2)# load the data dem_data <-read_csv("dem_data.csv")# create list of named values for the input selectionvars <-c("Democracy"="polyarchy","Clientelism"="clientelism","Corruption"="corruption","Women's Empowerment"="womens_emp","Wealth"="gdp_pc","Life Expectancy"="life_exp", "Education"="education")
The User Interface (ui)
The UI Code
Copy and paste the UI code into the app.R file…
# Define UI for application that draws a scatter plotui <-fluidPage(# Application titletitlePanel("Democracy and Development"),# Sidebar with a two dropdown menussidebarLayout(sidebarPanel(selectInput(input ='xcol', label ='X Variable', choices = vars),selectInput(input ='ycol', label ='Y Variable', choices = vars, selected = vars[[6]]) ),# Show a plot of the generated distributionmainPanel(plotOutput("scatterplot") ) ))
The UI Code
Adds the the title panel…
# Define UI for application that draws a scatter plotui <-fluidPage(# Application titletitlePanel("Democracy and Development"),# Sidebar with a two dropdown menussidebarLayout(sidebarPanel(selectInput(input ='xcol', label ='X Variable', choices = vars),selectInput(input ='ycol', label ='Y Variable', choices = vars, selected = vars[[6]]) ),# Show a plot of the generated distributionmainPanel(plotOutput("scatterplot") ) ))
The UI Code
Defines the sidebar layout with the two dropdown panels…
# Define UI for application that draws a scatter plotui <-fluidPage(# Application titletitlePanel("Democracy and Development"),# Sidebar with a two dropdown menussidebarLayout(sidebarPanel(selectInput(input ='xcol', label ='X Variable', choices = vars),selectInput(input ='ycol', label ='Y Variable', choices = vars, selected = vars[[6]]) ),# Show a plot of the generated distributionmainPanel(plotOutput("scatterplot") ) ))
The UI Code
Defines the main panel for the output. Note that we are calling plotOutput() and that we are calling the output “scatterplot” (the unique outputID for our plot)…
# Define UI for application that draws a scatter plotui <-fluidPage(# Application titletitlePanel("Democracy and Development"),# Sidebar with a two dropdown menussidebarLayout(sidebarPanel(selectInput(input ='xcol', label ='X Variable', choices = vars),selectInput(input ='ycol', label ='Y Variable', choices = vars, selected = vars[[6]]) ),# Show a plot of the generated distributionmainPanel(plotOutput("scatterplot") ) ))
The Server (server)
The Server Code
Copy and paste the server code into the app.R file…
# Define server logic required to draw a scatter plotserver <-function(input, output, session) {# Render the plot output$scatterplot <-renderPlot({# ggplot callggplot(dem_data, aes(x = .data[[input$xcol]], y = .data[[input$ycol]])) +geom_point(aes(color = region)) +geom_smooth(method ="loess") +scale_color_viridis_d(option ="plasma") +theme_minimal() +labs(x =names(vars[which(vars == input$xcol)]), # select names in vars thaty =names(vars[which(vars == input$ycol)]), # match input selectionscaption ="Source: V-Dem Institute",color ="Region" ) })}
The Server Code
Note that we are calling renderPlot(), which matches up with the plotOutput() in the UI. And we are referencing output as “scatterplot” (again, the unique outputID for our plot)…
# Define server logic required to draw a scatter plotserver <-function(input, output, session) {# Render the plot output$scatterplot <-renderPlot({# ggplot callggplot(dem_data, aes(x = .data[[input$xcol]], y = .data[[input$ycol]])) +geom_point(aes(color = region)) +geom_smooth(method ="loess") +scale_color_viridis_d(option ="plasma") +theme_minimal() +labs(x =names(vars[which(vars == input$xcol)]), # select names in vars thaty =names(vars[which(vars == input$ycol)]), # match input selectionscaption ="Source: V-Dem Institute",color ="Region" ) })}
The Server Code
We dynamically reference the input values from the dropdown menus in the UI. We use input$xcol and input$ycol to reference the user-selected variables. As a result of tidy evaluation in ggplot2, these inputs have to be referenced in .data[[]] in order to work….
# Define server logic required to draw a scatter plotserver <-function(input, output, session) {# Render the plot output$scatterplot <-renderPlot({# ggplot callggplot(dem_data, aes(x = .data[[input$xcol]], y = .data[[input$ycol]])) +geom_point(aes(color = region)) +geom_smooth(method ="loess") +scale_color_viridis_d(option ="plasma") +theme_minimal() +labs(x =names(vars[which(vars == input$xcol)]), # select names in vars thaty =names(vars[which(vars == input$ycol)]), # match input selectionscaption ="Source: V-Dem Institute",color ="Region" ) })}
The Server Code
Here we need to select the variable from the vector vars that created with the list of variables. We use which(vars == input$xcol) to find the index of the selected variable in the vector vars. We then use names() to get the name of the variable at that index…
# Define server logic required to draw a scatter plotserver <-function(input, output, session) {# Render the plot output$scatterplot <-renderPlot({# ggplot callggplot(dem_data, aes(x = .data[[input$xcol]], y = .data[[input$ycol]])) +geom_point(aes(color = region)) +geom_smooth(method ="loess") +scale_color_viridis_d(option ="plasma") +theme_minimal() +labs(x =names(vars[which(vars == input$xcol)]), # select names in vars thaty =names(vars[which(vars == input$ycol)]), # match input selectionscaption ="Source: V-Dem Institute",color ="Region" ) })}
Call the App
Run the App
To display the app, don’t forget this final line of code…
# Run the application shinyApp(ui = ui, server = server)
Your Turn!
Run the app and select different variables from the drop down menus.
What do you notice about the relationship between democracy and development?