-2

So for the life of me I can’t understand this extremely simple concept.

take this code for example in this image.

This code looks to me like the code from hell. I was told the pipes are like Russian nesting dolls. Each statement before the pipes apparently feeds into the statement after the pipe. That’s what I was told. So what I don’t understand is first how can this continue to chain on endlessly and second, how does the pipe know where in the next statement the previous statement goes??

Let me take an example... read.csv2 ... %>% select(-x..otu)

And it keeps going on and on from there… How does it know where to put the previous statement or function in the next statement?

user18139
  • 188
  • 1
  • 3
  • 13
  • By default, in the first argument of the RHS, unless the `.` is supplied in which case that argument is used. – Hugh Sep 30 '17 at 02:15
  • 1
    You haven't read `help("%>%")` yet? – Rich Scriven Sep 30 '17 at 02:18
  • Please don't post an image of your code. Post the code itself. This way people can at least copy and paste.. – acylam Sep 30 '17 at 02:44
  • 1
    K then maybe [the magrittr vignette](https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html) would be more helpful. – Rich Scriven Sep 30 '17 at 03:33
  • @useR it’s not my code. I got it off google as an example and the URL didn’t work when I tried to copy it – user18139 Sep 30 '17 at 12:19
  • 1
    This is really an horrible example to learn how pipe work :). And horrible code altogether, browse `magrittr` and `dplyr` questions on stack overflow and you'll see a lot of much more elegant code that will show you the value of pipes. – moodymudskipper Oct 01 '17 at 16:07

1 Answers1

1

The default is to put it in the first argument. You can however use a . to override this default. Piping is very useful when you need to feed the output of a function into the input of another without creating intermediate variables that would populate your global environment. In your example code, no intermediate variables were created before ggplot outputs a plot. Without %>%, the only way to not create intermediate variables yet, at the same time, have complex operations is to nest all the functions, which would be even more confusing...

For example (made up):

df %>%
  group_by(id) %>%
  summarize(n()) %>%
  inner_join(df) %>%
  mutate(revenue = price*quantity)

would translate to:

mutate(inner_join(summarize(group_by(df, id), n()), df), revenue = price*quantity)

which is not particularly appealing to the eye...

acylam
  • 18,231
  • 5
  • 36
  • 45
  • So is it putting paste0 into read.CSV2, as its first argument? If you read it left to right from that point, it looks like it is not putting it into the first argument, but instead just chaining and moving onto the next argument. But on the flipside, I can imagine that is putting paste0 as first argument and then specifying that there is a header so it is true? – user18139 Sep 30 '17 at 02:23
  • @user18139 that's exactly right. in the first step though you see that there is a dot in `"v2" %>% paste0("otutable_", ., ".csv")`. This means that `"v2"` takes place at the dot instead of the first argument. Also see my edit – acylam Sep 30 '17 at 02:37
  • Ooooohhhhhh! So everywhere there is a dot, it is specifically placing that piped statement into that exact place? Like in GG plot as well? – user18139 Sep 30 '17 at 02:39
  • @user18139 yep, and you can even have multiple dots in the following pipe so it will replace for all the dots. – acylam Sep 30 '17 at 02:40
  • @user18139: as suggested above, read the [magrittr vignette](https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html)! – Vincent Bonhomme Sep 30 '17 at 06:51
  • @useR : basically, even looking at vignette, there are places where it seems a little bit out of order. Does magrittr have some intuition of how to abstractly place items? If you look at the very first example on that page, at the very end there is a transform statement that pipes to a multiply statement… And it does not put the commands into the multiply statement but instead more or less chains to them. How does it understand what it is doing basically? That’s what I don’t get. It sometimes puts something inside of another object, and other times does operations – user18139 Sep 30 '17 at 12:43
  • Also, for your example, with the mutate above, it puts the revenue statement dead last and put everything else before it. Which I don’t really understand. Why does it put it absolutely last? – user18139 Sep 30 '17 at 12:44