如何在 mutate 调用中访问函数内的列名?
How can I access a column name within a function in a mutate call?
我正在改变一列,以使用我编写的函数在 R 中创建一个新列。
在我的函数中,我想发送一条消息,其中包含正在更改的列的名称。
如何在 mutate 调用中从函数内部访问正在改变的列名?
可重现的例子:
data <- tribble(
~colB,
1,
2,
3
)
# Function that will be used in the mutate
add1 <- function(numeric_vector) {
return(1 +numeric_vector)
# I want to message the user the name of the column they are mutating
# This simply returns the entire vector
message("You mutated", numeric vector)
# This returns 'numeric_vector'
message("You mutated", quo_name(quo(numeric_vector)))
}
# Desired Output:
data %>%
mutate(colC = add1(colB))
You mutated colB
colB colC
<dbl> <dbl>
1 2
2 3
3 4
我想你想要
add1 <- function(numeric_vector) {
message(paste("You mutated", quo_name(enquo(numeric_vector))))
return(1 + numeric_vector)
}
请注意,您必须在 return()
之前打印您的消息。 return()
之后的任何内容都不会在函数中 运行 因为当你点击该语句时你退出了。此外,您可以使用 enquo()
获取变量以获取其名称。您需要在它仍处于承诺状态时获取它的名称,这意味着在您实际使用它的价值之前。
使用 substitute
其中 returns 一个 name
class 对象。我们已将 message
调用包装在 on.exit
中,以确保在 计算后它是 运行 ,而不是 运行如果计算失败。如果这不重要,则将 on.exit(message(...))
替换为 message(...)
。请注意 add1
本身不使用任何包。
library(dplyr)
add1 <- function(numeric_vector) {
on.exit(message("You mutated ", substitute(numeric_vector)))
1 + numeric_vector
}
BOD %>% mutate(Time = add1(Time))
给予:
You mutated Time
Time demand
1 2 8.3
2 3 10.3
3 4 19.0
4 5 16.0
5 6 15.6
6 8 19.8
rlang
要使用 rlang,请使用该包中的 enexpr
。 dplyr 将使它可用。 enexpr
returns 一个 name
class 对象。
enexpr
类似于 substitute
但一个会影响处理的区别是 substitute
将提取承诺的代码部分,无论该承诺是否已被强制执行(即评估);然而,enexpr
将提取非强制承诺的代码,但将提取强制承诺的值。因为我们想要代码部分,所以我们必须确保 enexpr(numeric_vector)
是 运行 ,然后 numeric_vector
被用于计算。确保我们在开头引入一个新变量 arg_name
,即 运行,确保 enexpr
具有非强制参数。
library(dplyr)
add2 <- function(numeric_vector) {
arg_name <- enexpr(numeric_vector)
on.exit(message("You mutated ", arg_name))
1 + numeric_vector
}
BOD %>% mutate(Time = add2(Time))
我正在改变一列,以使用我编写的函数在 R 中创建一个新列。
在我的函数中,我想发送一条消息,其中包含正在更改的列的名称。
如何在 mutate 调用中从函数内部访问正在改变的列名?
可重现的例子:
data <- tribble(
~colB,
1,
2,
3
)
# Function that will be used in the mutate
add1 <- function(numeric_vector) {
return(1 +numeric_vector)
# I want to message the user the name of the column they are mutating
# This simply returns the entire vector
message("You mutated", numeric vector)
# This returns 'numeric_vector'
message("You mutated", quo_name(quo(numeric_vector)))
}
# Desired Output:
data %>%
mutate(colC = add1(colB))
You mutated colB
colB colC
<dbl> <dbl>
1 2
2 3
3 4
我想你想要
add1 <- function(numeric_vector) {
message(paste("You mutated", quo_name(enquo(numeric_vector))))
return(1 + numeric_vector)
}
请注意,您必须在 return()
之前打印您的消息。 return()
之后的任何内容都不会在函数中 运行 因为当你点击该语句时你退出了。此外,您可以使用 enquo()
获取变量以获取其名称。您需要在它仍处于承诺状态时获取它的名称,这意味着在您实际使用它的价值之前。
使用 substitute
其中 returns 一个 name
class 对象。我们已将 message
调用包装在 on.exit
中,以确保在 计算后它是 运行 ,而不是 运行如果计算失败。如果这不重要,则将 on.exit(message(...))
替换为 message(...)
。请注意 add1
本身不使用任何包。
library(dplyr)
add1 <- function(numeric_vector) {
on.exit(message("You mutated ", substitute(numeric_vector)))
1 + numeric_vector
}
BOD %>% mutate(Time = add1(Time))
给予:
You mutated Time
Time demand
1 2 8.3
2 3 10.3
3 4 19.0
4 5 16.0
5 6 15.6
6 8 19.8
rlang
要使用 rlang,请使用该包中的 enexpr
。 dplyr 将使它可用。 enexpr
returns 一个 name
class 对象。
enexpr
类似于 substitute
但一个会影响处理的区别是 substitute
将提取承诺的代码部分,无论该承诺是否已被强制执行(即评估);然而,enexpr
将提取非强制承诺的代码,但将提取强制承诺的值。因为我们想要代码部分,所以我们必须确保 enexpr(numeric_vector)
是 运行 ,然后 numeric_vector
被用于计算。确保我们在开头引入一个新变量 arg_name
,即 运行,确保 enexpr
具有非强制参数。
library(dplyr)
add2 <- function(numeric_vector) {
arg_name <- enexpr(numeric_vector)
on.exit(message("You mutated ", arg_name))
1 + numeric_vector
}
BOD %>% mutate(Time = add2(Time))