【发布时间】:2015-05-18 00:23:33
【问题描述】:
我正在尝试在 Shiny 中处理数据以生成可由 igraph 处理的图形数据框,但我正在努力处理嵌套数据转换。
我会收到以下错误消息
“plot.igraph(g3, layout = layout.mds) 中的错误:不是图形对象”
有什么想法吗?
这是服务器
library(shiny)
library(igraph)
# Define a server for the Shiny app
shinyServer(function(input, output) {
g4 <- reactive({
g2 = gtest[gtest[3]==input$article,]
g2 = g2[order(g2[3],decreasing = TRUE), ]
g2 = graph.data.frame(g2[1:5,2:3], directed=TRUE)
return(g2)
})
# Fill in the spot we created for a plot
output$g3plot <- renderPlot({
#render network graph
plot.igraph(g4,layout=layout.mds)
})
})
这是用户界面
library(shiny)
gtest<-as.data.frame(cbind(c(1:10),c(11:20), c(21:30))
# Define the overall UI
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Articles by similarities"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("article", "Article:", choice=gtest[1])),
hr()
),
# Create a spot for the plot
mainPanel(
plotOutput("g3plot")
)
)
)
【问题讨论】:
-
您应该阅读reactivity in Shiny 上的文章。
g2对input$article有反应,但g3和g4不会监听对g2的更改。为什么不把整个东西放在一个反应块中? -
是的,我试过了,但是不行 g4
-
您已尝试编辑您的问题,但您的代码发生了任意更改,您现在使用
isolate- 为什么?您遇到的错误以及您在我的回答中看到的错误与 Shiny 无关。您传递给plot.igraph的对象不是igraph对象,因此plot.igraph无法处理。 -
graph.data.frame(g2[1:5,2:3],directed=TRUE) 不是 igraph 对象?
-
如果您阅读了我链接到的文章,您就会理解这个问题。我的代码中的
g3不是igraph对象,但g3()是。检查以下答案中的代码是否解决了您的问题。