Does this work?
#All libraries being loaded and all csvs being read and assigned to variables
library(shiny)
library(tidyverse)
library(ggplot2)
library(dplyr)
library(plotly)
library(ggthemes)
short_raw <- tibble::tribble(
~date, ~country, ~daily_new_cases, ~cumulative_total_deaths, ~cumulative_total_cases, ~GDPPC, ~region, ~pop, ~rrank, ~prop_right, ~jud_eff, ~unemp, ~ffree, ~life_ex, ~inf_mort, ~death_rate, ~infec_rate,
"2021-6-11", "Afghanistan", 1172, 3460, 88888, "$1,958", "Asia-Pacific", "35.5", "39", "19.6", "29.6", "8.8", "10", 65, 62, 0.0389253892538925, 0.00250388732394366,
"2021-6-11", "Albania", 11, 2453, 132437, "$12,507", "Europe", "2.9", "27", "54.8", "30.6", "13.9", "70", 77.8, 0, 0.0185220142407333, 0.0456679310344828,
"2021-6-11", "Algeria", 372, 3560, 132727, "$15,237", "Middle East and North Africa", "41.5", "14", "31.6", "36.2", "10.0", "30", 75.6, 21, 0.0268219729218622, 0.00319824096385542,
"2021-6-11", "Angola", 130, 819, 36455, "$6,753", "Sub-Saharan Africa", "28.2", "33", "35.9", "26.6", "8.2", "40", 52.4, 66, 0.0224660540392264, 0.0012927304964539,
"2021-6-11", "Argentina", 26934, 84628, 4093090, "$20,876", "Americas", "44.1", "26", "47.8", "44.5", "8.7", "60", 76.3, 8, 0.0206758219340401, 0.0928138321995465,
"2021-6-11", "Armenia", 95, 4482, 223555, "$9,456", "Europe", "3.0", "24", "57.2", "46.3", "18.2", "70", 74.8, 1, 0.0200487575764353, 0.0745183333333333,
"2021-6-11", "Australia", 4, 910, 30229, "$50,334", "Asia-Pacific", "24.8", "4", "79.1", "86.5", "5.6", "90", 82.8, 1, 0.0301035429554401, 0.00121891129032258,
"2021-6-11", "Austria", 216, 10652, 648187, "$49,869", "Europe", "8.8", "16", "84.2", "71.3", "5.5", "70", 81.5, 0, 0.0164335292130203, 0.0736576136363636,
"2021-6-11", "Azerbaijan", 73, 4951, 335065, "$17,492", "Asia-Pacific", "9.8", "13", "59.1", "53.1", "5.0", "60", 72.7, 5, 0.0147762374464656, 0.034190306122449,
"2021-6-11", "Bahamas", 40, 236, 12092, "$31,139", "Americas", "0.4", "15", "42.2", "46.9", "12.6", "60", 76.1, 0, 0.0195170360568971, 0.03023,
"2021-6-11", "Bahrain", 967, 1196, 256921, "$48,505", "Middle East and North Africa", "1.5", "5", "63.5", "50.7", "1.2", "80", 76.9, 0, 0.0046551274516291, 0.171280666666667,
"2021-6-11", "Bangladesh", 2454, 13032, 822849, "$4,211", "Asia-Pacific", "163.2", "27", "36.1", "34.5", "4.4", "30", 71.8, 92, 0.0158376567268114, 0.00504196691176471,
"2021-6-11", "Barbados", 0, 47, 4033, "$18,664", "Americas", "0.3", "13", "52.9", "59.9", "9.7", "60", 75.5, 0, 0.0116538556905529, 0.0134433333333333,
"2021-6-11", "Belarus", 1026, 2958, 403845, "$18,931", "Europe", "9.5", "42", "55.2", "51.7", "0.5", "10", 72.3, 0, 0.00732459235597816, 0.04251,
"2021-6-11", "Belgium", 846, 25068, 1074204, "$46,553", "Europe", "11.4", "25", "81.3", "61.6", "7.4", "70", 81.1, 0, 0.0233363495202029, 0.0942284210526316,
"2021-6-11", "Belize", 14, 325, 12938, "$8,324", "Americas", "0.4", "24", "41.7", "46.9", "9.3", "50", 71, 0, 0.0251198021332509, 0.032345,
"2021-6-11", "Benin", 0, 102, 8109, "$2,277", "Sub-Saharan Africa", "11.1", "21", "37.2", "32.8", "2.5", "50", 60, 25, 0.0125786163522013, 0.000730540540540541,
"2021-6-11", "Bhutan", 20, 1, 1789, "$8,744", "Asia-Pacific", "0.8", "16", "62.5", "55.4", "2.4", "30", 69.8, 0, 0.000558971492453885, 0.00223625,
"2021-6-11", "Botswana", 0, 896, 59480, "$17,828", "Sub-Saharan Africa", "2.2", "3", "58.1", "45.7", "17.4", "70", 65.7, 2, 0.0150638870208473, 0.0270363636363636,
"2021-6-11", "Brazil", 86061, 484350, 17301220, "$15,603", "Americas", "207.7", "27", "57.3", "51.7", "13.3", "50", 75, 42, 0.027995135603154, 0.083299085219066,
"2021-6-11", "Brunei Darussalam", 0, 3, 248, "$78,196", "Asia-Pacific", "0.4", "14", "64.0", "56.0", "7.1", "50", 77.7, 0, 0.0120967741935484, 0.00062,
"2021-6-11", "Bulgaria", 123, 17893, 420213, "$21,687", "Europe", "7.1", "19", "62.5", "41.9", "6.2", "60", 74.5, 0, 0.0425807864107012, 0.0591849295774648,
"2021-6-11", "Burkina Faso", 0, 167, 13456, "$1,889", "Sub-Saharan Africa", "18.9", "9", "49.1", "42.9", "6.3", "40", 59.9, 38, 0.012410820451843, 0.000711957671957672,
"2021-6-11", "Burundi", 15, 8, 4975, "$735", "Sub-Saharan Africa", "10.9", "39", "20.6", "31.0", "1.6", "30", 59.6, 21, 0.00160804020100503, 0.000456422018348624,
"2021-6-11", "Cabo Verde", 79, 272, 31512, "$6,944", "Sub-Saharan Africa", "0.5", "4", "44.1", "49.0", "10.3", "60", 73.3, 0, 0.00863163239400863, 0.063024,
"2021-6-11", "Cambodia", 655, 311, 37321, "$4,012", "Asia-Pacific", "16.0", "22", "37.4", "27.6", "0.2", "50", 68.7, 10, 0.00833311004528282, 0.0023325625,
"2021-6-11", "Cameroon", 186, 1310, 80090, "$3,660", "Sub-Saharan Africa", "24.3", "29", "42.5", "31.3", "4.2", "50", 57.3, 45, 0.0163565988263204, 0.00329588477366255,
"2021-6-11", "Canada", 1441, 25886, 1399716, "$48,265", "Americas", "36.7", "1", "87.0", "69.4", "6.3", "80", 82.2, 2, 0.0184937515896082, 0.0381394005449591,
"2021-6-11", "Central African Republic", 0, 98, 7101, "$677", "Sub-Saharan Africa", "5.0", "38", "19.6", "29.6", "6.0", "30", 52.5, 15, 0.0138008731164625, 0.0014202,
"2021-6-11", "Chad", 0, 174, 4942, "$2,344", "Sub-Saharan Africa", "12.2", "36", "26.7", "24.6", "5.9", "40", 53.1, 46, 0.0352084176446783, 0.000405081967213115,
"2021-6-11", "Chile", 7941, 30472, 1461419, "$24,537", "Americas", "18.4", "3", "68.7", "56.3", "7.0", "70", 85, 2, 0.0208509674501289, 0.0794249456521739,
"2021-6-11", "China", 22, 4636, 91359, "$16,660", "Asia-Pacific", "1390.1", "20", "49.9", "75.2", "4.7", "20", 76.1, 157, 0.0507448636696987, 6.5721171138767e-05,
"2021-6-11", "Colombia", 29570, 94615, 3694707, "$14,485", "Americas", "49.3", "8", "59.2", "34.3", "8.9", "70", 74.8, 10, 0.0256082552689564, 0.0749433468559838,
"2021-6-11", "Comoros", 0, 146, 3894, "$1,588", "Sub-Saharan Africa", "0.8", "20", "36.5", "29.6", "4.3", "30", 63.5, 1, 0.0374935798664612, 0.0048675,
"2021-6-11", "Costa Rica", 1852, 4322, 339900, "$16,877", "Americas", "5.0", "11", "58.3", "54.0", "8.1", "50", 79.6, 1, 0.0127155045601648, 0.06798,
"2021-6-11", "Croatia", 134, 8123, 358379, "$24,424", "Europe", "4.1", "38", "66.0", "42.9", "11.2", "60", 78, 0, 0.0226659486186412, 0.087409512195122,
"2021-6-11", "Cuba", 1288, 1065, 154866, "$12,920", "Americas", "11.5", "31", "31.6", "10.0", "2.6", "10", 79.1, 1, 0.0068769129440936, 0.0134666086956522,
"2021-6-11", "Cyprus", 60, 372, 73115, "$37,023", "Europe", "0.9", "22", "73.1", "48.1", "11.0", "60", 85, 0, 0.0050878752649935, 0.0812388888888889,
"2021-6-11", "Denmark", 418, 2523, 289122, "$49,883", "Europe", "5.7", "6", "86.2", "77.8", "5.7", "80", 86, 0, 0.00872641998879366, 0.0507231578947368,
"2021-6-11", "Djibouti", 1, 154, 11571, "$3,559", "Sub-Saharan Africa", "1.0", "43", "29.7", "18.1", "5.8", "50", 63.5, 1, 0.0133091349062311, 0.011571,
"2021-6-11", "Dominican Republic", 1129, 3700, 306698, "$16,944", "Americas", "10.2", "19", "50.6", "18.1", "5.5", "40", 73.9, 6, 0.0120639847667738, 0.030068431372549,
"2021-6-11", "Ecuador", 1098, 20997, 437121, "$11,482", "Americas", "16.8", "29", "35.9", "20.2", "3.8", "40", 76.2, 6, 0.0480347546789104, 0.0260191071428571,
"2021-6-11", "Egypt", 733, 15547, 271780, "$12,671", "Middle East and North Africa", "94.8", "11", "37.0", "48.3", "12.1", "50", 79, 51, 0.0572043564647877, 0.0028668776371308,
"2021-6-11", "El Salvador", 0, 2288, 75351, "$8,948", "Americas", "6.4", "17", "37.6", "29.1", "4.5", "60", 73.5, 2, 0.0303645605234171, 0.01177359375,
"2021-6-11", "Equatorial Guinea", 0, 118, 8640, "$36,017", "Sub-Saharan Africa", "0.8", "44", "29.7", "18.1", "6.9", "30", 58.2, 3, 0.0136574074074074, 0.0108,
"2021-6-11", "Eritrea", 82, 16, 4848, "$1,581", "Sub-Saharan Africa", "5.9", "47", "35.5", "18.1", "6.4", "20", 64.7, 5, 0.0033003300330033, 0.000821694915254237,
"2021-6-11", "Estonia", 54, 1266, 130418, "$31,750", "Europe", "1.3", "7", "81.5", "76.0", "5.8", "70", 77.6, 0, 0.00970724899937125, 0.100321538461538,
"2021-6-11", "Ethiopia", 214, 4235, 273892, "$2,161", "Sub-Saharan Africa", "92.7", "26", "32.6", "40.9", "5.2", "20", 64.8, 136, 0.0154622990083683, 0.00295460625674218,
"2021-6-11", "Fiji", 51, 4, 970, "$9,777", "Asia-Pacific", "0.9", "18", "67.3", "42.9", "6.3", "50", 69.9, 0, 0.00412371134020619, 0.00107777777777778,
"2021-6-11", "Finland", 47, 964, 93660, "$44,333", "Europe", "5.5", "11", "89.6", "81.2", "8.6", "80", 81.1, 0, 0.0102925475122785, 0.0170290909090909,
"2021-6-11", "France", 3871, 110344, 5733838, "$43,761", "Europe", "64.8", "35", "82.5", "66.1", "9.4", "70", 82.4, 2, 0.0192443525610595, 0.0884851543209876,
"2021-6-11", "Gabon", 40, 156, 24736, "$19,254", "Sub-Saharan Africa", "1.9", "17", "28.1", "30.6", "19.7", "40", 66, 2, 0.00630659767141009, 0.0130189473684211,
"2021-6-11", "Gambia", 0, 180, 6008, "$1,713", "Sub-Saharan Africa", "2.1", "30", "39.9", "42.5", "9.5", "50", 61.1, 3, 0.0299600532623169, 0.00286095238095238,
"2021-6-11", "Georgia", 695, 5012, 352649, "$10,747", "Europe", "3.7", "8", "65.9", "54.6", "11.6", "70", 74.4, 1, 0.0142124321917828, 0.0953105405405405,
"2021-6-11", "Germany", 2194, 90398, 3720811, "$50,425", "Europe", "82.7", "14", "79.9", "75.4", "3.8", "70", 81, 2, 0.0242952410106291, 0.0449916686819831,
"2021-6-11", "Ghana", 75, 789, 94444, "$4,729", "Sub-Saharan Africa", "28.3", "13", "49.1", "44.2", "2.4", "60", 62.4, 37, 0.00835415696073864, 0.00333724381625442,
"2021-6-11", "Greece", 784, 12381, 413954, "$27,737", "Europe", "10.8", "43", "52.4", "49.5", "21.5", "50", 81, 0, 0.0299091203370423, 0.0383290740740741,
"2021-6-11", "Guatemala", 1861, 8416, 269308, "$8,145", "Americas", "16.9", "16", "40.3", "32.3", "2.7", "50", 71.9, 10, 0.0312504641525688, 0.0159353846153846,
"2021-6-11", "Guinea", 14, 167, 23366, "$2,041", "Sub-Saharan Africa", "13.0", "19", "34.7", "32.6", "4.5", "40", 59, 26, 0.00714713686553111, 0.00179738461538462,
"2021-6-11", "Guyana", 108, 422, 18196, "$8,161", "Americas", "0.8", "23", "41.7", "42.9", "12.0", "30", 66.2, 0, 0.0231919103099582, 0.022745,
"2021-6-11", "Haiti", 127, 349, 16206, "$1,815", "Americas", "11.0", "25", "10.4", "25.3", "14.0", "30", 63.5, 14, 0.021535233864001, 0.00147327272727273,
"2021-6-11", "Honduras", 828, 6567, 244295, "$5,562", "Americas", "8.3", "20", "43.4", "31.0", "4.5", "60", 74.6, 3, 0.0268814343314435, 0.0294331325301205,
"2021-6-11", "Hungary", 199, 29904, 806790, "$29,474", "Europe", "9.8", "31", "60.9", "45.2", "4.2", "70", 75.8, 0, 0.0370654073550738, 0.0823255102040816,
"2021-6-11", "Iceland", 0, 30, 6616, "$51,842", "Europe", "0.3", "4", "87.4", "63.8", "2.8", "70", 82.7, 0, 0.00453446191051995, 0.0220533333333333,
"2021-6-11", "India", 84695, 367097, 29358033, "$7,183", "Asia-Pacific", "1316.9", "31", "57.3", "61.6", "3.5", "40", 68.3, 910, 0.0125041415410903, 0.0222932895436252,
"2021-6-11", "Indonesia", 8083, 52566, 1894025, "$12,377", "Asia-Pacific", "262.0", "11", "52.2", "53.5", "4.2", "60", 69.1, 114, 0.0277535935375721, 0.00722910305343511,
"2021-6-11", "Iraq", 4320, 16668, 1246860, "$16,954", "Middle East and North Africa", "38.9", "N/A", "37.0", "12.3", "8.2", "N/A", 68.9, 32, 0.0133679803666811, 0.0320529562982005,
"2021-6-11", "Ireland", 319, 4941, 266073, "$75,538", "Europe", "4.7", "2", "85.8", "68.4", "6.4", "70", 81.4, 0, 0.0185700916665727, 0.0566112765957447,
"2021-6-11", "Israel", 23, 6428, 839653, "$36,340", "Middle East and North Africa", "8.7", "2", "80.0", "73.4", "4.3", "70", 82.5, 0, 0.00765554342091316, 0.0965118390804598,
"2021-6-11", "Italy", 1901, 126924, 4241760, "$38,140", "Europe", "60.6", "36", "71.7", "49.8", "11.2", "50", 82.7, 1, 0.0299224850062238, 0.0699960396039604,
"2021-6-11", "Jamaica", 69, 990, 49179, "$9,163", "Americas", "2.8", "5", "60.7", "49.2", "12.5", "50", 76.2, 1, 0.0201305435246752, 0.0175639285714286,
"2021-6-11", "Japan", 2046, 13907, 769920, "$42,832", "Asia-Pacific", "126.7", "8", "84.1", "68.5", "2.8", "60", 83.7, 2, 0.0180629156275977, 0.00607671665351223,
"2021-6-11", "Jordan", 343, 9577, 742521, "$12,494", "Middle East and North Africa", "7.1", "4", "58.4", "52.6", "14.9", "60", 74.1, 4, 0.0128979517077631, 0.104580422535211,
"2021-6-11", "Kazakhstan", 1120, 4136, 399096, "$26,252", "Asia-Pacific", "18.2", "12", "59.3", "56.1", "4.9", "50", 72, 4, 0.0103634213322108, 0.0219283516483516,
"2021-6-11", "Kenya", 488, 3378, 174773, "$3,491", "Sub-Saharan Africa", "46.7", "22", "53.8", "46.9", "11.5", "50", 63.4, 54, 0.0193279282269, 0.0037424625267666
)
#UI Argument of ShinyApp Function
ui <- navbarPage("Covid TrackR", #nav bar title
tabPanel("Socio-economics", #first graph tab
fluidPage(h2("How do different socio-economic metrics affect
Covid-19 death rates and infection rates around the world?"), align = "center", #Title
fluidRow(align = "left",
column(2,offset = 2,
h3("Variable Selection"), #variable column title
helpText(h5("Click to explore dropdown menu.")), #instruction text
selectInput("var",#first drop down menu variable assignment
label = "Choose an x-variable to display", #menu label
choices = c("Population", "Unemployment_Rate", #menu choices
"Property_Rights_Score", "Judical_Efficiency_Score",
"Life_Expectancy"),
selected = "Population"),#default menu choice
selectInput("vary", #second drop down menu variable assignment
label = "Choose an y-variable to display", #menu label
choices = c("Death_Rate", "Infection_rate"), #menu choices
selected = "Death_Rate") #default menu choice
),
column(6, #column where graph is displayed
h3("Graph"),
textOutput("selected_var"),
plotlyOutput("plot2")
)
)
)
)
)
server <- function(input, output) { #server argument of ShinyApp
#assigning data vectors to the selected value from the drop down menu of the UI (x var)
datax <- reactive({
switch(input$var,
Population = short_raw$pop,
Unemployment_Rate = short_raw$unemp,
Property_Rights_Score = short_raw$prop_right,
Judical_Efficiency_Score = short_raw$jud_eff,
Life_Expectancy = short_raw$life_ex
)
})
#assigning data vectors to the selected value from the drop down menu of the UI (y var)
datay <- reactive({
switch(input$vary,
Death_Rate = short_raw$death_rate,
Infection_rate = short_raw$infec_rate
)
})
output$selected_var <- renderText({ #rendering plot
paste("You have selected", input$var, "vs." , input$vary)
})
output$plot2 <- renderPlotly({
econ_plot <- short_raw %>%
ggplot(aes(color = country, x= !!as.numeric(datax()) , y = !!as.numeric(datay()), #selecting x and y var from those assigned from drop down menu
text = paste("Country:", country, "\n", #hover text from plotly
paste(input$var,": "), paste(datax(),ifelse(input$var == "Population", "M", ""),
ifelse(input$var == "Unemployment_Rate", "%", "")), "\n",
"Death Rate:", paste(format(round(death_rate*100, 2), nsmall = 2), "%"), "\n",
"GDPPC:", GDPPC, "\n",
"Total Deaths:", cumulative_total_deaths, "\n",
"Total Cases:", cumulative_total_cases, "\n")))+
geom_point()+
geom_smooth(method="loess", formula = y~x)+
scale_y_continuous(labels = scales::percent_format(accuracy = 1))+ #setting y axis to percentage
theme_clean()+
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position = "none")+
facet_wrap(~region)+
labs(x = input$var, y = input$vary) #graph title and lebels
econ_plot <- ggplotly(econ_plot, tooltip = "text") #printing plot with plotly
})
}
shinyApp(ui = ui, server = server)