R Shiny 传单中单击时的表格未显示

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

我想要一张桌子,点击闪亮应用程序中传单地图上的标记。但是:

Warning: Error in ==: comparison (1) is possible only for atomic and list types
      [No stack trace available]

我一次又一次地收到此错误。这是我的代码。我猜我的

renderTable()
有问题。

botsad.final <- read_csv("https://raw.githubusercontent.com/Janzeero-PhD/Botanical-Garden-of-NULES/master/botsad_final.csv")

server <- function(input, output) {
  # create a reactive value that will store the click position
  data_of_click <- reactiveValues(clickedMarker=NULL)

  # Leaflet map
  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles(options = providerTileOptions(noWrap = TRUE)) %>%
      addCircleMarkers(data = botsad.final,
                       lng = ~ X, lat = ~ Y, radius = 3,
                       popup = paste0("<img src = ", botsad.final$link_4, " />"),
                       color="black",  fillColor="red", stroke = F, 
                       fillOpacity = 0.5,
                       label = botsad.final$species_5)
  })

  # store the click
  observeEvent(input$map_marker_click, {
    data_of_click$clickedMarker <- input$map_marker_click
  })

  # Make a barplot or scatterplot depending of the selected point
  output$table <- renderTable({
      return(
        subset(botsad.final %>%
              dplyr::select(3, 7:12), 
              id == data_of_click$clickedMarker$id
              )
      )
    })
}

ui <- fluidPage(
  br(),
  column(8, leafletOutput("map", height = "600px")),
  column(4, br(), br(), br(), br(), tableOutput("table")),
  br()
)

shinyApp(ui = ui, server = server)

问题是否与我的软件包和 R 会话中的某些错误有关?我似乎遇到了类似的问题,试图找到

dplyr::select
的解决方案,但如果没有严格参考包,它就无法工作。至此,
library(tidyverse)
一直在工作。

r shiny r-leaflet
1个回答
2
投票

我可能需要一些小事情才能使其正常工作。

首先,dplyr 中的 select 语句删除您的 id 变量。看起来它在第 14 列中,如果您想按 id 过滤/子集,则应将其包含在内。

其次,id 需要包含在 addCircleMarkers 方法中,以便可以通过 data_of_click$clickMarker 访问它。添加 layerId=botsad.final$id 以包含 id。

第三,在选择标记之前,data_of_click 为 NULL。在渲染表格之前会检查 NULL。

让我知道这是否有效并且是您的想法。

library(tidyverse)
library(leaflet)

botsad.final <- read_csv("https://raw.githubusercontent.com/Janzeero-PhD/Botanical-Garden-of-NULES/master/botsad_final.csv")

server <- function(input, output) {
  # create a reactive value that will store the click position
  data_of_click <- reactiveValues(clickedMarker=NULL)

  # Leaflet map
  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles(options = providerTileOptions(noWrap = TRUE)) %>%
      addCircleMarkers(data = botsad.final,
                       lng = ~ X, lat = ~ Y, radius = 3,
                       popup = paste0("<img src = ", botsad.final$link_4, " />"),
                       color="black",  fillColor="red", stroke = F, 
                       fillOpacity = 0.5,
                       label = botsad.final$species_5,
                       layerId = botsad.final$id
      )
  })

  # store the click
  observeEvent(input$map_marker_click, {
    data_of_click$clickedMarker <- input$map_marker_click
  })

  # Make a barplot or scatterplot depending of the selected point
  output$table <- renderTable({
    if (is.null(data_of_click$clickedMarker)) {
      return(NULL)
    }
    return(
      subset(botsad.final %>%
               dplyr::select(3, 7:12, 14), 
                 id == data_of_click$clickedMarker$id
      )
    )
  })
}

ui <- fluidPage(
  br(),
  column(8, leafletOutput("map", height = "600px")),
  column(4, br(), br(), br(), br(), tableOutput("table")),
  br()
)

shinyApp(ui = ui, server = server)
© www.soinside.com 2019 - 2024. All rights reserved.