Assignment 5

Overview

For this assignment, you are going to be reproducing a version of this Shiny app and using it to evaluate Samuel Huntington’s classic book, The Third Wave which, according to Google, has been cited more than 19,700 times! For convenience, we will be looking a Journal of Democracy article by Huntington that summarizes his main points from the book. You will be doing most of your coding in the separate app.R template provided for you. A big part of your grade for this assignment will be based on getting that file to produce your Shiny App. However, you can use this document as a worksheet to help generate the various elements of code for your App.

Submission note: For this assignment you should submit your working Shiny App code via GitHub. You can use this workbook to develop your code.

Step 1: Wrangle your data (20 pts)

Load vdemdata, dplyr and readr into a separate R script or code chunk in a Quarto document. Select country_name, year, and three V-Dem democracy indicators: v2x_polyarchy, v2x_libdem, and v2x_paritpdem. Now go to the V-Dem codebook and select four additional indicators related to democracy that you would like to explore. Make sure that your chosen indicators cover the same time period as those in the app (1789-2021). Have a glimpse() and the data to make sure everythign is there. Then store your data in an object called vdem_trends and then save it as a .csv file by the same name.

Step 2: Do the setup work (20 pts)

Now, in your app.R file, load your packages, load the data into an object called vdem_trends, and create lists of the variables and countries for the ui.

Packages: You will need to load shiny, shinyWidgets, readr, dplyr, ggplot2 and lubridate.

Data: These will be loaded in from the .csv file you created in Step 1.

Variable list: Create a list of values using the combine function (c()) to be used in the “Indicator” dropdown menu. Some starter code is provided for you here:

vars <- c("Polyarchy" = "v2x_polyarchy"), # be sure to insert the variable names you have 
          "Liberal Democracy" = ......   # in your wrangled data on the right hand side
          )                              # of these equations 

Country names list: To get a list of country names, filter() vdem_trends for one year (tip: you can use max(year) for this), pull() the country column and pipe that into sort() (with no arguments) so that the country names are arranged in alphabetical order. Store the list of country names in an object called countries.

countries <- vdem_trends |>
  filter(....) |> # filter what?
  pull(....) |>   # pull what?
  sort()          # no arguments for sort

Step 3: Define the UI (20 pts)

Define the UI using code similar to what we saw for the FRED line chart app in Module 5.2. You will want a quarter of the columns to be occupied by the wellPanel() with the “Indicator:” and “Country:” dropdown menus and some help text. The remaining 3/4 of the space is going to be occupied by your plot output. For the slider input, use this code:

           sliderTextInput(
             "range",
             NULL,
             grid = TRUE,
             choices = 1789:2021,
             selected = c(1789, 2021),
             width = "100%"
           )

Step 4: Define the server logic (20 pts)

Now the tricky part. This app is going to require two reactive functions. The first is going to be for the country and indicator dropdowns:

  ctry_indicator <- reactive({
    vdem_trends |>
    filter(country == INPUT) |> # replace INPUT with relevant input
    select(year, INPUT)         # ditto
    })

The second is going to be for the year slider:

  plot_df <- reactive({
    ctry_indicator() |>
    filter(year %in% (INPUT:INPUT)) # replace INPUT with relevant input
   })

Now you need to render the plot. In the ggplot() call your data is going to be the saved in the second reactive function plot_df() (don’t forget the parentheses!). In aes() x will simply be year but y is a little tricky. Here you need to call get(input$indicator)) You might have guessed the input$indicator part, but to non-standard evaluation in aes(), you cannot pass your column into ggplot before you remove the parentheses. get() is one of many ways to do this.

For the y-labels, you can use names(vars[which(vars == input$indicator)])) to get the name of the selected indicator.

Step 5: Run the app and interpret the results (20 pts)

Use the shinyApp() funciton to run the app. Does it work? If not, try some of the debugging techniques we talked about in class or reach out on Discord for help.

Once you get the app running, take some time to explore the data in a few of your favorite countries. How do the trends in your chosen cases speak to Huntington’s idea about a third wave of democratization or, anecdotally, the types barriers that remain to democratization in that country? Type your answer below (not in your app.R file!).