【发布时间】:2012-11-07 02:31:36
【问题描述】:
在格式化发送到 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
但是当我尝试(以各种方式,如 here 建议的那样)写“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(...) 或dQuote(...) here 似乎会因为参数数量不同而破坏函数。
【问题讨论】:
标签: r string-formatting double-quotes output