When having no value in the selectInput box I get the following error message:
argument is of length zero
==> What do I have to do to avoid this error message?
Part of the data:
structure(list(country = c("Afghanistan", "Afghanistan", "Albania",
"Albania", "Algeria", "Algeria", "Andorra", "Andorra", "Angola",
"Angola", "Antigua and Barbuda", "Antigua and Barbuda", "Argentina",
"Argentina", "Armenia", "Armenia", "Australia", "Australia",
"Austria", "Austria", "Azerbaijan", "Azerbaijan", "Bahamas",
"Bahamas", "Bahrain", "Bahrain", "Bangladesh", "Bangladesh",
"Barbados", "Barbados", "Belarus", "Belarus", "Belgium", "Belgium",
"Belize", "Belize", "Benin", "Benin", "Bhutan", "Bhutan", "Bolivia",
"Bolivia", "Bosnia and Herzegovina", "Bosnia and Herzegovina",
"Botswana", "Botswana", "Brazil", "Brazil", "Brunei", "Brunei",
"Bulgaria", "Bulgaria", "Burkina Faso", "Burkina Faso", "Burundi",
"Burundi", "Cambodia", "Cambodia", "Cameroon", "Cameroon", "Canada",
"Canada", "Cape Verde", "Cape Verde", "Central African Republic",
"Central African Republic", "Chad", "Chad", "Chile", "Chile",
"China", "China", "Colombia", "Colombia", "Comoros", "Comoros",
"Congo, Dem. Rep.", "Congo, Dem. Rep.", "Congo, Rep.", "Congo, Rep.",
"Costa Rica", "Costa Rica", "Cote d'Ivoire", "Cote d'Ivoire",
"Croatia", "Croatia", "Cuba", "Cuba", "Cyprus", "Cyprus", "Czech Republic",
"Czech Republic", "Denmark", "Denmark", "Djibouti", "Djibouti",
"Dominica", "Dominica", "Dominican Republic", "Dominican Republic",
"Ecuador", "Ecuador", "Egypt", "Egypt", "El Salvador", "El Salvador",
"Equatorial Guinea", "Equatorial Guinea", "Eritrea", "Eritrea",
"Estonia", "Estonia", "Ethiopia", "Ethiopia", "Fiji", "Fiji",
"Finland", "Finland", "France", "France", "Gabon", "Gabon", "Gambia",
"Gambia", "Georgia", "Georgia", "Germany", "Germany", "Ghana",
"Ghana", "Greece", "Greece", "Grenada", "Grenada", "Guatemala",
"Guatemala", "Guinea", "Guinea", "Guinea-Bissau", "Guinea-Bissau",
"Guyana", "Guyana", "Haiti", "Haiti", "Honduras", "Honduras",
"Hungary", "Hungary", "Iceland", "Iceland", "India", "India",
"Indonesia", "Indonesia", "Iran", "Iran", "Iraq", "Iraq", "Ireland",
"Ireland", "Israel", "Israel", "Italy", "Italy", "Jamaica", "Jamaica",
"Japan", "Japan", "Jordan", "Jordan", "Kazakhstan", "Kazakhstan",
"Kenya", "Kenya", "Kiribati", "Kiribati", "Kuwait", "Kuwait",
"Kyrgyz Republic", "Kyrgyz Republic", "Lao", "Lao", "Latvia",
"Latvia", "Lebanon", "Lebanon", "Lesotho", "Lesotho", "Liberia",
"Liberia", "Libya", "Libya", "Lithuania", "Lithuania", "Luxembourg",
"Luxembourg", "Macedonia, FYR", "Macedonia, FYR", "Madagascar",
"Madagascar", "Malawi", "Malawi", "Malaysia", "Malaysia", "Maldives",
"Maldives", "Mali", "Mali", "Malta", "Malta", "Marshall Islands",
"Marshall Islands", "Mauritania", "Mauritania", "Mauritius",
"Mauritius", "Mexico", "Mexico", "Micronesia, Fed. Sts.", "Micronesia, Fed. Sts.",
"Moldova", "Moldova", "Mongolia", "Mongolia", "Montenegro", "Montenegro",
"Morocco", "Morocco", "Mozambique", "Mozambique", "Myanmar",
"Myanmar", "Namibia", "Namibia", "Nepal", "Nepal", "Netherlands",
"Netherlands", "New Zealand", "New Zealand", "Nicaragua", "Nicaragua",
"Niger", "Niger", "Nigeria", "Nigeria", "North Korea", "North Korea",
"Norway", "Norway", "Oman", "Oman", "Pakistan", "Pakistan", "Palestine",
"Palestine", "Panama", "Panama", "Papua New Guinea", "Papua New Guinea",
"Paraguay", "Paraguay", "Peru", "Peru", "Philippines", "Philippines",
"Poland", "Poland", "Portugal", "Portugal", "Qatar", "Qatar",
"Romania", "Romania", "Russia", "Russia", "Rwanda", "Rwanda",
"Samoa", "Samoa", "Sao Tome and Principe", "Sao Tome and Principe",
"Saudi Arabia", "Saudi Arabia", "Senegal", "Senegal", "Serbia",
"Serbia", "Seychelles", "Seychelles", "Sierra Leone", "Sierra Leone",
"Singapore", "Singapore", "Slovak Republic", "Slovak Republic",
"Slovenia", "Slovenia", "Solomon Islands", "Solomon Islands",
"Somalia", "Somalia", "South Africa", "South Africa", "South Korea",
"South Korea", "South Sudan", "South Sudan", "Spain", "Spain",
"Sri Lanka", "Sri Lanka", "St. Lucia", "St. Lucia", "St. Vincent and the Grenadines",
"St. Vincent and the Grenadines", "Sudan", "Sudan", "Suriname",
"Suriname", "Swaziland", "Swaziland", "Sweden", "Sweden", "Switzerland",
"Switzerland", "Syria", "Syria", "Tajikistan", "Tajikistan",
"Tanzania", "Tanzania", "Thailand", "Thailand", "Timor-Leste",
"Timor-Leste", "Togo", "Togo", "Tonga", "Tonga", "Trinidad and Tobago",
"Trinidad and Tobago", "Tunisia", "Tunisia", "Turkey", "Turkey",
"Turkmenistan", "Turkmenistan", "Uganda", "Uganda", "Ukraine",
"Ukraine", "United Arab Emirates", "United Arab Emirates", "United Kingdom",
"United Kingdom", "United States", "United States", "Uruguay",
"Uruguay", "Uzbekistan", "Uzbekistan", "Vanuatu", "Vanuatu",
"Venezuela", "Venezuela", "Vietnam", "Vietnam", "Yemen", "Yemen",
"Zambia", "Zambia", "Zimbabwe", "Zimbabwe"), Year = c("2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018", "2017", "2018", "2017",
"2018", "2017", "2018", "2017", "2018"), Life_Expectancy = c(58.4,
58.7, 77.9, 78, 77.6, 77.9, NA, NA, 64.9, 65.2, 77.4, 77.6, 76.8,
77, 75.8, 76, 82.7, 82.9, 81.7, 81.8, 72.2, 72.3, 74, 74.1, 77,
77.2, 73.1, 73.4, 76.7, 76.8, 73.6, 73.8, 81.1, 81.2, 72.2, 72.5,
64.7, 65, 74.5, 74.8, 73.6, 74, 77.8, 77.9, 66.3, 66.9, 75.5,
75.7, 77.2, 77.4, 75.2, 75.3, 61.2, 61.6, 60.8, 61.1, 69, 69.3,
60.7, 61.2, 82, 82.2, 73.8, 74.1, 50.9, 51.6, 60.2, 60.5, 80.5,
80.7, 76.8, 76.9, 78.4, 78.6, 67.8, 68, 62, 62.4, 63.4, 63.9,
81.2, 81.4, 60.6, 61.2, 77.5, 77.7, 79.2, 79.3, 80.6, 80.8, 79.2,
79.4, 81, 81.1, 66.9, 67.1, NA, NA, 75.9, 76.1, 78.1, 78.3, 72.4,
72.6, 75.5, 75.8, 65.8, 66.1, 64.1, 64.6, 77.5, 77.7, 66, 66.4,
65.6, 65.8, 81.9, 82.1, 82.5, 82.6, 67, 67.3, 67.6, 67.8, 74.1,
74.3, 81.1, 81.3, 66.3, 66.6, 81.2, 81.3, 71.7, 71.9, 73, 73.2,
61.3, 61.9, 59.3, 59.7, 68, 68.2, 64.3, 64.5, 72.9, 73.1, 75.8,
75.9, 82.4, 82.6, 68.9, 69.1, 71.9, 72, 76.3, 76.5, 67.8, 68,
81.3, 81.5, 82.3, 82.4, 82.5, 82.6, 75, 75.2, 84, 84.2, 76.5,
76.7, 71.8, 72, 67.1, 67.3, 62.1, 62.2, 79.8, 80, 71.3, 71.5,
67.6, 67.9, 75, 75.1, 80.3, 80.5, 50.8, 51.1, 64.9, 65.4, 75.3,
75.5, 75.2, 75.3, 82.2, 82.4, 75, 75.2, 63, 63.4, 60.7, 61, 75.8,
75.9, 79.8, 80.1, 62.4, 62.9, 81.6, 81.8, NA, NA, 70.4, 70.6,
74.8, 74.9, 76.6, 76.8, 65.7, 65.8, 72.3, 72.4, 68.5, 68.7, 77.2,
77.3, 75.2, 75.5, 60.5, 61.1, 70.2, 70.3, 65.3, 65.7, 71.2, 71.5,
81.8, 81.9, 81.6, 81.8, 78.5, 78.7, 62.1, 62.5, 65.6, 66.1, 70.9,
71.1, 82.2, 82.4, 77.8, 78.1, 67.8, 68, 72, 72.2, 79.2, 79.4,
61, 61.1, 74.7, 74.8, 80, 80.2, 70.4, 70.5, 78, 78.2, 81.1, 81.3,
80.6, 80.7, 75.4, 75.5, 71, 71.1, 68, 68.3, 72.2, 72.4, 70.7,
70.9, 77.5, 77.6, 66.5, 66.8, 76, 76.2, 74, 74.2, 59.6, 60, 83.8,
84, 77, 77.2, 81, 81.1, 63.3, 63.6, 57.6, 58, 63, 63.5, 81.2,
81.3, 60.2, 60.7, 83.1, 83.2, 77.6, 77.8, 76.4, 76.6, 71.9, 72,
68.5, 68.8, 71.5, 71.6, 58.2, 58.6, 82.2, 82.4, 83.3, 83.5, 69,
69.8, 72, 72.2, 64.9, 65.5, 78, 78.2, 73, 73.3, 62.9, 63.1, 70.6,
70.7, 73.3, 73.4, 77.7, 78, 79.3, 79.6, 70.4, 70.5, 62.6, 62.9,
72.2, 72.3, 76.7, 76.9, 81, 81.2, 79, 79.1, 77.4, 77.6, 70.4,
70.5, 64.1, 64.3, 75.7, 75.9, 74.7, 74.9, 66.9, 67.1, 59.1, 59.5,
59.8, 60.2)), row.names = c(NA, -374L), class = c("tbl_df", "tbl",
"data.frame"))
The code:
library(shiny)
library(shinydashboard)
library(readxl)
library(plotly)
library(purrr)
library(dplyr)
library(tidyverse)
library(shinyjs)
Global$Year <- as.numeric(as.character(Global$Year))
GlobalLifeExp <- function(land) {
df <- Global %>%
filter(country %in% c(land)) %>%
filter(Year >= 1870)
}
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
selectInput("Country", label = "Please choose the countries you want to compare:", c("Afghanistan",
"Albania",
"Algeria",
"Andorra",
"Angola",
"Antigua and Barbuda",
"Argentina",
"Armenia",
"Australia",
"Austria",
"Azerbaijan",
"Bahamas",
"Bahrain",
"Bangladesh",
"Barbados",
"Belarus",
"Belgium",
"Belize",
"Benin",
"Bhutan",
"Bolivia",
"Bosnia and Herzegovina",
"Botswana",
"Brazil",
"Brunei",
"Bulgaria",
"Burkina Faso",
"Burundi",
"Cambodia",
"Cameroon",
"Canada",
"Cape Verde",
"Central African Republic",
"Chad",
"Chile",
"China",
"Colombia",
"Comoros",
"Congo, Dem. Rep.",
"Congo, Rep.",
"Costa Rica",
"Cote d'Ivoire",
"Croatia",
"Cuba",
"Cyprus",
"Czech Republic",
"Denmark",
"Djibouti",
"Dominica",
"Dominican Republic",
"Ecuador",
"Egypt",
"El Salvador",
"Equatorial Guinea",
"Eritrea",
"Estonia",
"Ethiopia",
"Fiji",
"Finland",
"France",
"Gabon",
"Gambia",
"Georgia",
"Germany",
"Ghana",
"Greece",
"Grenada",
"Guatemala",
"Guinea",
"Guinea-Bissau",
"Guyana",
"Haiti",
"Honduras",
"Hungary",
"Iceland",
"India",
"Indonesia",
"Iran",
"Iraq",
"Ireland",
"Israel",
"Italy",
"Jamaica",
"Japan",
"Jordan",
"Kazakhstan",
"Kenya",
"Kiribati",
"Kuwait",
"Kyrgyz Republic",
"Lao",
"Latvia",
"Lebanon",
"Lesotho",
"Liberia",
"Libya",
"Lithuania",
"Luxembourg",
"Macedonia, FYR",
"Madagascar",
"Malawi",
"Malaysia",
"Maldivdes",
"Mali",
"Malta",
"Marshall Islands",
"Mauritania",
"Mauritius",
"Mexico",
"Micronesia, Fed. Sts.",
"Moldova",
"Mongolia",
"Montenegro",
"Morocco",
"Mozambique",
"Myanmar",
"Namibia",
"Nepal",
"Netherlands",
"New Zealand",
"Nicaragua",
"Niger",
"Nigeria",
"North Korea",
"Norway",
"Oman",
"Pakistan",
"Palestine",
"Panama",
"Papua New Guinea",
"Paraguay",
"Peru",
"Philippines",
"Poland",
"Portugal",
"Qatar",
"Romania",
"Russia",
"Rwanda",
"Samoa",
"Sao Tome and Principe",
"Saudi Arabia",
"Senegal",
"Serbia",
"Seychelles",
"Sierra Leone",
"Singapore",
"Slovak Republic",
"Slovenia",
"Solomon Islands",
"Somalia",
"South Africa",
"South Korea",
"South Sudan",
"Spain",
"Sri Lanka",
"St. Lucia",
"St. Vincent and the Grenadines",
"Sudan",
"Suriname",
"Swaziland",
"Sweden",
"Switzerland",
"Syria",
"Tajikistan",
"Tanzania",
"Thailand",
"Timor-Leste",
"Togo",
"Tonga",
"Trinidad and Tobago",
"Tunisia",
"Turkey",
"Turkmenistan",
"Uganda",
"Ukraine",
"United Arab Emirates",
"United Kingdom",
"United States",
"Uruguay",
"Uzbekistan",
"Vanuatu",
"Venezuela",
"Vietnam",
"Yemen",
"Zambia",
"Zimbabwe"), selected = "Switzerland", multiple = TRUE,
selectize = TRUE, width = NULL, size = NULL),
plotlyOutput(outputId = "HLE", height = 500)
# closing Tab items
) # closing dashboard body
ui <- shinyUI(dashboardPage(skin = "black",
header = header,
sidebar = sidebar,
body = body,
useShinyjs()
))
server <- function(input, output){
GlobalLifeExpG <- reactive({
GlobalLifeExp(input$Country)
})
output$HLE <- renderPlotly({
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
GlobalLifeExpG() %>%
accumulate_by(~Year) %>%
plot_ly(x = ~Year, y = ~Life_Expectancy,
split = ~country,
frame = ~frame,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste("Year:", Year, "<br>Life Expectancy:", Life_Expectancy)) %>%
animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
) %>%
animation_slider(
hide = T
)
})
addClass(selector = "body", class = "sidebar-collapse")
}
shinyApp(ui = ui, server = server)
...