可反应桌子R Shiny内的传单地图

问题描述 投票:0回答:1

我想将地图包含在可反应表格的一列中,每个地图将占据表格内的一个单元格。 像这样的东西:

enter image description here

我试过这个:

library(shiny)
library(reactable)
library(leaflet)

ui <- fluidPage(
  mainPanel(
    reactableOutput("table")  
  )
)

server <- function(input, output, session) {
  df_aus <- data.frame(country = "australia",
                        latitude = -35.31,
                        longitude = 149.13)
  
  df_belg <- data.frame(country = "belgium",
                         latitude = 50.83,
                         longitude = 4.33)
  
  map_aus = leaflet(df_aus) %>%
    addProviderTiles("CartoDB.Positron")%>%
    addMarkers(
      label = ~ country,
      layerId = ~ country
      )
  map_belg = leaflet(df_belg) %>%
    addProviderTiles("CartoDB.Positron")%>%
    addMarkers(
      label = ~ country,
      layerId = ~ country
    )
  
  df <- data.frame(country = c("australia","belgium"),
                   values = c(10, 20),
                   map = c(map_aus,map_belg))
  
  output$table <- renderReactable({
    reactable(df)
  })
}

shinyApp(ui, server)

然而,即使这两个地图都有效,这也会返回错误。我想我可能必须在列中添加

renderLeaflet()
,但我不确定在哪里。

我也尝试像这样添加

renderLeaflet()
:但它也不起作用。

  df <- data.frame(country = c("australia","belgium"),
                   values = c(10, 20),
                   map = c(leafletOutput('map_1'), leafletOutput('map_2'))
                   )
  
  output$map_1 <- renderLeaflet(map_aus)
  output$map_2 <- renderLeaflet(map_belg)
r shiny r-leaflet reactable
1个回答
2
投票

你需要这样做:

library(shiny)
library(reactable)
library(leaflet)

ui <- fluidPage(
    mainPanel(
        reactableOutput("table")  
    )
)

server <- function(input, output, session) {
    df_aus <- data.frame(country = "australia",
                         latitude = -35.31,
                         longitude = 149.13)
    
    df_belg <- data.frame(country = "belgium",
                          latitude = 50.83,
                          longitude = 4.33)
    
    map_aus = leaflet(df_aus, height = "100px") %>%
        addProviderTiles("CartoDB.Positron")%>%
        addMarkers(
            label = ~ country,
            layerId = ~ country
        )
    map_belg = leaflet(df_belg, height = "100px") %>%
        addProviderTiles("CartoDB.Positron")%>%
        addMarkers(
            label = ~ country,
            layerId = ~ country
        )
    maps <- list(map_aus, map_belg)
    df <- data.frame(country = c("australia","belgium"),
                     values = c(10, 20),
                     map = NA)
    
    output$table <- renderReactable({
        reactable(data = df, columns = list(
            map = colDef(cell = function(value, index) {
                maps[[index]]
            })
        ), )
    })
}

shinyApp(ui, server)

要定义自定义列,您需要使用

colDef
并为此列定义一个函数。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.