繁体   English   中英

R Shiny中的散点图(散点图)的事件数据

[英]Event data for plotly (scattergeo) maps in R Shiny

我正在构建一个闪亮的应用程序,但在绘制地图事件数据时遇到了一些麻烦。 我过去创建了一个散点图,并在plot_ly函数中定义了一个'key'变量。 如果单击散点图中的某个点,则将提取关键点,并将该关键点用于子集数据帧并生成后续图。 我在下面的代码中遵循相同的格式,但是密钥未存储在事件数据中。 事件数据仅包含“ curveNumber”和“ pointNumber”。 它似乎适用于此处的Choropleth映射: https ://plot.ly/r/shinyapp-map-click/,但我无法将其用于“ scattergeo”。

任何帮助将不胜感激。

library(shiny)
library(plotly)

ui <- shinyUI(fluidPage(

  titlePanel("My Shiny App"),
  sidebarLayout(
    sidebarPanel(
      numericInput("idnum", label = h3("ID #"), 
                   value = 3)
    ),
    mainPanel(
      plotlyOutput("map"),
      verbatimTextOutput("click")
    )
  )
))

server <- shinyServer(function(input, output) {

  output$map <- renderPlotly({

    df <- data.frame(id = c(3,6,20,35), lat = c(30.67,32.46,37.83,29.62), lon = c(-97.82, -62.34, -75.67, -85.62))

    sub <- df[which(df$id == input$idnum),]

    g <- list(
      scope = 'north america',
      showland = TRUE,
      landcolor = toRGB("grey83"),
      subunitcolor = toRGB("white"),
      countrycolor = toRGB("white"),
      showlakes = TRUE,
      lakecolor = toRGB("white"),
      showsubunits = TRUE,
      showcountries = TRUE,
      resolution = 50,
      projection = list(
        type = "miller",
        rotation = list(lon = -100)
      ),
      lonaxis = list(
        showgrid = TRUE,
        gridwidth = 0.5,
        range = c(-140, -55),
        dtick = 5
      ),
      lataxis = list(
        showgrid = TRUE,
        gridwidth = 0.5,
        range = c(20, 60),
        dtick = 5
      )
    )

    plot_ly(sub, lon = ~lon, lat = ~lat, key = ~id, text = ~paste(id), hoverinfo = "text",
            marker = list(size = 10),
            type = 'scattergeo',
            locationmode = 'USA-states') %>%
      layout(title = 'Locations', geo = g)
  })

  output$click <- renderPrint({
    d <- event_data("plotly_click")
    if (is.null(d)) "Click events appear here" else d
  })
})

shinyApp(ui = ui, server = server)

一种获取密钥的解决方法是替换:

sub <- df[which(df$id == input$idnum),]

sub <- df[which(df$id == input$idnum),] 
rownames(sub) <- sub$id 
key <- row.names(sub)

看起来key正在使用行名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM