使Leaflet与R Shiny中的数据表选择一起使用 [英] Getting Leaflet to work with Datatable selection in R shiny

查看:47
本文介绍了使Leaflet与R Shiny中的数据表选择一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Massmpg数据集,就像ggplot的mpg数据集一样,但增加了City(来自马萨诸塞州),lat和lng属性.我制作了闪亮的应用程序,可让我在数据表中选择城市,但我无法弄清楚如何使传单地图仅关注该城市的经纬度.取而代之的是,地图仅显示该州的所有位置.

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.

这是代码

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)想念您对反应性物体的回答.我正在尝试,但没有使其正常工作.这是我的代码,下面有更改.我具有指向Shinyapps页面 https://michaelcardio2020.shinyapps.io/TestFrame2/的链接,框 https://app.box.com/s/ryowy2x0h2owm3b1s18ok3342p5gharu

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)

推荐答案

您需要在传单地图输出中引用反应性元素.您可以通过以下方法解决您的问题:

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

  1. 创建反映您的数据表对象的反应式对象
  2. 在数据表和传单地图中同时引用该反应性对象

确保使用()调用反应性元素.例如,如果«table»是您的反应式的名称,则在输出对象中使用table()对其进行调用.

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.

这篇关于使Leaflet与R Shiny中的数据表选择一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆