【问题标题】:Using R shiny with Leaflet and DataTable with reactive object将 R Shiny 与 Leaflet 和 DataTable 与反应性对象一起使用
【发布时间】:2021-05-20 21:51:52
【问题描述】:

我对两天前在另一篇文章中提出的问题做了一个非常简化的版本 (Getting Leaflet to work with Datatable selection in R shiny) 我更接近解决方案。在 MA 的 4 个城市中有一个包含 12 个位置的 R 数据框。 dataTable 让用户选择城市,而传单地图将仅显示所选城市中的位置。

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

City <- c("Boston","Boston", "Boston", "Lowell","Lowell", "Lowell","Worcestor", "Worcestor","Worcestor","Springfield","Springfield","Springfield")

lat <- c(42.35, 42.355, 42.345, 42.63,42.625,42.635,42.27,42.265,42.275, 42.1,42.105,42.095)

lng <- c(-71.05,-71.045,-71.055,-71.316,-71.315,-71.317,-71.79,-71.785,-71.795,-72.6,-72.595,-72.605)

MassLocations <- data.frame(City, lat,lng)


# MassLocations has 4 cities with 3 locations each
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(MassLocations$City))))
                    ),
                            ),
                # Create a new row for the table.
                leafletOutput("map01"),
                DT::dataTableOutput("table")
                
)


server <- function(input, output) {
    
    dataShow <- reactive({
        DT::renderDataTable(DT::datatable({
            data <- MassLocations
            if (input$City != "All") {
                data <- data[data$City == input$City,]
                
            }
            data
        }))
    })
    

        # Filter data based on selections
        output$table <- dataShow()
        
        
        # map
        output$map01 <- renderLeaflet({
            #pal <- colorNumeric("YlOrRd", domain=c(min(quakes$mag), max(quakes$mag)))
            qMap <- leaflet(data = (dataShow())) %>% 
                addTiles() %>%
                addCircles(radius =3, color="red")
            qMap
        })
        
} 
        
# Run the application 
shinyApp(ui = ui, server = server)  '''

不幸的是,在闪亮的窗口首次打开然后崩溃后,我收到了错误消息。错误是 收听http://127.0.0.1:7312 *警告:错误:在没有主动反应上下文的情况下不允许操作。

  • 您试图做一些只能在反应式消费者内部完成的事情。 55: 错误:如果没有活动的反应上下文,则不允许操作。
  • 您试图做一些只能在反应式消费者内部完成的事情。*

【问题讨论】:

    标签: r shiny datatable leaflet reactive


    【解决方案1】:

    如果您将renderDataTable() 放在reactive() 之外,它可以正常工作

    server <- function(input, output) {
      # Filter data based on selections
      dataShow <- reactive({
          data <- MassLocations
          if (input$City != "All") {
            data <- data[data$City == input$City, ]
          }
          data
        })
    
      # Display
      output$table <- DT::renderDataTable(
        DT::datatable(dataShow()))
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 2018-11-18
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 2021-04-16
      • 2014-11-06
      相关资源
      最近更新 更多