R:将字符串作为参数传递时没有引号

R:将字符串作为参数传递时没有引号,r,function,R,Function,我想知道如何使R将参数视为字符串,即使它没有引号。可能具有未知的功能条: foo <- function(x) bar(a)) foo(a abadsf 23) [1] "a abadsf 23" 这是你想要的吗 printname <- function(x) deparse(substitute(x)) printname(foo) [1] "foo" printname(bar) [1] "bar" 脚本中的用法: print.input() input with spac

我想知道如何使R将参数视为字符串,即使它没有引号。可能具有未知的功能

foo <- function(x) bar(a))
foo(a abadsf 23)
[1] "a abadsf 23"

这是你想要的吗

printname <- function(x) deparse(substitute(x))
printname(foo)
[1] "foo"
printname(bar)
[1] "bar"
脚本中的用法:

print.input()
input with spaces
[1] "input with spaces"

您可以使用quote,但这不适用于空格

deparse(quote(abc))
## [1] "abc"
deparse(quote(abc 1))
## Error: unexpected numeric constant in "deparse(quote(abc 1"

当变量a被定义为“foo”时,您想打印“a”,还是我根本不明白?不,我只是写了
a=“foo”
,所以很明显我不想打印“foo”。也许我应该给出
(函数(x)print(bar(a))(a)[1]“a”
,我就是这么说的。您想要打印“a”,但是有一个变量a被定义为“foo”。它是正确的吗?我担心foo()中的部分每次都会产生语法错误。您到底为什么要这样做?我的错误是,它适用于该示例,但不适用于一般情况<代码>打印名(foo-foo)错误:“打印名(foo-foo)”中出现意外符号@fsmart抱歉,我当时不知道需要空间。我添加了一些内容,但我希望您有理由寻找此功能,并且上面的解决方案不足以满足它。很有趣。感谢您的想法,但这并不是我真正想要的
deparse(quote(abc))
## [1] "abc"
deparse(quote(abc 1))
## Error: unexpected numeric constant in "deparse(quote(abc 1"