Skip to content

Commit

Permalink
Add reactive parliament plot
Browse files Browse the repository at this point in the history
  • Loading branch information
stesiam committed Nov 4, 2024
1 parent c3d7cfb commit 0e62cd9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 68 deletions.
80 changes: 66 additions & 14 deletions app/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ library(shiny.i18n)
library(dplyr)
library(cookies)
library(htmltools)
library(highcharter)

source("global.R")


i18n <- Translator$new(translation_csvs_path = "www/translations/")
i18n <- Translator$new(translation_csvs_path = "www/translations")
i18n$set_translation_language("en")

# options(shiny.launch.browser = .rs.invokeShinyWindowExternal)
Expand Down Expand Up @@ -125,7 +126,16 @@ ui <- add_cookie_handlers(
)
)
),

div(class = "py-1 max-w-2xl mx-auto",
column(width = 12,
div(class = "card flex justify-center max-w-2xl bg-base-100",
tags$div(
class = "flex p-2 justify-center items-center",
tags$i(class = "fa-solid fa-users-slash text-2xl text-red-600"), # FontAwesome icon
tags$h2(class = "card-title ml-4", i18n$t("Parliament Overview"))
),
highchartOutput("my_chart")
))),

div(class = "py-1 max-w-2xl mx-auto",
column(width = 12,
Expand Down Expand Up @@ -485,19 +495,33 @@ observeEvent(input$selected_language, {
})

data = reactive({
data.frame(
raw_data = data.frame(
"Party" = c("ND", "SYRIZA", "PASOK","KKE","Ellisi", "Niki", "Plefsi",
"Mera", "Other1", "Other2", "Foni Logikis", "Nea Aristera"),
"FullParty" = c("New Democracy", "Coalition of the Radical Left", "Panhellenic Socialistic Movement",
"Communist Party of Greece", "Greek Solution", "Victory", "Course of Freedom",
"Mera25 - DieM25", "Other Party #1", "Other Party #2",
"Voice of Reason", "New Left"),
"Perc" = as.numeric(c(input$nd_pct, input$syriza_pct, input$pasok_pct, input$kke_pct,
input$ellisi_pct, input$niki_pct, input$plefsi_pct, input$mera25_pct,
input$other1_pct, input$other2_pct, input$foni_pct, input$nar_pct))
)
input$other1_pct, input$other2_pct, input$foni_pct, input$nar_pct)),
"col" = c("#008AC5", "#FFC0CB", "#64A12D", "#EB001F",
"#608AC5", "#CC7722", "#BE3075","#E34234",
"#808588", "#777B7E", "#808AC5", "#ff3050"),
"orientation" = c(7,5,6, 2, 9,8,3,4, 1,10,9.5,4.5)
)
})

data_without_NAs = reactive({
data() |>
mutate(Perc = ifelse(is.na(Perc), 0, Perc))
})



calc_seats = reactive({
req(input$elect_systems)
data = data()
data = data_without_NAs()

if (input$elect_systems == "bonus40") {
data %>%
Expand Down Expand Up @@ -555,30 +579,58 @@ observeEvent(input$selected_language, {
})
})

observeEvent(data(), {
observeEvent(data_without_NAs(), {
output$parties_elect_mps <- renderText({
parties_elect_mps(data())
parties_elect_mps(data_without_NAs())
})
})

observeEvent(data(), {
observeEvent(data_without_NAs(), {
output$outOfParliament <- renderText({
paste0(round((100 - inparliament(data())), digits =2), "%")
paste0(round((100 - inparliament(data_without_NAs())), digits =2), "%")
})
})

observeEvent(data(), {
observeEvent(data_without_NAs(), {
output$unallocatedPercentage <- renderText({
paste0(round(unallocated(data()), digits =2), "%")
paste0(round(unallocated(data_without_NAs()), digits =2), "%")
})
})


observeEvent(data(), {
observeEvent(data_without_NAs(), {
output$diff_first_second <- renderText({
paste0(round(find_difference(data()), digits =2), "%")
paste0(round(find_difference(data_without_NAs()), digits =2), "%")
})
})

output$my_chart <- renderHighchart({
w = data_without_NAs() |>
mutate(
Seats = calc_seats()$IntSeats
) %>%
dplyr::filter(Seats >0) %>%
arrange(orientation)



hchart(
w,
"item",
hcaes(
name = Party,
y = Seats,
label = FullParty,
color = col),
name = "Representatives",
showInLegend = TRUE,
size = "125%",
center = list("50%", "75%"),
startAngle = -100,
endAngle = 100
) %>%
hc_legend(labelFormat = '{FullParty} <span style="opacity: 0.4">{y}</span>')
})

}

Expand Down
9 changes: 7 additions & 2 deletions app/global.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

a = data.frame(
"Party" = c("ND", "SYRIZA", "PASOK","KKE","Ellisi", "Niki", "Plefsi", "Mera", "Other1", "Other2"),
"Perc" = c(18, 30, 11, 8, 7, 3, 3, 4, 3, 2)
"Perc" = c(18, 30, 11, 8, 7, 3, 3, 4, 3, 2),
"col" = c("#008AC5", "#FFC0CB", "#64A12D", "#EB001F",
"#608AC5", "#CC7722", "#E34234", "#BE3075",
"#808588", "#777B7E"),
"orientation" = c(7,5,6, 2, 9,8,3,4, 1,10)
)


a <- a %>%
arrange(desc(orientation))
calculate_seats = function(a){


Expand Down
104 changes: 52 additions & 52 deletions app/www/translations/translation_el.csv
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
en,el
"Greek Elections App","Βουλευτικες Εκλογες"
"English","Αγγλικα"
"Greek","Ελληνικα"
"Party", "Κομμα"
"Percentage", "Ποσοστο"
"Seats", "Εδρες"
"New Democracy","Nέα Δημοκρατία"
"SYRIZA","ΣΥΡΙΖΑ"
"PASOK","ΠΑΣΟΚ"
"KKE","ΚΚΕ"
"Greek Solution", "Ελληνική Λύση"
"Niki", "Νίκη"
"Course of Freedom", "Πλεύση Ελευθερίας"
"Foni Logikis", "Φωνή Λογικής"
"MeRA25", "ΜέΡΑ25"
"New Left", "Νέα Αριστερά"
"Other Party #1", "Άλλο κόμμα #1"
"Other Party #2", "Άλλο κόμμα #2"
"Choose Electoral System: ", "Εκλογικό σύστημα: "
"Bonus 40 Seats (2007-2009)", "Μπόνους 40 εδρών (2007-2009)"
"Bonus 50 Seats (2012-2019)", "Μπόνους 50 εδρών (2012-2019)"
"No Bonus (2023Α)", "Αναλογική κατανομή (2023Α)"
"Proportional Bonus (2023Β-", "Αναλογικό μπόνους (2023Β-)"
"Center-right/Right", "Κεντροδεξιά/Δεξιά"
"Center-left/Left", "Κεντροαριστερά/Αριστερά"
"Center-left", "Κεντροαριστερά"
"Left/Far-left", "Αριστερά"
"Right/Far-right", "Δεξιά/Ακροδεξιά"
"Far-right", "Ακροδεξιά"
"Catch-all", "Πολυσυλλεκτικό"
"Far-left", "Ακροαριστερά"
"Left", "Αριστερά"
"Mitsotakis K.", "Μητσοτάκης Κ."
"Kasselakis S.", "Κασσελάκης Σ."
"Androulakis N.", "Ανδρουλάκης Ν."
"Velopoulos K.", "Βελόπουλος Κ."
"Koutsoumpas D.", "Κουτσούμπας Δ."
"Natsios D.", "Νατσιός Δ."
"Konstantopoulou Z.", "Κωνσταντοπούλου Ζ."
"Latinopoulou A.", "Λατινοπούλου Α."
"Varoufakis Y.", "Βαρουφάκης Γ."
"Haritsis A.", "Χαρίτσης Α."
"Majority Required", "Ποσοστό για πλειοψηφία"
"Parliament Parties", "Κοινοβουλευτικά κόμματα"
"Out of Parliament", "Ποσοστό εκτός Βουλής"
"Unallocated Percentage", "Ακατανέμητο ποσοστό"
"Number of parties to elect MPs", "Αριθμός κομμάτων με ποσοστό άνω του 3%"
"Given the (%) of parties out of parliament", "Με βάση το ποσοστό των κομμάτων εκτός βουλής"
"This should not be negative", "Αυτό ΔΕΝ πρέπει να είναι αρνητικό"
"Parties below 3% plus unallocated percentage", "Κόμματα κάτω του 3% συν ακατανέμητα"
"Percentage Difference", "Ποσοστιαία Διαφορά"
"Between 1st and 2nd party", "Μεταξύ πρώτου και δεύτερου κόμματος"
English,Αγγλικα
Greek,Ελληνικα
Party,Κομμα
Percentage,Ποσοστο
Seats,Εδρες
New Democracy,Nέα Δημοκρατία
SYRIZA,ΣΥΡΙΖΑ
PASOK,ΠΑΣΟΚ
KKE,ΚΚΕ
Greek Solution,Ελληνική Λύση
Niki,Νίκη
Course of Freedom,Πλεύση Ελευθερίας
Foni Logikis,Φωνή Λογικής
MeRA25,ΜέΡΑ25
New Left,Νέα Αριστερά
Other Party #1,Άλλο κόμμα #1
Other Party #2,Άλλο κόμμα #2
Choose Electoral System: ,Εκλογικό σύστημα:
Bonus 40 Seats (2007-2009),Μπόνους 40 εδρών (2007-2009)
Bonus 50 Seats (2012-2019),Μπόνους 50 εδρών (2012-2019)
No Bonus (2023Α),Αναλογική κατανομή (2023Α)
Proportional Bonus (2023Β-,Αναλογικό μπόνους (2023Β-)
Center-right/Right,Κεντροδεξιά/Δεξιά
Center-left/Left,Κεντροαριστερά/Αριστερά
Center-left,Κεντροαριστερά
Left/Far-left,Αριστερά
Right/Far-right,Δεξιά/Ακροδεξιά
Far-right,Ακροδεξιά
Catch-all,Πολυσυλλεκτικό
Far-left,Ακροαριστερά
Left,Αριστερά
Mitsotakis K.,Μητσοτάκης Κ.
Kasselakis S.,Κασσελάκης Σ.
Androulakis N.,Ανδρουλάκης Ν.
Velopoulos K.,Βελόπουλος Κ.
Koutsoumpas D.,Κουτσούμπας Δ.
Natsios D.,Νατσιός Δ.
Konstantopoulou Z.,Κωνσταντοπούλου Ζ.
Latinopoulou A.,Λατινοπούλου Α.
Varoufakis Y.,Βαρουφάκης Γ.
Haritsis A.,Χαρίτσης Α.
Majority Required,Ποσοστό για πλειοψηφία
Parliament Parties,Κοινοβουλευτικά κόμματα
Out of Parliament,Ποσοστό εκτός Βουλής
Unallocated Percentage,Ακατανέμητο ποσοστό
Number of parties to elect MPs,Αριθμός κομμάτων με ποσοστό άνω του 3%
Given the (%) of parties out of parliament,Με βάση το ποσοστό των κομμάτων εκτός βουλής
This should not be negative,Αυτό ΔΕΝ πρέπει να είναι αρνητικό
Parliament Overview,Επισκόπηση Βουλής
Parties below 3% plus unallocated percentage,Κόμματα κάτω του 3% συν ακατανέμητα
Percentage Difference,Ποσοστιαία Διαφορά
Between 1st and 2nd party,Μεταξύ πρώτου και δεύτερου κόμματος

0 comments on commit 0e62cd9

Please sign in to comment.