library(vdemdata)
library(dplyr)
library(readr)
dem_data <- vdem |>
filter(year > 2000) |>
select(
country = country_name,
polyarchy = v2x_polyarchy,
clientelism = v2xnp_client,
corruption = v2xnp_regcorr,
womens_emp = v2x_gender,
gdp_pc = e_gdppc,
inf_mort = e_peinfmor,
life_exp = e_pelifeex,
education = e_peaveduc,
region = e_regionpol_6C
) |> mutate(
region = case_match(region,
1 ~ "Eastern Europe",
2 ~ "Latin America",
3 ~ "Middle East",
4 ~ "Africa",
5 ~ "The West",
6 ~ "Asia")
) |>
group_by(country, region) |>
summarize_all(mean, na.rm = TRUE)
#glimpse(dem_data)
write_csv(dem_data, "dem_data.csv")
Module 1.3
Scatter Plot App Setup
- Install the
tidyverse
(if you have not already). Type (install.packages("tidyverse")
) in your console - Install the
devtools
package. Typeinstall.packages("devtools")
in your console. You will need this to install thevdemdata
package because it is not on the CRAN Network. - Install the
vdemdata
package from GitHub. Typedevtools::install_github("vdeminstitute/vdemdata")
in your console. - Start a new project in RStudio
- Create a new R script file that you will use to wrangle the data for the app
- Read Mastering Shiny, Chapter 1
Overview
In this module we are going to start learning by doing. We are going to learn about the basic elements of an R Shiny app by making a scatter plot of wealth and democracy. Here is what we are going to be building:
Take a few minutes to familiarize yourself with this scatter plot app. In the UI, you will see that there are two dropdown menus that allow you to select the variables to appear on the x and y axes of the scatter plot.
Based on what you see here, what do you think you will need to do to prepare the data for the app? Looking ahead, can you guess what you might do to create the server side of the app?
Setting Up the App
I said earlier that there are three main components of a Shiny app (UI, server and call to app). However, with most apps, there is usually a bit of setup int the code before you get to this point. So perhaps you could say there are actually four elements of a Shiny app, with the set up being the “0th” element.
There are going to be two components to the setup for this app. First we are going to “pre-wrangle” our data and store it in a .csv file. Second, we are going to build a setup code chunk that we will include in our app.R
file. Let’s get started.
Consequently, you are going to need to start two files for this module: a new R script file that you will use to wrangle the data for the app; and a new app.R file that will contain the code for the app itself (File>New File>Shiny Web App...
).
Wrangling and storing the data
For this app, we want users to be able to create scatter plots using selected variables from the V-Dem dataset. Let’s say that we want to have a nice mix of variables related to democracy and development so that users can explore how democracy relates to development and vice versa. So we will filter the data for the post-2000 period and select measures relating to democracy, governance and women’s empowerment and then four measures related to development.
We will also make sure to code a region variable and include region in the group_by()
call so that it stays in the data frame. Then we will take the country mean of the measures for the post-2000 period so that we have one set of observations for each each country.
Finally, we will save these data in a .csv file to include with the app. Alternatively, we could have our app wrangle the data each time the app is loaded. This would ensure that the data are always up to date, but it would require more resources than is available with the free version of the Shiny server due to the size of the V-Dem dataset.
Setup code chunk
Now let’s build a setup code chunk that we can include in the app.R script. Here we will load the packages we need for the app and read in the data we just wrangled from the CSV file. Finally, let’s go ahead and create a list of variable names for our dropdown menus in the app and map these to the variables in our data frame.
# load packages
library(shiny)
library(readr)
library(ggplot2)
# load the data
dem_data <- read_csv("dem_data.csv")
# create list of named values for the input selection
vars <- c("Democracy" = "polyarchy",
"Clientelism" = "clientelism",
"Corruption" = "corruption",
"Women's Empowerment" = "womens_emp",
"Wealth" = "gdp_pc",
"Infant Mortality" = "inf_mort",
"Life Expectancy" = "life_exp",
"Education" = "education")