In R, I have a function that outputs a function (a function factory, as it is called).
However, when piping an input to it with %>%, strange errors result.
Here is a reduced example:
ax <- function(a) {
function(x) a*x
}
library(magrittr)
3 %>% ax() %>% plot # Error in a * x : non-numeric argument to binary operator
ax(3) %>% plot # works, a plot of y=3x is produced
Hadley's book led me to try inserting force(a) into the function body of ax.
Now, a plot is produced in both cases.
But I am confused as to why this works. To me, the two cases are essentially identical. Why then is force() needed for the first case, and not the second?