MikeHealth21
MikeHealth21

Reputation: 35

Getting Leaflet to work with Datatable selection in R shiny

I have a Massmpg dataset just like the mpg dataset from ggplot but with the addition of City (from Massachusetts) , lat, and lng attributes. I have made shiny app which lets me select the city in a datatable but I cannot figure out how to get the leaflet map to focus only on the lat and lng in that city. Instead the map just shows all the locations in the state.

Here is the code

library(shiny)
library(DT)
library(leaflet)
Massmpg <- (read.csv("Massmpg.csv"))

ui <- fluidPage(titlePanel("Mass mpg by location"),
                
                # Create a new Row in the UI for selectInputs
                fluidRow(
                    column(4,
                           selectInput("City",
                                       "City:",
                                       c("All",
                                         unique(as.character(Massmpg$City))))
                    ),
                    column(4,
                           selectInput("cyl",
                                       "cyl:",
                                       c("All",
                                         unique(as.character(Massmpg$cyl))))
                    ),
                    column(4,
                           selectInput("trans",
                                       "trans:",
                                       c("All",
                                         unique(as.character(Massmpg$trans))))
                    )
                ),
                # Create a new row for the table.
                leafletOutput("map01"),
                DT::dataTableOutput("table")
                
)

server <- function(input, output) {
        
        # Filter data based on selections
        output$table <- DT::renderDataTable(DT::datatable({
            data <- Massmpg
            if (input$City != "All") {
                data <- data[data$City == input$City,]
            }
            if (input$cyl != "All") {
                data <- data[data$cyl == input$cyl,]
            }
            if (input$trans != "All") {
                data <- data[data$trans == input$trans,]
            }
            data
        }))
        
        # map
        output$map01 <- renderLeaflet({
            #pal <- colorNumeric("YlOrRd", domain=c(min(quakes$mag), max(quakes$mag)))
            qMap <- leaflet(data = (Massmpg)) %>% 
                addTiles() %>%
                addCircles(radius =3, color="red")
            qMap
        })     
        
    }


shinyApp(ui = ui, server = server)

Matt thak you for your answer on reactive object. I am trying it but not getting it to work. Here is my code with changes below. I have links to the shinyapps page https://michaelcardio2020.shinyapps.io/TestFrame2/ and the Massmpg data in Box https://app.box.com/s/ryowy2x0h2owm3b1s18ok3342p5gharu

library(shiny)
library(DT)
library(leaflet)
Massmpg <- (read.csv("Massmpg.csv"))


# Define UI for application that draws a histogram
ui <- fluidPage(titlePanel("Mass mpg by location"),
                
                # Create a new Row in the UI for selectInputs
                fluidRow(
                    column(4,
                           selectInput("City",
                                       "City:",
                                       c("All",
                                         unique(as.character(Massmpg$City))))
                    ),
                    column(4,
                           selectInput("cyl",
                                       "cyl:",
                                       c("All",
                                         unique(as.character(Massmpg$cyl))))
                    ),
                    column(4,
                           selectInput("trans",
                                       "trans:",
                                       c("All",
                                         unique(as.character(Massmpg$trans))))
                    )
                ),
                # Create a new row for the table.
                leafletOutput("map01"),
                DT::dataTableOutput("table")
                
)


    
server <- function(input, output) {
    
    data <- reactive({
        x <- Massmpg
    }) 
        
        # Filter data based on selections
        output$table <- DT::renderDataTable(DT::datatable({
            data <- Massmpg
            if (input$City != "All") {
                data <- data[data$City == input$City,]
            }
            if (input$cyl != "All") {
                data <- data[data$cyl == input$cyl,]
            }
            if (input$trans != "All") {
                data <- data[data$trans == input$trans,]
            }
            data
        }))
        
        # map
        output$map01 <- renderLeaflet({
            Massmpg <- data()
            #pal <- colorNumeric("YlOrRd", domain=c(min(quakes$mag), max(quakes$mag)))
            qMap <- leaflet(data = (Massmpg)) %>% 
                addTiles() %>%
                addCircles(radius =3, color="red")
            qMap
        })
        
    }

# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 873

Answers (1)

Matt Gossett
Matt Gossett

Reputation: 184

You need to reference a reactive element in your leaflet map output. You can solve your issue with the following:

  1. Create reactive object that mirrors your data table object
  2. Reference that reactive object in both your data table and leaflet map

Make sure that call your reactive element with (). For example, if « table » is the name of your reactive, call it with table() in your output objects.

Upvotes: 1

Related Questions