按下R中的传单弹出窗口时如何创建事件? [英] How to create an event when pressing on a leaflet popup in R?

查看:76
本文介绍了按下R中的传单弹出窗口时如何创建事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击Leaflet多边形时,我想在Shiny中更改tabPanel.关于如何执行此操作,我有一些想法,但是找不到实现这些信息所需的信息.我在tabPanel中有传单,但是当单击多边形时,我想切换到另一个标签.

I would like to make the tabPanel change in Shiny when I click on a Leaflet polygon. I have a couple of ideas on how to do this, but I can't find the information I need to implement them. I have the leaflet in a tabPanel, but I would like to switch to another tab when a polygon is clicked on.

    leaflet(llmap) %>%
      addTiles() %>%
      addPolygons(stroke = F,
                  fillOpacity = .8,
                  smoothFactor = .5,
                  color=~pal(x),
                  popup = pop)

我考虑过制作popup=updateTabsetPanel(session="New Tab"),但这是行不通的.我的另一个想法是在用户单击新多边形时随时调用updateTabsetPanel(session="New Tab"),但是我不知道我需要返回什么事件以使其知道已经单击了新多边形,或者即使弹出了新的弹出窗口.有人知道吗?

I thought of making popup=updateTabsetPanel(session="New Tab"), but that doesn't work. My other idea is to call updateTabsetPanel(session="New Tab") anytime the user clicks on a new polygon, but I don't know what event I would need to return to let it know that a new polygon was clicked or even if a new popup popped up. Does anyone know this?

推荐答案

以下是一个反应函数的示例,该函数在单击多边形时会更新:

Here's an example of a reactive function that updates when you click a polygon:

output$myMap <- renderLeaflet({
    map_out() #this is just a function that returns a leaflet map
  })


output$MyGraph <- renderPlot({  
    event <- input$myMap_shape_click #Critical Line!!!

    ... #Code to run here

    GraphData <- GraphData[event$id] # subsetting example

    }
  })  

这里需要注意的几件事:

A few things to note here:

  1. input$myMap_shape_click会根据您在上面所说的地图而变化.我称它为myMap,所以结构如图所示.如果您使用output$YourMap进行初始化,则点击将用input$YourMap_shape_click

  1. the input$myMap_shape_click changes based on what you call your map above. I called it myMap, so the structure is as shown. If you used output$YourMap to initialize, the click would be called with input$YourMap_shape_click

可以使用event$id访问您单击的多边形的ID.这对于基于单击的多边形的子集+图形绘制非常有用.也可以访问event$latevent$lng

the id of the polygon that you click on can be accessed with event$id. This can be really useful for subsetting + graphing based on a polygon that is clicked. Also accessible are event$lat and event$lng

renderPlot可以是任何反应性函数.如果它不绑定到特定的输出,则可以只使用observe,如下所示.这样,您的代码将在单击多边形的任何时间运行.这是因为input$myMap_shape_click的值每次单击都会更改.

the renderPlot can be any reactive function. If its not tied to a specific output, you can just use observe as below. This way, your code will run anytime a polygon is clicked. This is because the value of input$myMap_shape_click changes every time you click.

我以前没有使用过updateTabsetPanel,但是我想这会起作用:

I haven't used updateTabsetPanel before, but I'd imagine this will work:

observe({

  event <- input$myMap_shape_click

  updateTabsetPanel(session, "inTabset", selected = event$id)

}) 

这会将选项卡切换到与您单击的多边形具有相同ID的面板.

which would switch the tab to a panel with the same id as the polygon you clicked.

这篇关于按下R中的传单弹出窗口时如何创建事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆