1

我有一个闪亮的应用程序,其中我有一个使用 渲染的网络图ggraph,类似于下面的应用程序:

library(ggraph)
library(igraph)
library(shiny)

ui <- fluidPage(
    plotOutput("plot", brush = brushOpts(id = "plot_brush"))
)

server <- function(input, output) {
  graph <- graph_from_data_frame(highschool)

  output$plot <- renderPlot({
    ggraph(graph) + 
      geom_edge_link(aes(colour = factor(year))) + 
      geom_node_point()
  })

  observe(print(
    brushedPoints(as_data_frame(graph, what = "vertices"), input$plot_brush)
        )
    )
}

shinyApp(ui, server)

我要做的是,当您在图表中单击并拖动以捕获某些节点时,我可以检查有关捕获的那些特定点的更多信息。现在,我只是在使用observe({print()}),以便可以在控制台中看到捕获的内容。

我的问题是,每当我在应用程序中选择一个区域时,无论所选区域中包含多少个节点,控制台都会返回 0 行。如何让它返回所选区域中包含的节点?

4

1 回答 1

1

这个回应给我指明了方向:

library(ggraph)
library(igraph)
library(shiny)
library(dplyr)

ui <- fluidPage(
  plotOutput("plot", brush = brushOpts(id = "plot_brush"))
)

server <- function(input, output) {
  graph2 <- graph_from_data_frame(highschool)

  set.seed(2017)
  p <- ggraph(graph2, layout = "nicely") + 
    geom_edge_link() + 
    geom_node_point()

  plot_df <- ggplot_build(p)

  coords <- plot_df$data[[2]]

  output$plot <- renderPlot(p)

  coords_filt <- reactive({
    if (is.null(input$plot_brush$xmin)){
      coords
    } else {
    filter(coords, x >= input$plot_brush$xmin, 
           x <= input$plot_brush$xmax, 
           y >= input$plot_brush$ymin, 
           y <= input$plot_brush$ymax)
    }
  })

  observe(print(
    coords_filt()
  )
  )

}

shinyApp(ui, server)
于 2017-09-25T16:44:33.410 回答