I'm preparing a function to graph trigonometric functions. I have written a function which successfully returns the correct value when given the parameters for single value of theta, but when I attempt to pass a data frame with a column for each parameter using purrr::pmap_dfr() in order to produce a set of values to graph, I get an error whose message doesn't appear to match the situation.
Here a reproducible example:
thetas <- seq(from = -3, to = 3, by = 0.05)
amplitudes <- rep(1, times = length(thetas))
period_denoms<- rep(1, times = length(thetas))
horizontal_shifts <- rep(0, times = length(thetas))
vertical_shifts <- rep(0, times = length(thetas))
argList <- list(thetas,
amplitudes,
period_denoms,
horizontal_shifts,
vertical_shifts) %>%
as_tibble(.name_repair = ~ c("theta",
"amplitude",
"period_denom",
"horizontal_shift",
"vertical_shift")
)
xformed_sine <- function(theta, amplitude, period_denom, horizontal_shift, vertical_shift){
period <- (2 * pi)/abs(period_denom)
return(as.double(amplitude * sinpi(period(theta + horizontal_shift)) + vertical_shift))
}
pmap_dfr(argList, xformed_sine)
R returns this error:
"Error in dplyr::bind_rows()
:
! Argument 1 must be a data frame or a named atomic vector."
As I read the error, R is complaining that the object argList is not a data frame, yet when I run this code: is.data.frame(argList)
the returned value is TRUE.
The return from that last bit doesn't match the error message, which tells me that argList isn't a data frame. Both can't be true, and I suspect that either I misunderstand the error message, or there is a bug in the pmap_dfr() function.
Any thoughts?
The error is referring to what your
xformed_sine()
function returns. You can make it a tibble and your code will run:Alternately, just keep your
xformed_sine()
as is and usepmap_dbl()
.A little extra explanation: There's a hint that purrr is hitting an error after it completes the mapping function, as it fails when trying to bind rows on
res
(res = result of map operation) inrlang::last_error()
:It is kind of an ambiguous error message though, I can see how it is confusing without any additional context.