r - 如何在 R Shiny 中为 Mapdeck add_scatterplot 使用多个数据框输入?
问题描述
在 r shiny 中处理大型数据集时,我发现Mapdeck是一种非常酷的替代映射方法。但是,我发现很难传递多个数据集以扩展结果地图的复杂性。
例如:我试图通过从第二个数据框中提取信息来填充工具提示以创建一个带有“两个”项目的弹出窗口(以说明一对多的关系)。但我无法让它工作。有什么想法吗?
数据样本
Map_DF <- data.frame("Point_ID" = 1:3, "Latitude" = c(38.05, 39.08, 40.05), "Longitude" = c(-107.00, -107.05, -108.00))
PointUse_DF <- data.frame("Point_ID" = c(1, 1, 2, 2, 3), "PointUse" = c("farm", "house", "farm", "house", "farm"))
编码。
# ################################################################################################
# ################################################################################################
# # Sec 1a. Needed Libaries & Input Files
library(shiny)
library(shinydashboard)
library(mapdeck)
library(dplyr)
################################################################################################
################################################################################################
#UI
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
mapdeckOutput(outputId = 'mapA')
)
)
################################################################################################
################################################################################################
server <- function(input, output) {
##The MapDeck Token
key <- '## put your own token here ##'
set_token(key) ## set your access token
### The Map
output$mapA <- renderMapdeck({
mapdeck() %>%
add_scatterplot(
data = Map_DF,
lat = "Latitude",
lon = "Longitude",
layer_id = 'Point_ID',
tooltip = c("Point_ID",
PointUse_DF$PointUse[Map_DF$Point_ID] # the idea of passing in a a key and returning multiple items
)
)
})
}
################################################################################################
################################################################################################
shinyApp(ui = ui, server = server)
解决方案
data
工具提示在弹出窗口内呈现对象的一列。因此,无论您想在弹出窗口中显示什么,都必须已经存在于数据中
使用您的示例,您需要创建工具提示列
# Using data.table to rshape the data to give a comma-separated list of 'PointUse' values
library(data.table)
dt <- setDT( PointUse_DF )[, .(info = paste0(PointUse, collapse = ", ")), by = Point_ID]
## Now you can put this onto the map object
Map_DF <- merge(
x = Map_DF
, y = dt
, by = "Point_ID"
, all.x = T
)
mapdeck() %>%
add_scatterplot(
data = Map_DF,
lat = "Latitude",
lon = "Longitude",
layer_id = 'Point_ID',
radius_min_pixels = 3,
tooltip = "info"
)
)
推荐阅读
- ios - iOS:CGContextStrokePath(context) 在 iOS 12 中因 EXC_BAD_ACCESS 而崩溃
- php - facebook 上的文件夹图像共享给出错误“href 应该代表一个有效的 URL”?
- node.js - 在本地主机上运行 Node.Js
- html - 波兰语 ejs 中的特定字符
- reactjs - 在反应中动态添加组件的最简单方法
- linux - nlohmann json 有一个名为 name 的字符串成员,我如何检查它是 null 还是有效字符串
- github - 如何在 Flutter 中添加来自 GitHub 的包?
- python - 谷歌云平台:我如何获得一个签名的 URL 来使用 Python 将一个对象放到谷歌云商店
- unix - 修剪 Awk 中的前导和尾随空格
- react-native - React-native QR 码扫描仪抛出错误