I have an app that displays countries’ vulnerability to climate change by year and category. The countries are in color according to their vulnerability ranking among the countries. It works fine in that when you hover over a country, it shows you its name and ranking.
But it shows nothing when you hover over one of the countries that are white – those countries have an NA ranking because there’s not enough data. What I would like is for when you hover over those countries, it shows its name and “Insufficient data”.
This 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 = "")
# Prepare the data with tooltips
map_data <- combined_vul_data %>%
select(ISO3, Name, Rank = !!sym(rank_column)) %>%
mutate(Tooltip = ifelse(is.na(Rank), paste(Name, " - Insufficient data"), paste(Name, " - Rank:", 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 = "Name", colorvar = "Rank",
options = list(colorAxis = paste0("{minValue:", min_rank, ", maxValue:", max_rank, ", colors:['#00FF00', '#FFFF00', '#FF0000']}"),
backgroundColor = '#81d4fa', datalessRegionColor = '#f5f5f5',
defaultColor = '#f5f5f5',
tooltip = "{isHtml: 'true'}"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
The dataframe is too big to dput – open to other suggestions. Is there something really obvious I’m doing wrongly with the tooltip?