1

I have a Shiny app in which I have a network graph rendered using ggraph, something similar to the app below:

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)

What I'm looking to do is when you click and drag in a chart so that some nodes are captured, I can examine more information about those specific points captured. For now, I'm just using observe({print()}) so that I can see in the console what gets captured.

My problem, whenever I select an area in the app, I get 0 rows returned in the console, no matter how many nodes are included in the area selected. How do I make it return the nodes included in the area selected?

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

1

This response showed me the way:

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)
Phil
  • 7,287
  • 3
  • 36
  • 66