x = 10
rm(x) # removed x from the environment
x = 10
x %>% rm() # Doesn't remove the variable x
1)为什么管道技术不删除变量?
2) 我如何交替使用管道和 rm() 来删除变量?
脚注:这个问题可能类似于 Pipe in magrittr package is not working for function load()
最佳答案
使用 %<>%
用于将值分配给 NULL
的运算符
x %<>%
rm()
在管道中,我们获取的是值而不是对象。因此,通过使用
%<>%
即就地复合赋值运算符,'x' 的值被赋值为 NULLx
#NULL
如果我们需要删除对象,请将其传递为
character
字符串,将其提供给 list
rm
的论据这需要一个 character
对象,然后指定 environment
x <- 10
"x" %>%
rm(list = ., envir = .GlobalEnv)
当我们调用“x”时
x
Error: object 'x' not found
...
的原因不起作用的是对象 .
未在 rm
内评估x <- 10
"x" %>%
rm(envir = .GlobalEnv)
Warning message: In rm(., envir = .GlobalEnv) : object '.' not found
另一种选择是使用
do.call
x <- 10
"x" %>%
list(., envir = .GlobalEnv) %>%
do.call(rm, .)
x
Error: object 'x' not found
关于r - magrittr 包中的管道不适用于函数 rm(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49642537/