【问题标题】:How to execute a command that is built as string [duplicate]如何执行构建为字符串的命令[重复]
【发布时间】:2026-01-08 13:30:01
【问题描述】:

我正在寻找构建字符串,这应该是执行命令的输入。字符串已准备好并按如下所述构造。

问题:

如何将字符串的内容作为命令发送?

# Build list with content
my_list <- list("no1" = 12)

# Define content, parts of string.
text_left  <- "ls.str("
text_right <- ")"

# Build string.
test_string_result <- paste0(
  text_left,
  "my_list",
  text_right
)

# Result of string.
print(string_result)

命令“print(string_result)”的打印输出,给出:

[1] "ls.str(my_list)"

想要的结果:

我希望“string_result”作为将内容作为命令发送的基础, 例如它应该发送“ls.str(my_list)

【问题讨论】:

  • eval(parse(text = test_string_result)).

标签: r


【解决方案1】:

使用eval(parse(text=print(test_string_result)))

结果:

no1 : num 12

【讨论】: