【问题标题】:Update a datatable after clicking on a network node in a shiny app在闪亮的应用程序中单击网络节点后更新数据表
【发布时间】:2018-09-24 13:08:13
【问题描述】:

我有一个简单的闪亮应用程序,它显示一个网络,在下表中,您可以看到所有通过边缘和边缘名称的网络节点连接。我想更新数据表以在单击节点时仅显示选定的节点信息。例如,当我单击节点“articaine”时,表格中只会显示“articaine”连接。

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

【问题讨论】:

  • 看起来visGetSelectedNodes() 应该允许您将选定的节点传递给server 并限制您的数据表。
  • 有用的评论。如果我能精确地创建我想要的表而不仅仅是节点

标签: r shiny


【解决方案1】:

这是一个替代解决方案,它允许选择多个节点,并且不使用observe,但在其他方面类似于发布的解决方案firmo23。我过滤到在“to”或“from”列中具有选定节点的任何边,因为我不清楚您要的是哪个。

另外,一些布局方面的cmets:侧边栏和主面板布局不是必需的。我更喜欢嵌套 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)

reprex package (v0.2.1) 于 2018 年 9 月 24 日创建

【讨论】:

  • 我接受您的解决方案更完整。你知道我是否可以将相同的逻辑应用于边缘而不是节点?我为此打开了一个 Q。
  • 我认为这是可能的 - 我昨晚看到了你的问题。昨晚我第一次天真地尝试反映这个解决方案没有奏效。如果在此期间没有出现另一种解决方案,我稍后会尝试再次查看它。谢谢!
  • 如果从下拉列表中选择节点,您认为表也可以更新吗?
  • 哦,有趣,我没有测试过这个。与nodesIdSelection 下拉菜单交互时,似乎没有任何visEvents 被触发。可能没有一个好的方法可以做到这一点,但我认为这可能值得一个单独的问题。
【解决方案2】:

我让它像这样工作:

#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{}

      })
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-05
    • 2022-01-23
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2016-07-08
    • 2020-07-21
    • 1970-01-01
    相关资源
    最近更新 更多