rloopsvisnetwork

How to shorten code for "visRemoveNodes" using loop


I have constructed multiple protein - protein networks for diseases in shiny app and I ploted them using visnetwork. I found the articulation points for each network and I want to remove them.

My code for a disease looks like this:

 output$plot54 <- renderVisNetwork({
        alsex <- as.matrix(alsex)
        sel1 <- alsex[,1]
        sel2 <- alsex[,2]
        n10 <- unique(c(sel1,sel2))
        
        n10 <- as.data.frame(n10)
        colnames(n10) <- "id"
        ed10 <- as.data.frame(alsex)
        colnames(ed10) <- c("from", "to", "width")
        n10
        
        g <- graph_from_data_frame(ed10) 
        articulation.points(g)

        nodes4 <- data.frame(n10, color = ifelse(n10$id=="CLEC4E"|n10$id=="ACE2"|n10$id=="MYO7A"|n10$id=="HSPB4"
|n10$id=="EXOSC3"|n10$id=="RBM45"|n10$id=="SPAST"|n10$id=="ALMS1"|n10$id=="PIGQ"
|n10$id=="CDC27"|n10$id=="GFM1"|n10$id=="UTRN"|n10$id=="RAB7B"|n10$id=="GSN"|n10$id=="VAPA"|n10$id=="GLE1"
|n10$id=="FA2H"|n10$id=="HSPA4"|n10$id=="SNCA"|n10$id=="RAB5A"|n10$id=="SETX","red","blue"))
        
        
        
        visNetwork(nodes4, ed10, main = "Articulation Points") %>%
          visNodes (color = list(highlight = "pink"))%>%
          visIgraphLayout()%>%
          visOptions(highlightNearest = list(enabled = T, hover = T), 
                     nodesIdSelection = T)%>%
          visInteraction(keyboard = TRUE)
        
        
      })

 observe({
        input$delete54
        visNetworkProxy("plot54") %>%
          visRemoveNodes(id="CLEC4E")%>%visRemoveEdges(id = "CLEC4E")%>%
          visRemoveNodes(id="ACE2")%>%visRemoveEdges(id = "ACE2")%>%
          visRemoveNodes(id="MYO7A")%>%visRemoveEdges(id = "MYO7A")%>%
          visRemoveNodes(id="HSPB4")%>%visRemoveEdges(id = "HSPB4")%>%
          visRemoveNodes(id="EXOSC3")%>%visRemoveEdges(id = "EXOSC3")%>%
          visRemoveNodes(id="RBM45")%>%visRemoveEdges(id = "RBM45")%>% 
          visRemoveNodes(id="SPAST")%>%visRemoveEdges(id = "SPAST")%>%
          visRemoveNodes(id="ALMS1")%>%visRemoveEdges(id = "ALMS1")%>%
          visRemoveNodes(id="PIGQ")%>%visRemoveEdges(id = "PIGQ")%>%
          visRemoveNodes(id="CDC27")%>%visRemoveEdges(id = "CDC27")%>%
          visRemoveNodes(id="GFM1")%>%visRemoveEdges(id = "GFM1")%>%
          visRemoveNodes(id="UTRN")%>%visRemoveEdges(id = "UTRN")%>%
          visRemoveNodes(id="RAB7B")%>%visRemoveEdges(id = "RAB7B")%>%
          visRemoveNodes(id="GSN")%>%visRemoveEdges(id = "GSN")%>%
          visRemoveNodes(id="VAPA")%>%visRemoveEdges(id = "VAPA")%>%
          visRemoveNodes(id="GLE1")%>%visRemoveEdges(id = "GLE1")%>%
          visRemoveNodes(id="FA2H")%>%visRemoveEdges(id = "FA2H")%>%
          visRemoveNodes(id="HSPA4")%>%visRemoveEdges(id = "HSPA4")%>%
          visRemoveNodes(id="SNCA")%>%visRemoveEdges(id = "SNCA")%>%
          visRemoveNodes(id="RAB5A")%>%visRemoveEdges(id = "RAB5A")%>%
          visRemoveNodes(id="SETX")%>%visRemoveEdges(id = "SETX")
        
      })

Using

  g <- graph_from_data_frame(ed10) 
  articulation.points(g)

I found the articulation points, and I marked them with red color using ifelse as you can see in nodes4 vector.

My questions:

  1. How to shorten my code in ifelse using loop, so I don't have to write the articullation points one by one manually.
  2. How to shorten my code in visRemoveNodes and visRemoveEdges using loop, so I don't have to write them one by one manually as well.

Crossed posted at: https://community.rstudio.com/t/how-to-shorten-code-for-visremovenodes-using-loop/72506


Solution

  • The answer for the second question is:

       observe({
    l <- c("CLEC4E","ACE2", "MYO7A", "HSPB4", "EXOSC3", "RBM45","SPAST","ALMS1",
                    "PIGQ","CDC27","GFM1","UTRN", 
                    "RAB7B", "GSN", "VAPA", "GLE1","FA2H","HSPA4",
                    "SNCA","RAB5A","SETX") #we put all genes that we want to delete in a vector
    for (i in l){
              input$delete54
              visNetworkProxy("plot54")%>%
                visRemoveNodes(id= i)%>%visRemoveEdges(id = i)
            }
    })