如何在 rstudio 中使用循环缩短 "visRemoveNodes" 的代码

How to shorten code for "visRemoveNodes" using loop in rstudio

我在闪亮的应用程序中构建了多个蛋白质 - 疾病蛋白质网络,并使用 visnetwork 绘制了它们。我找到了每个网络的连接点,我想删除它们。

我的疾病代码如下所示:

 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")
        
      })

使用

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

我找到了关节点,并使用 ifelse 将它们标记为红色,如您在 nodes4 向量中所见。

我的问题:

  1. 如何在 ifelse 中使用循环来缩短我的代码,这样我就不必手动一一写关节点了。
  2. 如何使用循环在 visRemoveNodesvisRemoveEdges 中缩短我的代码,所以我不必写他们也一个一个手动。

交叉发布于: https://community.rstudio.com/t/how-to-shorten-code-for-visremovenodes-using-loop/72506

第二个问题的答案是:

   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)
        }
})