R闪亮传单中的点击表未显示 [英] Table on click in R Shiny leaflet is not shown
问题描述
我想得到一张桌子,单击Shiny应用程序中的传单地图上的标记.但是:
I wanna get a table clicking on the marker on leaflet map in Shiny app. But:
Warning: Error in ==: comparison (1) is possible only for atomic and list types
[No stack trace available]
我一次又一次收到此错误.这是我的代码.我的renderTable()
出了点问题.
I get this error again and again. Here is my code. Something is wrong with my renderTable()
, I guess.
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)
一直在工作.
Can the problem be related to some errors in my packages and R session? I seemed to get similar issue trying to find solutions for dplyr::select
, while it didnt work without strict reference to the package. Herewith, library(tidyverse)
was always working.
推荐答案
为使此功能正常工作,我可能需要做一些小事情.
I there may be a few minor things to get this working.
首先,dplyr中的select语句删除您的id变量.看起来它在第14列中,如果要按ID进行过滤/子集设置,则应将其包括在内.
First, the select statement in dplyr removes your id variable. Looks like it's in column 14 and should be included if you want to filter/subset by id.
第二,id必须包含在addCircleMarkers方法中,以便可以通过data_of_click $ clickMarker对其进行访问.添加layerId = botsad.final $ id以包含ID.
Second, id needs to be included in the addCircleMarkers method so it can be accessed through data_of_click$clickMarker. Add layerId=botsad.final$id to include the id.
第三,在选择标记之前,data_of_click为NULL.在呈现表格之前将检查NULL.
Third, data_of_click is NULL before a marker is selected. Would check for NULL before rendering table.
让我知道这是否可行,并且是您的初衷.
Let me know if this works and is what you had in mind.
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)
这篇关于R闪亮传单中的点击表未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!