library(magrittr)
myDf <- dataSetSubsetting
%>% !is.na(.[,c("colx")])
%>% !is.na(.[,c("coly")])
%>% someFunction(.[,c("colx", "coly")], .)
我认为magrittr
的工作原理。但是,它给我一个错误:
384: myDf <- dataSetSubsetting %>% !is.na(.[,c("colx")])
385: %>%
^
为什么?!
答案 0 :(得分:2)
以下是基于erasmortg评论的更详细解释。
library(dplyr)
#* Works
mtcars %>%
mutate(am = factor(am, 0:1, c("Automatic", "Manual")))
#* Fails: the interpreter looks at the end of the first
#* line and sees no operator, so it prints 'mtcars'
#* and declares the operation complete.
#* It then looks at the second line and can't find
#* the left hand side for '%>%', causing an error.
mtcars
%>% mutate(am = factor(am, 0:1, c("Automatic", "Manual")))
#* Fails: Similar story to the previous example. In this case,
#* printing is delayed until all of the statements within
#* the braces successfully run.
{mtcars
%>% mutate(am = factor(am, 0:1, c("Automatic", "Manual")))}
#* Succeeds: The interpreter sees that you've opened
#* with parentheses, and tries to connect all of the
#* lines into a coherent statement. With the parentheses,
#* when the interpreter sees no operator at the end of
#* 'mtcars', it says to itself, "I hope I find one on the
#* next line" and tries to piece it together.
(mtcars
%>% mutate(am = factor(am, 0:1, c("Automatic", "Manual"))))