JohnCoene / datamaps

📍 datamaps htmlwidget for R
http://datamaps.john-coene.com/
Other
23 stars 3 forks source link

Issue with the development version of magrittr #2

Closed lionel- closed 4 years ago

lionel- commented 4 years ago

Hello, I'm seeing an issue with the development version of magrittr which switches to a lazy evaluation approach. It causes this error in your package:

states <- data.frame(ori.lon = c(-97.03720, -87.90446),
    ori.lat = c(32.89595, 41.97960),
    des.lon = c(-106.60919, -97.66987),
    des.lat = c(35.04022, 30.19453),
    strokeColor = c("blue", "red"),
    arcSharpness = c(2, 1))

states %>%
  datamaps(scope = "USA", default = "lightgray") %>%
  add_arcs(ori.lon, ori.lat, des.lon, des.lat, strokeColor)
#> Error in eval(substitute(origin.lon), data) :
#>   invalid 'envir' argument of type 'closure'

That's because the pipeline is now evaluated as if it were nested, instead of linearly:

add_arcs(
  datamaps(states, scope = "USA", default = "lightgray"),
  ori.lon,
  ori.lat,
  des.lon,
  des.lat,
  strokeColor
)
#> Error in eval(substitute(origin.lon), data) :
#>   invalid 'envir' argument of type 'closure'

Because of lazy evaluation, add_arcs() is run first, and datamaps() only when actually used inside add_arcs(). One ways to fix this is to force() the input like this:

diff --git a/R/add.R b/R/add.R
index 6e36bb7..834b559 100644
--- a/R/add.R
+++ b/R/add.R
@@ -206,6 +206,7 @@ add_arcs_name <- function(p, origin, destination, ...){
 #' @export
 add_arcs <- function(p, origin.lon, origin.lat, destination.lon, destination.lat, ...){

+  force(p)
   data <- get("data", envir = data_env)
   ori.lon <- eval(substitute(origin.lon), data)
   ori.lat <- eval(substitute(origin.lat), data)

We're planning to release magrittr in one month. It'd be great if you could update your package :)

Note that the future native pipe in the next version of R will use this same approach, so by fixing your package for the new magrittr behaviour you'll make it usable with the native pipe as well.

JohnCoene commented 4 years ago

Thank you Lionel, and great work on the pipe.