I want to make an app that shows the world map. Each country should be displayed in a color according to its vulnerability in a particular year in a particular category. The select buttons show fine, the color bar and numbers either side are correct. But I can’t get the countries to change color. It just looks like this right now:
Here is my code:
# Define UI
ui <- fluidPage(
titlePanel("Climate Vulnerability and Readiness"),
sidebarLayout(
sidebarPanel(
selectInput("year", "Select Year", choices = 1995:2021, selected = 2021),
selectInput("category", "Select Category",
choices = list("Capacity" = "vul_capacity", "Economic" = "vul_economic",
"Ecosystems" = "vul_ecosystems", "Exposure" = "vul_exposure",
"Food" = "vul_food", "Habitat" = "vul_habitat",
"Health" = "vul_health", "Infrastructure" = "vul_infrastructure",
"Sensitivity" = "vul_sensitivity", "Vulnerability" = "vul_vulnerability",
"Water" = "vul_water"), selected = "vul_capacity")
),
mainPanel(
htmlOutput("map")
)
)
)
# Define server logic
server <- function(input, output) {
output$map <- renderGvis({
year <- input$year
category <- input$category
# Create the column name for the selected year and category
rank_column <- paste(category, "_Rank_", year, sep = "")
# Filter and prepare the data
map_data <- combined_vul_data %>%
select(ISO3, Name, Rank = !!sym(rank_column)) %>%
filter(!is.na(Rank))
# Determine the range of the Rank values
min_rank <- min(map_data$Rank, na.rm = TRUE)
max_rank <- max(map_data$Rank, na.rm = TRUE)
# Create the map
gvisGeoChart(map_data, locationvar = "ISO3", colorvar = "Rank",
options = list(colorAxis = paste0("{minValue:", min_rank, ", maxValue:", max_rank, ", colors:['#00FF00', '#FFFF00', '#FF0000']}"),
backgroundColor = '#81d4fa', datalessRegionColor = '#f5f5f5',
defaultColor = '#f5f5f5'))
})
}
# Run the application
shinyApp(ui = ui, server = server)
The dataframe is really big so I’m not sure what to share for dput – am open to ideas. I’m hoping there’s just something really obviously wrong with my code that’s noticeable and a quick fix.