Combination of pipes and spot filler in R

I am new to R and I am trying to understand the%>% operator and the use of ".". (point). The following code works as a simple example.

library(magrittr) library(ensurer) ensure_data.frame <- ensures_that(is.data.frame(.)) data.frame(x = 5) %>% ensure_data.frame 

However, the following code does not work

 ensure_data.frame <- ensures_that(. %>% is.data.frame) data.frame(x = 5) %>% ensure_data.frame 

where I will now associate the placeholder in the is.data.frame method.

I assume this is my understanding of the point placeholder constraints / interpretation, which is lagging, but can anyone clarify this?

+4
source share
2 answers

The "problem" is that magrittr has a short notation for anonymous functions:

 . %>% is.data.frame 

roughly coincides with

 function(.) is.data.frame(.) 

In other words, when the point is the left (leftmost) side, the pipe has a special behavior.

You can avoid behavior in several ways, for example.

 (.) %>% is.data.frame 

or in any other way when the LHS is not identical . In this particular example, this may seem like an undesirable behavior, but usually in such examples there really is no need to bind the first expression, therefore is.data.frame(.) Is as expressive as . %>% is.data.frame . %>% is.data.frame , and examples like

 data %>% some_action %>% lapply(. %>% some_other_action %>% final_action) 

can be considered more understandable than

 data %>% some_action %>% lapply(function(.) final_action(some_other_action(.))) 
+1
source

This is the problem:

 . = data.frame(x = 5) a = data.frame(x = 5) a %>% is.data.frame #[1] TRUE . %>% is.data.frame #Functional sequence with the following components: # # 1. is.data.frame(.) # #Use 'functions' to extract the individual functions. 

Looks like a mistake, but dplyr experts can listen.

A simple workaround in your expression is .[] %>% is.data.frame .

0
source

Source: https://habr.com/ru/post/1258083/


All Articles