发送包含双引号的文本字符串以起作用 [英] Send a text string containing double quotes to function

查看:288
本文介绍了发送包含双引号的文本字符串以起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在格式化发送到R中的函数的文本字符串时,我在使用双引号时遇到问题.

I'm having a problem with using double quotes while formatting text strings being sent to functions in 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

但是当我尝试时(按照各种建议,

But when I try (in various ways, as suggested here) to write "R" instead of R, I get the following outputs:

> 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

按照建议在此处似乎破坏了功能.

Using as.character(...) or dQuote(...) as suggested here seems to break the function because of different number of arguments.

推荐答案

您可以尝试以下方法:

foo <- function(numarg = 5, textarg = "** Default text **" ){ 
    cat(c(textarg, "\n")) 
    val <- (numarg^2) + numarg
    return(val) 
}

foo <- function(numarg = 5, textarg = "** Default text **" ){ 
    print(noquote(textarg)) 
    val <- (numarg^2) + numarg
    return(val) 
}

foo( 4, "Learning R is fun!" )
foo( 4, 'Learning "R" is fun!' )

这篇关于发送包含双引号的文本字符串以起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆