Correct click coordinates with ggplot map in shiny

I have the start of a brilliant ggplot world map application. I would like to get the coordinates of the click on the plot so that users can do something with the map, but the coordinates are very strange (either NULL, or something very small). Repeated pressing changes only one coordinate:

complex map

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")
  )
))

server.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)
  })
})

If I just use the command map()to create a non-ggplot world map, I get what looks like good lat / long values ​​for click codes:

simple map

server.R (changed):

library(shiny)
library(maps)
library(mapdata)

shinyServer(function(input, output) {

    output$myworld <- renderPlot({
      map("world2Hires")
    })

    output$clickcoord <- renderPrint({
      print(input$plotclick) 
    })

})
+3
source share
1 answer

print(worldmap) worldmap , , . ggplot2. print() , x y reset (0, 1).

0

All Articles