在格式化发送到 R 中的函数的文本字符串时,我在使用双引号时遇到问题。
考虑一个示例函数代码:
foo <- function( numarg = 5, textarg = "** Default text **" ){
print (textarg)
val <- numarg^2 + numarg
return(val)
}
使用以下输入运行时:
foo( 4, "Learning R is fun!" )
输出是:
[1] "Learning R is fun!"
[1] 20
但是,当我尝试(以各种方式,如此处所建议的)写“R”而不是 R 时,我得到以下输出:
> foo( 4, "Learning R is fun!" )
[1] "Learning R is fun!"
[1] 20
> foo( 4, "Learning "R" is fun!" )
Error: unexpected symbol in "funfun( 4, "Learning "R"
> foo( 4, "Learning \"R\" is fun!" )
[1] "Learning \"R\" is fun!"
[1] 20
> foo( 4, 'Learning "R" is fun!' )
[1] "Learning \"R\" is fun!"
[1] 20
由于参数数量不同,使用as.character(...)
or在这里dQuote(...)
建议似乎会破坏函数。