2

我一直在尝试使用R 中的Mapdeck开发一些围绕构建站点地图的 Web 应用程序。现在我正在尝试开发一个将在点击时返回值的应用程序。

我的问题是,对于下面的示例,Mapdeck 在点击事件中究竟返回了什么?我认为这是分配给“id”值的任何内容,但是我得到的东西似乎是与我的数据框输入无关的嵌套数据。在下面的示例中,我希望它返回每个站点的“Point_ID”值。

# ################################################################################################
# ################################################################################################
# # Sec 1a. Needed Libaries & Input Files

library(shiny)
library(shinydashboard)
library(mapdeck)
library(dplyr)

Map_DF <- data.frame("Point_ID" = c("A1", "B1", "C3"), 
                     "Latitude" = c(38.05, 39.08, 40.05), 
                     "Longitude" = c(-107.00, -107.05, -108.00),
                     "PointUse" = c("farm", "house", "well"),
                     "info" = c("A1 38.05 -107.00 farm", 
                               "B1 39.08 -107.05 house",
                               "C3 40.05 -108.00 well"))


################################################################################################
################################################################################################
#UI
ui <- dashboardPage(
    dashboardHeader(), 
    dashboardSidebar(), 
    dashboardBody(
        fluidRow(mapdeckOutput(outputId = 'mapA')),
        fluidRow(htmlOutput("textOutA"))
    )
)

################################################################################################
################################################################################################
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",
                id = 'Point_ID',
                tooltip = "info",
                radius = 10000,
                # radius_min_pixels = 3,
                auto_highlight = TRUE,
            )
    })

    ##Click Event, return text
    observe({
        click <- input$mapA_scatterplot_click
        if(is.null(click)) {
            output$textOutA <- renderUI({
                str1 <- paste("Nothing...")
                HTML(paste(str1))
            })
        } else {
            output$textOutA <- renderUI({
                str1 <- paste(click)
                HTML(paste(str1))
            })
        }
    })

}

################################################################################################
################################################################################################
shinyApp(ui = ui, server = server)
4

1 回答 1

1

这可能是id参数的错误。现在,您可以采取以下措施来克服这个问题(想法是使用字符串中包含的行索引):

# ################################################################################################
# ################################################################################################
# # Sec 1a. Needed Libaries & Input Files

library(shiny)
library(shinydashboard)
library(mapdeck)
library(dplyr)

Map_DF <- data.frame("Point_ID" = c("A1", "B1", "C3"), 
                     "Latitude" = c(38.05, 39.08, 40.05), 
                     "Longitude" = c(-107.00, -107.05, -108.00),
                     "PointUse" = c("farm", "house", "well"),
                     "info" = c("A1 38.05 -107.00 farm", 
                                "B1 39.08 -107.05 house",
                                "C3 40.05 -108.00 well"))


################################################################################################
################################################################################################
#UI
ui <- dashboardPage(
  dashboardHeader(), 
  dashboardSidebar(), 
  dashboardBody(
    fluidRow(mapdeckOutput(outputId = 'mapA')),
    fluidRow(htmlOutput("textOutA"))
  )
)

################################################################################################
################################################################################################
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, layer_id = "PointUse",
        lat = "Latitude",
        lon = "Longitude",
        id = 'Point_ID',
        tooltip = "info",
        radius = 10000,
        # radius_min_pixels = 3,
        auto_highlight = TRUE,
      )
  })
  

  output$textOutA <- renderUI({
   
    click <- input$mapA_scatterplot_click
    if(is.null(click)) {str1 <- "Nothing..."}
    else {str1 <- Map_DF[as.numeric(gsub(".*:","",gsub(",.*","",click)))+1,]$Point_ID}
    
    HTML(str1)
    })
  

  
}

################################################################################################
################################################################################################
于 2020-10-29T13:09:53.877 回答