R闪亮地图单击点附近

时间:2019-03-01 11:32:56

标签: r shiny maps shapes

我对R Shiny很陌生。我尝试创建一张欧洲彩色地图。如果您单击地图的某个区域,我希望显示一些其他信息。我想以某种方式将单击的坐标链接到基础数据框,并选择要显示的其他值。我尝试了几种想法,nearPoints函数对我来说似乎是最有前途的道路。但是我总是会出错。

我创建了一个简单的代码示例,其中显示了错误。知道我该如何解决吗?

非常感谢

# Load packages ----
library(shiny)
library(leaflet)
library(tidyr)
library(tidyverse)
library(tidyr)
library(dplyr)
library(eurostat)
library(DT)
library(shinydashboard)

isoc_r_broad_h <- get_eurostat("isoc_r_broad_h")
nuts <- get_eurostat_geospatial(resolution = "60", nuts_level = "1", year = "2013") 
data <- inner_join(nuts, isoc_r_broad_h) 
data$time <- format(as.Date(data$time, format="%Y/%d/%m"),"%Y") 
data <- filter(data, time == "2017")



header<-dashboardHeader(title="Broadband access %")


# User interface ----
ui <- dashboardBody(fluidRow(  verbatimTextOutput("click"), 


                           column(width = 9,
                                  box(width = NULL, solidHeader = TRUE,
                                      mainPanel(plotOutput("map",click = "plot_click"))
                                  )),
                           column(width = 4,
                                  verbatimTextOutput("plot_clickinfo")
                           ),
                           column(width = 5,
                                  box(width=NULL,
                                      DT::dataTableOutput('table')
                                  )),
                           column(width = 5,
                                  box(width=NULL,
                                      DT::dataTableOutput('plot_clickedpoints')
                                  ))

))



# Server logic ----
server <- function(input, output) {


  output$map <- renderPlot({


    data %>% select(values) %>% plot(, main = "Broadband %")    
  })

  output$plot_clickinfo <- renderPrint({
    cat("Click:\n")
    str(input$plot_click)
 }) 


  output$table <- DT::renderDataTable({


    dplyr::select(as.data.frame(data), geo,values) 


  },options = list(  pageLength = 4, processing=FALSE, order = list(2, 'desc')   ))

  output$plot_clickedpoints <- renderTable({
   # For base graphics, we need to specify columns, though for ggplot2,
   # it's usually not necessary.
    res <- nearPoints(data(), input$plot_click, data$geo)
   if (nrow(res) == 0)
     return()
    res
 })  


}



dashboardPage(
  header,
 dashboardSidebar(disable = TRUE),
 ui
)


# Run app ----
shinyApp(ui, server)

0 个答案:

没有答案