I'm wondering how to pipe data to make a plot and add points with the native R pipe operator |>
dtf = data.frame(pop1 = 1:10, pop2 = 2:11, gen = 1:10)
dtf |>
(\(dt) plot(x = dt[,"gen"], y = dt[,"pop1"]))() |>
(\(dt) points(x = dt[,"gen"], y = dt[,"pop2"]))()
The lines below work:
dtf |>
(\(dt) plot(x = dt[,"gen"], y = dt[,"pop1"]))()
But when adding the points, R 'forgot' that it was piping the data since there is no output from plot to pass it to points. Is there a way to 'continue' the pipe to feed the points function?
I figured out that this would work, but kind of misses the purpose of the pipe operator:
dtf |>
(function(dt) {
plot(x = dt[,"gen"],
y = dt[,"pop1"], pch = 19, col = "red")
points(x = dt[,"gen"],
y = dt[,"pop2"], pch = 19, col = "black")
}
)()