0

I am fairly new to R Shiny and I've been working on an app with an interactive world map which shows each country's performance at the Olympics, using scale_fill_gradient. The app user gets to choose the performance indicator (total medals won, gold only, weighted score) and the year (1996 to 2020).

The problem is there's no more error shown, but the plot doesn't show either! I have tried to run the functions as normal R script and they worked fine there (the plot showed up in the viewer pane). I found a few others who have also run into problems with no plot or error showing, but their cases are different to mine (e.g. mismatch in Output and Render) so the resolutions don't work for me.

It's a massive dataset so I've not included it here, I thought I might check first if the error could be spotted from the code alone. Here's what I've used:

function

world_map1 <- function(WorldMap, year, performance) {
  
  w_plot1 <- WorldMap %>%
    filter(Year == year) %>%
    select("long", "lat", "group", "region", all_of(performance)) %>%
    replace(is.na(.), 0) %>%
    rename_at(performance, ~ "Value") %>%
    mutate(Value = as.numeric(as.character(Value)))
  
  tooltip_css <- "background-color:#2E2E2E; font-family: Calibri; color:#F2F2F2;"
  
  w_g1 <- ggplot() +
    geom_polygon_interactive(data = subset(w_plot1, lat >= -60 & lat <= 90), 
                             aes(x = long, 
                                 y = lat, 
                                 fill = Value, 
                                 group = group,
                                 tooltip = sprintf("%s<br/>%s", region, Value))) +
    scale_fill_gradient(name = "Medals /Score",
                        low = "lightgoldenrodyellow",
                        high = "goldenrod1",
                        na.value = "white") 
  
  return(
    girafe(
      ggobj = w_g1,
      options = list(
        opts_tooltip(
          css = tooltip_css
        )
      ))
  )
}

ui

ui <- fluidPage(
  titlePanel("Title"),
  sidebarLayout(
    sidebarPanel(
      radioButtons(inputId = "performance", label = "Performance measure:",
                   choices = c("Total medals won" = "Total",
                               "Gold medals won" = "Gold",
                               "Weighted points system" = "Weighted"
                   )),
      width = 3
      
    ),
    mainPanel(
      girafeOutput("mapPlot1"),
      sliderInput(inputId = "year", label = "Year:",
                  min = 1996, max = 2020, step = 4, value = 1996, ticks = FALSE, sep = ""
      )
    )
  )
)

server

server <- function(input, output) {
  output$mapPlot1 <- renderGirafe({
      ggiraph(code = print(world_map1(WorldMap, input$year, input$performance)))
    } 
    
  )
}

run app

shinyApp(ui = ui, server = server)

Any help or insights appreciated!

I thought it was my theme() block so I removed that, as shown above. Also checked other cases on no plot showing here, couldn't find one with fixes that would work for me because it seems the underlying problem is different?

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
fmoddy
  • 3
  • 2

0 Answers0