0

I am relatively new to R and I wonder how to create a reactive igraph in shiny. I am trying to build a shiny app that analyzes Airbnb data. Overall I want to visualize a bipartite network that shows the connection between the listing_id of the apartment and the reviewer_id of the reviewer in an interactive fashion.

I tried to base the graph on a reactive filter that filters the data based on a neighborhood (input from the UI). The graph itself should show connections between listing_ids based on common reviewer_ids (i.e. Listing A is connected to Listing B if they have at least 1 reviewer in common).

The code that I have so far is:

#---------------UI----------
ui <- fluidPage(

  sidebarLayout(position = "right",
    sidebarPanel(h3("Input Parameters"),

                 selectInput("neighborhood.selection", "Select the neighborhood you want to display", 
                             c("Manhattan", "Brooklyn", "Queens", "Staten Island",
                               "Bronx")),
                 #textOutput("o.neighborhood.selection"),
                 #selectInput("vertex.selection", "Select the Vertex you want to display", 
                             #c("Reviewer ID" = 1, "Listing ID" = 2)),
                 ),
    mainPanel(h2("Overall Network Graph"),
              plotOutput("o.big.network")

              )
  ),
)

#-----------server----------

library(igraph)
library(shiny)
library(data.table)
library(rsconnect)
library(dplyr)

dt.merged <- readRDS("dt.merged.final.rds")
dt.merged$reviewer_id <- paste0("0", dt.merged$reviewer_id)
dt.merged <- dt.merged[1:500]

server <- function(input, output) {
dt.merged.reactive <- reactive({
  req(input$neighborhood.selection)
  filter(dt.merged,neighbourhood_group_cleansed %in% input$neighborhood.selection)
})

  all.listings <- dt.merged.reactive()[, list(name=unique(listing_id), type=TRUE)]
  all.reviewers <- dt.merged.reactive()[, list(name=unique(reviewer_id), type=FALSE)]  
  all.vertices <-   list(all.listings,all.reviewers)
  output$o.big.network  <- renderPlot({
    # Creating the graph
    g <- graph.data.frame(dt.merged.reactive()[, list(listing_id, reviewer_id)], directed=FALSE, vertices= all.vertices) 
  })
}

  • 2
    Hi, your example is not reproducible because we don't have access to ```dt.merged.final.rds```. You should adapt your example with datasets that everybody have, such as ```iris``` or ```mtcars```. See [here](https://stackoverflow.com/help/minimal-reproducible-example) to know how to make a reproducible example – bretauv Feb 28 '20 at 14:55
  • 1
    Hi, Welcome! Could you provide (minimal) sample data, so that the code can be copy-pasted and run "as is". Namely could you replace the `dt.merged <- readRDS("dt.merged.final.rds")` line with `dt.merged <- < copy output of a trimmed down dput(dt.merged)>` that allows to reproduce the issue? – Aurèle Feb 28 '20 at 14:56
  • See also R specific https://stackoverflow.com/questions/5963269 – Aurèle Feb 28 '20 at 15:27

0 Answers0