首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R DiagrammeR包-动态更新图

R DiagrammeR包-动态更新图
EN

Stack Overflow用户
提问于 2018-04-30 05:03:16
回答 1查看 255关注 0票数 0

我试图在for循环的每一步更新R Studio查看器中的图表。

在循环结束时,我想获得以下信息:

愿望结果

但我得到的是:

不良结果

以上代码如下:

代码语言:javascript
复制
library(tidyverse)
library(DiagrammeR)

a_graph <-
  create_graph() %>%
  add_node(label = 'Start', type = 'actif', node_aes = node_aes(fillcolor = "orange",shape = "rectangle"
  ) ) 

a_graph %>% render_graph()


update_my_graph <- function(label, label_from){
  from_id <- which( (a_graph %>% get_node_df())$label==label_from )
  a_graph <<- a_graph %>% 
    select_nodes(
      conditions = 
        type == 'actif') %>%
    set_node_attrs_ws(
      node_attr = fillcolor,
      value = "blue") %>%
    clear_selection() %>% 
    add_node(label = label, from = from_id, type = 'actif', node_aes = node_aes(fillcolor = "orange",shape = "rectangle",fixedsize = FALSE))
  a_graph %>% render_graph(layout = "tree")
}

for(i in 1:5) update_my_graph(i,'Start')

R版本3.4.1 (2017-06-30) -“单蜡烛”tidyverse 1.2.1 DiagrammeR 1.0.0 RStudio 1.1.383

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-30 05:35:17

你的功能是正确的,真的。“坏结果”实际上是第7行中的第一个a_graph %>% render_graph(),并且没有调用更多的绘图,因此结果。要了解这一点,您可以在进行for(i in 1:5) update_my_graph(i,'Start')之前删除该情节。您将看到没有绘图输出。在完成了五个更新之后,您可以再次调用a_graph %>% render_graph(layout = "tree"),您将看到它已经给出了您想要的结果。函数本身并不是打印情节。

因此,这是一件很简单的事情:

代码语言:javascript
复制
update_my_graph <- function(label, label_from){
  from_id <- which((a_graph %>% get_node_df())$label==label_from)
  a_graph <<- a_graph %>% 
    select_nodes(conditions = type == 'actif') %>%
    set_node_attrs_ws(node_attr = fillcolor, value = "blue") %>%
    clear_selection() %>% 
    add_node(label = label, from = from_id, type = 'actif', 
             node_aes = node_aes(fillcolor = "orange", 
                                 shape = "rectangle", 
                                 fixedsize = FALSE))
  print(a_graph %>% render_graph(layout = "tree"))
}

也就是说,简单地将print放在a_graph %>% render_graph(layout = "tree")周围。您还可以执行return(a_graph %>% render_graph(layout = "tree")),然后调用存储的地块。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50094237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档