[英]How to prevent `...` to disappear inside of deparse(substitute(x))
Short version精简版
Consider this function:考虑这个 function:
my_fun <- function(x){
deparse(substitute(x))
}
Why does the function remove the ` if nothing else than one variable name is added?如果只添加一个变量名,为什么 function 会删除 `? See here:
看这里:
my_fun(`bad name`)
"bad name"
my_fun(`bad name` - 1)
"`bad name` - 1"
Long version长版
I wrote a simple function that takes a dataframe and does some transformation with choosen columns.我写了一个简单的 function ,它采用 dataframe 并对选定的列进行一些转换。 This is the function:
这是 function:
my_fun <- function(data, transform){
transform <- deparse(substitute(transform))
transform <- unlist(strsplit(transform, "\\+"))
out <- do.call("cbind.data.frame", lapply(transform, function(transform_i){
eval(parse(text= transform_i), data)
}))
names(out) <- transform
return(out)
}
With this function we can do stuff like this:有了这个 function 我们可以做这样的事情:
# Create data.
df <- data.frame(matrix(rep(1:4, 2), ncol= 2))
names(df) <- c("good_name", "bad name")
# Transform data with function.
my_fun(df, sqrt(good_name) + sqrt(`bad name`) - 1)
sqrt(good_name) sqrt(`bad name`) - 1
1 1.000000 0.0000000
2 1.414214 0.4142136
3 1.732051 0.7320508
4 2.000000 1.0000000
But the function fails if we enter a name containig a white space like here: my_fun(df, `bad name`).但是如果我们输入一个包含空格的名称,function 就会失败:my_fun(df, `bad name`)。 I noticed that
deparse(substitute(transform))
removes the ` if I make no transformation.我注意到
deparse(substitute(transform))
如果我不进行任何转换,则会删除 `。 See here:看这里:
my_fun <- function(data, transform){
deparse(substitute(transform))
}
my_fun(df, `bad name`)
"bad name"
my_fun(df, `bad name` -1)
"`bad name` - 1"
How can I prevent deparse(substitute(transform))
removing the `?如何防止
deparse(substitute(transform))
删除`?
I know that there are many ways out there to transform dataframe like my function does.我知道有很多方法可以像我的 function 那样转换 dataframe 。 For example, we could use with(df, `bad name` -1) and with(df, `bad name`) here.
例如,我们可以在这里使用 with(df, `bad name` -1) 和 with(df, `bad name`)。 But that is not the question.
但这不是问题。
From the help file ?default
: The default for the backtick option is not to quote single symbols but only composite expressions .来自帮助文件
?default
:反引号选项的默认值不是引用单个符号,而是仅引用复合表达式。 This SO post hints at looking at the ?Quotes
help file - we can escape backticks using a backslash if needed. 这篇 SO 帖子提示查看
?Quotes
帮助文件 - 如果需要,我们可以使用反斜杠来转义反引号。
In this example, one may try setting the backtick argument of the deparse
function to TRUE
.在此示例中,可以尝试将
deparse
function 的反引号参数设置为TRUE
。 To compare different approaches, and how combinations are deparsed, consider the following situations:要比较不同的方法,以及如何对组合进行解析,请考虑以下情况:
substitute(`bad name`)
#> `bad name`
deparse(substitute(`bad name`))
#> [1] "bad name"
deparse(substitute(`bad name`), backtick = T)
#> [1] "`bad name`"
# and see this fail:
substitute(`bad name)
# and this return
deparse(
deparse(substitute(`bad name`))
)
#> "\"bad name\""
deparse(substitute(
`deparse(substitute(bad name))`
))
#> [1] "deparse(substitute(bad name))"
deparse(substitute(
`deparse(substitute(`bad name`))`
))
#> Error: unexpected symbol in:
#> "deparse(substitute(
#> `deparse(substitute(`bad"
#> > ))
#> Error: unexpected ')' in ")"
# but
deparse(substitute(
`deparse(substitute(\`bad name\`))`
))
#> [1] "deparse(substitute(`bad name`))"
If the intent is to enforce character names, have a look at deparse1
for R4.0.0 (April 2020) and later.如果打算强制使用字符名称,请查看适用于
deparse1
(2020 年 4 月)及更高版本的deparse1 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.