I'd like to use $
at the end of a magrittr
/tidyverse
pipeline. $
works directly next to tidyverse
functions like read_csv
and filter
, but as soon I create a pipeline with %>%
it raises an error. Here is a simple reproducible example.
# Load libraries and create a dummy data file
library(dplyr)
library(readr)
write_csv(data_frame(x=c(0,1), y=c(0,2)), 'tmp.csv')
# This works
y <- read_csv('tmp.csv')$y
str(y)
# This also works
df_y <- read_csv('tmp.csv')
y <- filter(df_y, y > 0)$y
str(y)
# This does not work
y <- read_csv('tmp.csv') %>% filter(y > 0)$y
My questions are:
1) What are the underlying explanations/mechanics for why using $
at the end of a pipepline does not work?
2) What's a best practice way for what I am trying to accomplish? Specifically, to get a vector as the end result of a pipeline?