0

I'd like to construct a shiny app with leaflet displaying the locations of Ukraininan power plants. In the UI users shall have the option to select the plants shown based on the fuel type (e.g. nuclear, coal, hydro, wind, solar, gas). The data are from the global power plant database:

Global PP Database

The sidebar panel, as well as the leaflet map are displayed correctly, yet the app is not reactive, i.e. the markers do not change based on the inputs in the sidebar panels, but are statically shown all at once, reagardless of the UI input (see screenshot below).

There is probbaly some mistake in the server function of my code. I am new to shiny programming, so it can be a very basic mistake.

library(shiny)
library(dplyr)
library(leaflet)

UkraineLatitude<-48.383022
UkraineLongitude<-31.1828699

DS_Power_Plants<-read.csv("xxx")
Power_Plants_UKR<-filter(DS_Power_Plants, country=="UKR")

ui <- fluidPage(
  titlePanel("Power Plant map of Ukraine"),
  sidebarLayout(
    sidebarPanel(
      selectInput("PlantType",
                  "Power Plants by fuel type",
                  choices = Power_Plants_UKR$primary_fuel,
                  selected=NULL,
                      multiple=TRUE),
    ),
                  
    
    mainPanel(leafletOutput(outputId = "Plantmap"))
  )
)


server <- function(input, output) {
  
  reactive({
   Power_Plants_UKR%>%filter(Power_Plants_UKR$primary_fuel,input$PlantType)
  })

  output$Plantmap<-renderLeaflet({
    leaflet(Power_Plants_UKR)%>%
       addTiles()%>%addMarkers()
  })
}

shinyApp(ui = ui, server = server)

Somehow there seems to be a problem with linking the reactive function and the renderLeaflet function.

UI_Screesnhot

Luke
  • 11
  • 2

1 Answers1

1

You should create a new reactive object from your filtered dataframe and pass that to the plotting function:

library(shiny)
library(dplyr)
library(leaflet)

UkraineLatitude<-48.383022
UkraineLongitude<-31.1828699



DS_Power_Plants<-read.csv("global_power_plant_database.csv")
Power_Plants_UKR<-filter(DS_Power_Plants, country=="UKR")

ui <- fluidPage(
  titlePanel("Power Plant map of Ukraine"),
  sidebarLayout(
    sidebarPanel(
      selectInput("PlantType",
                  "Power Plants by fuel type",
                  choices = Power_Plants_UKR$primary_fuel,
                  selected=NULL,
                  multiple=TRUE),
    ),
    
    
    mainPanel(leafletOutput(outputId = "Plantmap"))
  )
)


server <- function(input, output) {
  
  plants_to_plot <- reactive({
    Power_Plants_UKR%>%filter(primary_fuel %in% input$PlantType)
  })
  
  output$Plantmap<-renderLeaflet({
    leaflet(plants_to_plot())%>%
      addTiles()%>%addMarkers()
  })
}

shinyApp(ui = ui, server = server)

Resulting in:

Andy Baxter
  • 5,833
  • 1
  • 8
  • 22