我启动了一个带有 ggplot 世界地图的闪亮应用程序。我想获取绘图上点击的坐标,以便用户可以对地图进行操作,但坐标很奇怪(要么NULL
或非常小的东西)。重复点击似乎只会改变一个坐标:
ui.R:
library(shiny)
# Define UI for application
shinyUI(pageWithSidebar(
# Application title
headerPanel("My App"),
sidebarPanel(
textOutput("clickcoord")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("myworld", height="600px", clickId="plotclick")
)
))
服务器.R:
library(shiny)
library(maps)
library(mapdata)
library(ggplot2)
library(rworldmap)
shinyServer(function(input, output) {
output$myworld <- renderPlot({
world <- map_data("world")
worldmap <- ggplot(aes(x = long, y = lat, group = group), data = world) +
geom_path()
print(worldmap)
})
output$clickcoord <- renderPrint({
print(input$plotclick)
})
})
如果我只是使用map()
命令生成非 ggplot 世界地图,我得到了看起来不错的点击坐标纬度/经度值:
服务器.R(修改):
library(shiny)
library(maps)
library(mapdata)
shinyServer(function(input, output) {
output$myworld <- renderPlot({
map("world2Hires")
})
output$clickcoord <- renderPrint({
print(input$plotclick)
})
})