jbkunst / highcharter

R wrapper for highcharts
http://jkunst.com/highcharter/
Other
718 stars 147 forks source link

plotly and highcharter don't work well together when using hcaes_ #728

Closed antonio-peran closed 1 year ago

antonio-peran commented 2 years ago

If you load both plotly and highcharter libraries, doing a simple plot throws an error.

# insert reprex here
library(highcharter)
library(plotly)
highchart() %>% hc_add_series(iris, hcaes_(x = "Sepal.Length", y = "Sepal.Width", group = "Species"), type = "scatter")
Error: Problem with `mutate()` column `group`.
i `group = ~Species`.
x `group` must be a vector, not a `formula` object.
Run `rlang::last_error()` to see where the error occurred.
antonio-peran commented 2 years ago

Hi again :wave:

I have been working on the issue and have discovered that the problem is with the function hcaes_, which has a different behaviours depending on whether you have loaded the package plotly or not.

The function is defined as follows

function (x, y, ...) 
{
  mapping <- list(...)
  if (!missing(x)) {
    mapping["x"] <- list(x)
  }
  if (!missing(y)) {
    mapping["y"] <- list(y)
  }
  mapping <- lapply(mapping, function(x) {
    if (is.character(x)) {
      parse(text = x)[[1]]
    }
    else {
      x
    }
  })
  mapping <- structure(mapping, class = "uneval")
  mapping <- mapping[names(mapping) != ""]
  class(mapping) <- c("hcaes", class(mapping))
  mapping
}

and the problem is with the lines

  mapping <- structure(mapping, class = "uneval")
  mapping <- mapping[names(mapping) != ""]

When the package plotly is not loaded the second line above removes the class "uneval" from mapping and the code works perfectly. On the other hand, when the package plotly is loaded the class "uneval" persists and the execution throws an error.

Therefore, a posible solution is to delete the line mapping <- structure(mapping, class = "uneval") from the definition of the funcion hcaes_. This works fine for me with or without loading plotly, but I don't know if the class "uneval" is necessary for other reasons.

I hope this helps :relaxed:

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Feel free to reopen it if you find it necessary.