- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在条件和ifelse分支中都使用转发结果的情况下,我在管道中使用ifelse。以下是简化版本:如果放在大括号内,ifelse +管道似乎只能处理与.
不同的条件。
library("magrittr")
FALSE %>% ifelse(., 'true', 'false')
#> [1] "false"
FALSE %>% ifelse(. == TRUE, 'true', 'false')
#> Error in ifelse(., . == TRUE, "true", "false"): unused argument ("false")
FALSE %>% {ifelse(. == TRUE, 'true', 'false')}
#> [1] "false"
library("magrittr")
NULL %>% ifelse(is.null(.), "", as.character(.))
#> Error in ifelse(., is.null(.), "", as.character(.)): unused argument (as.character(.))
NULL %>% {ifelse(is.null(.), "", as.character(.))}
#> [1] ""
{}
对我来说已经足够好,但是我想了解这种现象的原因。
.
还是使用
.
之间没有区别,这是我的问题的重点。
最佳答案
magrittr
文档说,在嵌套函数调用中使用点时,其行为如您所见。
Using the dot for secondary purposes
Often, some attribute or property of lhs is desired in the rhs call in addition to the value of lhs itself, e.g. the number of rows or columns. It is perfectly valid to use the dot placeholder several times in the rhs call, but by design the behavior is slightly different when using it inside nested function calls. In particular, if the placeholder is only used in a nested function call, lhs will also be placed as the first argument! The reason for this is that in most use-cases this produces the most readable code.
For example,iris %>% subset(1:nrow(.) %% 2 == 0)
is equivalent toiris %>% subset(., 1:nrow(.) %% 2 == 0)
but slightly more compact. It is possible to overrule this behavior by enclosing the rhs in braces. For example,1:10 %>% {c(min(.), max(.))} is equivalent to c(min(1:10), max(1:10))
.
ifelse
中的一个单独的函数调用,因此它具有这种行为。
关于磁悬浮管(%>%)具有不良行为的原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47109900/
我在使用 Heroku 时遇到问题,当我尝试部署我的应用程序时,它没有运行 run assets:precompile而且我还没有在本地预编译我的 Assets ,我在 Git 中没有 list 文件
(这是为您提供围绕我的问题的背景信息。您可以跳到“问题”并阅读该内容,然后如果您想直接进入主题,则可以返回并浏览背景知识.抱歉,这是一面文字墙!) 我需要将一堆非常非常糟糕的 JSON 存储在数据库中
我是一名优秀的程序员,十分优秀!