[英]Pipe in magrittr package is not working for function rm()
x = 10
rm(x) # removed x from the environment
x = 10
x %>% rm() # Doesn't remove the variable x
1) Why doesn't pipe technique remove the variable? 1)为什么管道技术不删除变量?
2) How do I alternatively use pipe and rm() to remove a variable? 2) 我如何交替使用管道和 rm() 来删除变量?
Footnote: This question is perhaps similar to Pipe in magrittr package is not working for function load()脚注:这个问题可能类似于magrittr 包中的 Pipe is not working for function load()
Use the %<>%
operator for assigning the value to NULL
使用
%<>%
运算符将值分配给NULL
x %<>%
rm()
In the pipe, we are getting the value instead of the object.在管道中,我们获取的是值而不是对象。 So, by using the
%<>%
ie in place compound assignment operator, the value of 'x' is assigned to NULL因此,通过使用
%<>%
ie in place 复合赋值运算符,'x' 的值被赋值为 NULL
x
#NULL
If we need the object to be removed, pass it as character
string, feed it to the list
argument of rm
which takes a character
object and then specify the environment
如果我们需要的对象被删除,把它作为
character
的字符串,其提供给该list
的说法rm
这需要一个character
对象,然后指定environment
x <- 10
"x" %>%
rm(list = ., envir = .GlobalEnv)
When we call 'x'当我们调用“x”时
x
Error: object 'x' not found
错误:找不到对象“x”
The reason why the ...
doesn't work is that the object .
...
不起作用的原因是对象.
is not evaluated within the rm
不在
rm
内评估
x <- 10
"x" %>%
rm(envir = .GlobalEnv)
Warning message: In rm(., envir = .GlobalEnv) : object '.'
警告消息:在 rm(.,envir = .GlobalEnv) 中:对象 '.' not found
没有找到
Another option is using do.call
另一种选择是使用
do.call
x <- 10
"x" %>%
list(., envir = .GlobalEnv) %>%
do.call(rm, .)
x
Error: object 'x' not found
错误:找不到对象“x”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.