How to finish work with assignment operator?

I want to end a pipe with an assignment operator in R.

my goal (in pseudo R):

data %>% analysis functions %>% analyzedData 

where the data and the data being analyzed are like data.frame.

I tried several variations of this, each of which gave a unique error message. some iterations I've tried:

 data %>% analysis functions %>% -> analyzedData data %>% analysis functions %>% .-> analyzedData data %>% analysis functions %>% <-. analyzedData data %>% analysis functions %>% <- analyzedData 

Error messages:

 Error in function_list[[k]](value) : could not find function "analyzedData" Error: object 'analyzedData' not found Error: unexpected assignment in: .. 

Update: the way I decided to do this:

 data %>% do analysis %>% {.} -> analyzedData 

Thus, in order to eliminate / debug a long channel, you can delete these two lines in your channel in order to minimize code repetition and isolate the problem.

 data %>% pipeline functions %>% {.}-> tempWayPoint tmpWayPoint %>% more pipeline functions %>% {.} -> endPipe 
+8
r dplyr magrittr
source share
4 answers

It may be easiest to complete the task first (e.g. mentioning scoa), but if you really want to put it at the end, you can use assign

 mtcars %>% group_by(cyl) %>% summarize(m = mean(hp)) %>% assign("bar", .) 

which will save the output to "bar"

Alternatively, you can simply use the -> operator. You mentioned this in your question, but it looks like you are using something like

 mtcars %>% -> yourvariable 

instead

 mtcars -> yourvariable 

You do not want to have %>% before ->

+6
source share

It looks like you are trying to decorate the %>% pipeline operator with the side effect of creating a new object. We can assume that for this you can use the assignment operator -> , but it will not work in the pipeline. This is because -> has a lower priority than user-defined operators such as %>% , which interferes with parsing: your pipeline will be parsed as (initial_stages) -> (final_stages) , which is pointless.

The solution is to replace -> with the user version. While we are doing this, we could also use the lazyeval package to make sure that it will create an object where it should go:

 `%->%` <- function(value, x) { x <- lazyeval::lazy(x) assign(deparse(x$expr), value, x$env) value } 

Usage example:

 smry <- mtcars %>% group_by(cyl) %->% # ->, not > tmp %>% summarise(m=mean(mpg)) tmp #Source: local data frame [32 x 11] #Groups: cyl # # mpg cyl disp hp drat wt qsec vs am gear carb #1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 #2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 #3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 #4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 #5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 #.. ... ... ... ... ... ... ... .. .. ... ... smry #Source: local data frame [3 x 2] # # cyl m #1 4 26.66364 #2 6 19.74286 #3 8 15.10000 
+5
source share

You can think of a pipe chain as a multi-line function that works like any other multi-line function. The usual way to save the output is to assign it to the first line:

 analyzedData <- data %>% analysis functions 

Like you:

 plot <- ggplot(data,aes(x=x,y=x)) + geom_point() 
+4
source share

Update: the way I decided to do this: data %>% do analysis %>% {.} -> analyzedData

Thus, in order to eliminate / debug a long channel, you can delete these two lines in your channel in order to minimize code repetition and isolate the problem.

 data %>% pipeline functions %>% {.}-> tempWayPoint tmpWayPoint %>% more pipeline functions %>% {.} -> endPipe 

If you have a better way to do this, please let me know.

+2
source share

All Articles