I am working on a shiny dashboard project to visualize data from the World Bank. In total i need 14 different variables that I want to show in a data table. It works in R but when I try to do it in shiny I get a connection error. I think it takes too much time to download. Is there a way to load the data more efficient or in background or whatever? I can't find a solution to make it run and I don't want to store the data in a file on the server because then I don't have an automatic update... :/
Here's a minimal vital code snippet
library(shiny)
library(dplyr)
library(WDI)
library(wbstats)
library(lubridate)
ui <- fluidPage(
titlePanel('WDI Example'),
mainPanel(tableOutput('table'))
)
server <- function(input, output) {
startDate <- as.numeric(format(Sys.Date(), "%Y")) -20
endDate <- as.numeric(format(Sys.Date(), "%Y"))
fetch.data <- list()
list <- c("SP.POP.TOTL",
"NY.ADJ.NNTY.PC.CD",
"SH.XPD.CHEX.PC.CD",
"SH.XPD.OOPC.CH.ZS",
"SH.MED.BEDS.ZS",
"SH.MED.PHYS.ZS",
"NY.GDP.MKTP.CD",
"NY.GDP.MKTP.KD",
"NY.GDP.MKTP.KD.ZG",
"SH.XPD.CHEX.GD.ZS",
"SP.DYN.LE00.IN",
"SP.POP.65UP.TO",
"SP.POP.DPND",
"SP.POP.DPND.OL")
for (i in 1:length(list)){
fetch.data[[i]] <- WDI(indicator = as.character(list[i]),
start = startDate,
end = endDate,
country = "all",
extra = TRUE)
}
data <- fetch.data
for (i in 1:length(list)){
data[[i]] <- subset(data[[i]], region != "Aggregates")
data[[i]] <- data[[i]] %>% select(country, iso3c, region, year, noquote(list[i]))
data[[i]]$date <- as.Date(as.character(date), format = "%Y")
data[[i]] <- data[[i]][complete.cases(data[[i]][ ,5]),]
data[[i]] <- data[[i]] %>% group_by(country) %>% arrange(desc(date)) %>% slice(1)
}
indicators <- fetch.data[[1]] %>%
select(country, iso3c, region) %>%
subset(region != "Aggregates") %>%
arrange(desc(country)) %>%
group_by(country) %>%
slice(1)
for (i in 1:length(list)){
indicators <- merge(indicators, data[[i]][ ,c(2,5)], by.x = "iso3c", by.y = "iso3c", all = TRUE)
}
indicators <- indicators %>%
arrange(country) %>%
mutate_if(is.numeric, round, 2)
output$table <- renderTable(indicators)
}
shinyApp(ui = ui, server = server)
and here is an example that does not work… https://thera-trainer.shinyapps.io/example/
Thanks for advice