在R函数输入周围添加引号?

huangapple go评论55阅读模式
英文:

Addition quotations around an R function input?

问题

你可以使用以下方式在R中给函数输入加上引号:

function_name <- function(x) {
  paste0("The input you have chosen is '", x, "'")
}

# 调用函数并传入参数
result <- function_name(x = "gender")

# 输出: "The input you have chosen is 'gender'"

这段代码会将输入的参数 x 包裹在单引号中,生成你想要的输出。

英文:

How do I add quotations around an input to a function in R?

I would like to have something such as,

function_name (x) { 
paste0(&quot;The input you have chosen is&quot;, x,)
}

function_name(x = gender)

output: "The variable you have chosen is gender"

I know that I could do it using paste0 if the input was
function_name(&quot;gender&quot;), but I don't know how to do this if the input doesn't have quotations.

I have tried using paste0 to paste single quotations around the word, but gives errors such as:

> Error in paste0("'", x, "'") : object 'Gender' not found.

I have also tried escaping the quotation marks, but the slashes are being read and giving errors as well.

Thanks in advance for any suggestions.

答案1

得分: 3

只需用substitute函数包装参数。这样可以进行非标准评估(NSE):

function_name <- function (x) { 
  paste0("你选择的输入是", substitute(x)) #注意这里
}

现在你可以运行:

function_name(gender) # 不使用引号
[1] "你选择的输入是 gender"
function_name("gender") # 使用引号
[1] "你选择的输入是 gender"
英文:

Just wrap the parameter with substitute function. This enables one to NSE:

function_name &lt;- function (x) { 
  paste0(&quot;The input you have chosen is &quot;, substitute(x)) #Note here
}

And now you can run:

function_name(gender) # Without quotes
[1] &quot;The input you have chosen is gender&quot;
function_name(&quot;gender&quot;) # With quotes
[1] &quot;The input you have chosen is gender&quot;

答案2

得分: 1

As pointed by AdroMine, this envolves non-standard evaluation. The link he provided is quite useful.

But to provide an answer, you can do the following:

function_name <- function(x) {
paste("The input you have chosen is", as.character(rlang::enexpr(x)))
}

function_name(x = gender)
[1] "The input you have chosen is gender"

We are capturing the "word" gender before R tries to evaluate it (i.e., tries to find which value should be associated with gender), and then, we are getting the actual text with as.character()

英文:

As pointed by AdroMine, this envolves non-standard evaluation. The link he provided is quite useful.

But to provide an answer, you can do the following:

function_name &lt;- function(x) {
  paste(&quot;The input you have chosen is&quot;, as.character(rlang::enexpr(x)))
}

function_name(x = gender)
[1] &quot;The input you have chosen is gender&quot;

We are capturing the "word" gender before R tries to evaluate it (i.e., tries to find which value should be associated with gender), and then, we are getting the actual text with as.character()

huangapple
  • 本文由 发表于 2023年6月6日 01:57:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定