将函数中的参数传递给 DiagrammeR 中的 GrViz 时出错:“找不到对象”

问题描述

我正在尝试创建一个函数,该函数基于使用 diagrammeR::grViz 的输入输出图形,但 grViz 函数无法替换我的函数中的参数。这是一个基本示例:

library(DiagrammeR)

foo <- function(insert_text_here){
  text_to_show <- insert_text_here
  DiagrammeR::grViz("digraph {
    graph [layout = dot,rankdir = LR]

    node [shape = rectangle,fixedsize = true,width = 4.5,height = 1.5,fontname =   Helvetica,fontsize  = 20]
    rec1 [label = @@1]
    rec2 [label = b]
    # edge definitions with the node IDs
    rec1 -> rec2
    # from code

  }

  [1]: text_to_show
  ")
}

foo(insert_text_here = "hello")

返回此错误:

eval(parse(text = split_references[i])) 中的错误:找不到对象“text_to_show”```。

如果我在函数外部的全局环境中定义变量 text_to_show,它可以完美无误地运行:

text_to_show <- "hello"
foo(insert_text_here = "hello")

#success

因此,当 GrViz 的输入由函数的参数定义时,问题就出现了。有没有办法解决这个问题?

(这似乎与这个问题有关:https://github.com/rich-iannone/DiagrammeR/issues/266

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)