代码之家  ›  专栏  ›  技术社区  ›  firmo23

在闪亮的应用程序中单击网络节点后更新数据表

  •  1
  • firmo23  · 技术社区  · 7 年前

    #dataset
    id<-c("articaine","benzocaine","etho","esli")
      label<-c("articaine","benzocaine","etho","esli")
      node<-data.frame(id,label)
    
      from<-c("articaine","articaine","articaine","articaine","articaine","articaine","articaine","articaine","articaine")
      to<-c("benzocaine","etho","esli","benzocaine","etho","esli","benzocaine","etho","esli")
      title<-c("SCN1A","SCN1A","SCN1A","SCN2A","SCN2A","SCN2A","SCN3A","SCN3A","SCN3A")
    
      edge<-data.frame(from,to,title)
    
    #app
    
    #ui.r
    library(igraph)
    library(visNetwork)
    library(dplyr)
    library(shiny)
    library(shinythemes)
    library(DT)
    
    ui <- fluidPage(theme = shinytheme("cerulean"),  # Specify that the Cerulean Shiny theme/template should be used
    
                    # Generate Title Panel at the top of the app
                    titlePanel("Network Visualization App"),  
    
                    # Render as a sidebarLayout. Shiny expects that a sidebarPanel() function and a mainPanel() function are present.
    
                    sidebarLayout(
    
                      # Sidebar section. Can set the width of the sidebar for any value ranging from 1 to 12.
    
                      sidebarPanel(             
                      ), # End of the sidebar panel code
    
                      # Define the main panel
                      mainPanel(
    
                        h3("Network Visualization"),
    
                        # Plot the network diagram within the main panel. 
                        # Note that visNetworkOutput is not a Shiny package function, but a visNetwork package function.
                        visNetworkOutput("plot2"),
                        fluidRow(
                          DTOutput('tbl')
                        )
    
                        ) # End of main panel code
    
                    )
    )
    #server.r
    library(igraph)
    library(visNetwork)
    library(dplyr)
    library(shiny)
    library(shinythemes)
    
    server <- function (input, output, session){
    
    
      # Use the renderVisNetwork() function to render the network data.
    
      output$plot2 <- renderVisNetwork({
        visNetwork(nodes = node,edge)%>% 
    
    
          visOptions(highlightNearest=T, nodesIdSelection = T) %>%
    
          # Specify that hover interaction and on-screen button navigations are active
          visInteraction(hover = T, navigationButtons = T) %>%
    
          visIgraphLayout()
    
      })
      output$tbl = renderDT(
        edge, options = list(lengthChange = FALSE)
      )
    }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   DanTan    7 年前

    这里有一个替代的解决方案,它允许选择多个节点,并且不使用 observe

    另外,关于布局的一些评论:侧边栏和主面板布局是不需要的。我更喜欢在树上筑巢 fluidRow() column() 明确定义面板,我在下面做了。

    library(igraph)
    library(visNetwork)
    library(dplyr)
    library(shiny)
    library(shinythemes)
    library(DT)
    
    #dataset
    id<-c("articaine","benzocaine","etho","esli")
    label<-c("articaine","benzocaine","etho","esli")
    node<-data.frame(id,label)
    
    from<-c("articaine","articaine","articaine",
            "articaine","articaine","articaine",
            "articaine","articaine","articaine")
    to<-c("benzocaine","etho","esli","benzocaine","etho","esli","benzocaine","etho","esli")
    title<-c("SCN1A","SCN1A","SCN1A","SCN2A","SCN2A","SCN2A","SCN3A","SCN3A","SCN3A")
    
    edge<-data.frame(from,to,title)
    
    
    #app
    
    ui <- fluidPage(
    
      # Generate Title Panel at the top of the app
      titlePanel("Network Visualization App"),
    
      fluidRow(
        column(width = 6,
               DTOutput('tbl')),
        column(width = 6,
               visNetworkOutput("network")) #note that column widths in a fluidRow should sum to 12
      ),
      fluidRow(column(width = 6), 
               column(width=6, "Click and hold nodes for a second to select additional nodes.")
      )
    
    ) #end of fluidPage
    
    
    server <- function (input, output, session){
    
      output$network <- renderVisNetwork({
        visNetwork(nodes = node,edge) %>% 
          visOptions(highlightNearest=TRUE, 
                     nodesIdSelection = TRUE) %>%
          #allow for long click to select additional nodes
          visInteraction(multiselect = TRUE) %>%
          visIgraphLayout() %>% 
    
          #Use visEvents to turn set input$current_node_selection to list of selected nodes
          visEvents(select = "function(nodes) {
                    Shiny.onInputChange('current_node_selection', nodes.nodes);
                    ;}")
    
      })
    
      #render data table restricted to selected nodes
      output$tbl <- renderDT(
        edge %>% 
          filter((to %in% input$current_node_selection)|(from %in% input$current_node_selection)),
        options = list(lengthChange = FALSE)
      )
    
    }
    
    shinyApp(ui, server)
    

    创建日期:2018-09-24 reprex package (第0.2.1版)

        2
  •  2
  •   firmo23    7 年前

    我让它像:

    #ui.r
        library(igraph)
        library(visNetwork)
        library(dplyr)
        library(shiny)
        library(shinythemes)
        library(DT)
    
        ui <- fluidPage(theme = shinytheme("cerulean"),  # Specify that the Cerulean Shiny theme/template should be used
    
                        # Generate Title Panel at the top of the app
                        titlePanel("Network Visualization App"),  
    
                        # Render as a sidebarLayout. Shiny expects that a sidebarPanel() function and a mainPanel() function are present.
    
                        sidebarLayout(
    
                          # Sidebar section. Can set the width of the sidebar for any value ranging from 1 to 12.
    
                          sidebarPanel(             
                          ), # End of the sidebar panel code
    
                          # Define the main panel
                          mainPanel(
    
                            h3("Network Visualization"),
    
                            # Plot the network diagram within the main panel. 
                            # Note that visNetworkOutput is not a Shiny package function, but a visNetwork package function.
                            visNetworkOutput("plot2"),
    
                              dataTableOutput("nodes_data_from_shiny"),
                              uiOutput('dt_UI')
    
    
                            ) # End of main panel code
    
                        )
        )
        #server.r
        library(igraph)
        library(visNetwork)
        library(dplyr)
        library(shiny)
        library(shinythemes)
    
        server <- function (input, output, session){
    
    
          # Use the renderVisNetwork() function to render the network data.
    
          output$plot2 <- renderVisNetwork({
    
            visNetwork(nodes,edge)%>% 
              visEvents(select = "function(nodes) {
                        Shiny.onInputChange('current_node_id', nodes.nodes);
                        ;}")%>%
    
              visOptions(highlightNearest=T, nodesIdSelection = T) %>%
    
              # Specify that hover interaction and on-screen button navigations are active
              visInteraction(hover = T, navigationButtons = T) %>%
    
              visIgraphLayout()
    
          })
    
          myNode <- reactiveValues(selected = '')
    
          observeEvent(input$current_node_id, {
            myNode$selected <<- input$current_node_id
          })
          output$table <- renderDataTable({
            edge[which(myNode$selected == edge$from),]
          })
          output$dt_UI <- renderUI({
            if(nrow(edge[which(myNode$selected == edge$from),])!=0){
              dataTableOutput('table')
            } else{}
    
          })
        }
    
    推荐文章