2

我正在使用 Neo4j 图形数据构建一个交互式 Shiny 应用程序,方法是使用 RNeo4j 包将 Neo4j 与 R 连接起来。

该应用程序包含一个表格,显示从 Neo4j 提取的图形数据的属性,用户可以查看和更改表格内容(图形数据的属性)。这些更改可以作为图形数据属性的更新写回 Neo4j。这个功能可以使用 RNeo4j 包中的updateProp& functiona 来完成。getOrCreateNode

但是,我有一个反应性错误。

下面是我的代码:

library(RNeo4j)
library(dplyr)
library(shiny)
library(shinydashboard)
library(visNetwork)
library(tidyr)
library(sqldf)
library(igraph)
library(plotly)
library(stringi)
library(stringr)

graph = startGraph("http://localhost:7474/db/data/", username = "xxx", password = "xxx")
summary(graph)

# build dashboard
# UI items list 
header <- dashboardHeader(
  title = "Neo4j"
)

sidebar <- dashboardSidebar(
  sidebarMenu (
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "dashboard1",
            box(textOutput("aa")),
            box(title = "CATEGORY", DT::dataTableOutput("category")),
            box(uiOutput("category_status"))

    )
  )
)

# UI
ui <- dashboardPage(header, sidebar, body)

# Server
server <- function(input, output, session) {

  # Query graph data and properties from Neo4j and store them in table format in R 
  query = "
  MATCH (c:Category)
  RETURN c.id AS Category, c.Price AS Price, c.status AS Category_Status
  "
  info = cypher(graph, query) # R Table to store Neo4j data

  # Build Shiny output tables
  output$i1 <- renderTable(info)
  output$category = DT::renderDataTable(info, selection = 'single')
  # This is to build the function of change the status of category with 3 options
  output$category_status = renderUI({
    x = info[input$category_rows_selected, ]
    if (length(x)){
      selectInput("category_status", "", 
                  c( "Sold", "On Sale","Out of Stock"), 
                  selected = x$Category_Status)
    }
  })
  # Table to examine if the status change was made successfully
  output$aa<-renderText(input$category_status)

  # Write back the changes made in Shiny to Neo4j using "updateProp" & "getOrCreateNode" function in RNeo4j
  if(info$Category_Status[input$category_rows_selected] != input$category_status) {
    category_num = as.numeric(substring(info$Category[input$category_rows_selected], 16))
    updateProp(getOrCreateNode(graph, "Category", Category = paste("CC_CCAA_AAC.0C-", category_num, sep="")), 
               status = input$status)
  }
}

# Shiny dashboard
shiny::shinyApp(ui, server)

以下是错误消息:

Listening on http://127.0.0.1:4401
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  55: stop
  54: .getReactiveEnvironment()$currentContext
  53: .subset2(x, "impl")$get
  52: $.reactivevalues
  50: server [#44]
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

通过反复试验,如果我删除将更改写回 Neo4j 的代码,Shiny 应用程序可以正常工作,这是代码的最后一部分。然而,这是这个项目的核心功能。

此外,这个回写功能通过单独站在 Shiny 之外正常工作。所以问题是两个部分之间的相互作用。

我想知道是否可以添加observeEventShiny 来解决这个问题。

提前致谢。

4

1 回答 1

1

正如您所说,代码的最后一部分导致了问题。背后的原因是您reactives在此代码块中使用。那就是您使用input$category_rows_selectedinput$category_status并且input$status 没有固定值,而是取决于您与应用程序的交互。

根据您想要做什么,您基本上有两种选择:

  1. 将代码块包含在isolate. 但是,在这种情况下,当您更改相应的输入时,您的代码将永远不会更新。
  2. 将代码块包含在observe. 在这种情况下,只要任一输入(如上所列)更改其值,就会执行此代码。如果您希望代码块仅在某些输入发生更改时运行,您可以isolate使用您不想依赖的输入。

例如,此代码将在任何时候执行input$category_rows_selectedinput$category_status更改,但如果input$status更改则不会执行,因为它包含在isolate

observe({
   if(info$Category_Status[input$category_rows_selected] != input$category_status) {
       category_num = as.numeric(substring(info$Category[input$category_rows_selected], 16))
       updateProp(getOrCreateNode(graph, "Category", 
                  Category = paste("CC_CCAA_AAC.0C-", category_num, sep="")), 
                  status = isolate(input$status))
  }
})
于 2019-03-12T08:44:03.743 回答