我显然是一个简单的问题,我的灰质目前拒绝掌握 - 说我有一个清单:
list(a = "foo", b = c("bar", "biz", "booze"))
和一个函数fn
。我怎样才能得到这样的字符串:
"fn(a = \"foo\", b = c(\"bar\", \"biz\", \"booze\"))"
P.S。
我知道我会很遗憾在早上问这个......
答案 0 :(得分:5)
这应该让你开始,对吧......?
deparse(list(a = "foo", b = c("bar", "biz", "booze")),control = NULL)
[1] "list(a = \"foo\", b = c(\"bar\", \"biz\", \"booze\"))"
一个更完整的版本,我完成了@ aL3xa评论...
gsub("^list","fn",
deparse(list(a = "foo", b = c("bar", "biz", "booze")),control = NULL))
答案 1 :(得分:3)
您也可以直接操作语言对象,如R语言定义的Chapter 6中所述:
X <- quote(list(a = "foo", b = c("bar", "biz", "booze")))
X[[1]] <- quote(fn) ## as.symbol("fn") would also work
deparse(X)
# [1] "fn(a = \"foo\", b = c(\"bar\", \"biz\", \"booze\"))"
或者,如果您的列表已存储在命名对象中,则可以使用c()
和as.call()
拼凑所需的呼叫:
ll <- list(a = "foo", b = c("bar", "biz", "booze"))
deparse(as.call(c(as.symbol("fn"), ll)))
# [1] "fn(a = \"foo\", b = c(\"bar\", \"biz\", \"booze\"))"