shiny
, tidyverse
and cepespR
packages.library(shiny)
library(tidyverse)
library(cepespR)
cepespR
, save the data as a “.csv” and then include a line opening the “.csv”. (Remove the cepespR
and write.csv
code after).data <- get_elections(year=2014, position="President",regional_aggregation="State",political_aggregation="Candidate") %>%
mutate(NOME_PARTIDO=iconv(NOME_PARTIDO, "ASCII", "UTF-8", sub=""))
write.csv(data,"data.csv")
data <- read.csv("data.csv")
Before creating the Shiny app, create an example analysis first: Filter the election for the second round (NUM_TURNO
) and the PSDB (SIGLA_PARTIDO
). In a separate line of code, create a ggplot
output that displays a column chart (geom_col
) of the number of votes the party received in each state.
Now, let’s put this analysis within a Shiny app. Create a User Interface with appropriate inputs to allow the user to select the round of the election (NUM_TURNO
) and the Party (SIGLA_PARTIDO
).
Turn your code for filtering the dataset by round and party into a reactive
function within the server
function that responds to the two inputs in your UI.
Turn your ggplot2 graphic output into a renderPlot
function within the server
function so that it depends on the reactive
function you created in the previous question.
Create a place for this renderPlot
graphic to appear in your ui
function using plotOutput
.
Test your Shiny App and see if it works!
Make the scale of your graph fixed (ylim
) so it shows all the values but is easier to compare vote quantities when selecting between parties. (Hint: The maximum number of votes is for the PSDB in SP in the second round).
Create another reactive
output that calculates the total percentage of the vote the selected party received in the selected round of the election.
Create a text output (renderText
) in the server
function and a textOutput
in the ui
function to display the result of the calculation in Question 7.
library(tidyverse)
library(shiny)
library(cepespR)
data <- read_csv("data.csv")
# Define UI for application that draws a histogram
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("Partido",
label="Partido",
choices = c("PSDB","PT","PSOL"),
selected = "PT"),
radioButtons("Turno",
label="Numero Turno",
choices=c(1,2),
selected=1)),
mainPanel(
plotOutput("Graph"),
h4("The total number of votes received by this party in this election is:"),
textOutput("Pct_Vote")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
new_data <- reactive({
new_data <- data %>% filter(SIGLA_PARTIDO==input$Partido & NUM_TURNO==input$Turno) %>%
mutate(NOME_PARTIDO=iconv(NOME_PARTIDO, "ASCII", "UTF-8", sub=""))
})
output$Graph <- renderPlot({
new_data() %>% ggplot() +
geom_col(aes(x=SIGLA_UE,y=QTDE_VOTOS)) +
ylim(c(0,16000000))
})
output$Pct_Vote <- renderText({
new_data() %>% summarise(Total_votes=sum(QTDE_VOTOS,na.rm=T)) %>%
pull(Total_votes) %>%
formatC(format="d", big.mark=".")
})
}
# Run the application
shinyApp(ui = ui, server = server)