Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tidyeval to hcaes and mutate_mapping #423

Merged
merged 5 commits into from
Apr 17, 2018
Merged

Conversation

cnolanminich
Copy link

Fixes #420

Updated primarily based on this ggplot2 commit that used rlang in the definition of aes().

Tested against the examples with ggplot2 2.2.1.9000’, and ran a couple of our apps that use both ggplot2 and highcharter

@jbkunst
Copy link
Owner

jbkunst commented Apr 11, 2018

Hey @cnolanminich ,

This THANK YOU is not enough to express all my gratitude. I will test to merge this PR.

Hope @PaulC91 @peeyooshc @tungmilan can test after the merge.

Thanks again and give me your name/email if you want to be added to the DESCRIPTION file as contributor.

@cnolanminich
Copy link
Author

@jbkunst thank you for this excellent package! I added my name to the DESCRIPTION, ran devtools::check() to make sure everything was formatted properly, and while doing so, noticed two functions from rlang that I had previously neglected to add to the NAMESPACE

@tungttnguyen
Copy link

tungttnguyen commented Apr 12, 2018

Okay I've finished testing with both CRAN & dev versions of ggplot2 and @cnolanminich 's changes worked perfectly with new tidyeval and old examples posted by @jbkunst

I think it would be a good idea to include at least one working example with this PR either in the NEWS.md or somewhere else

Here is an example taken from the issue that I opened few weeks ago

library(tidyverse)
library(lubridate)
library(highcharter)
library(nycflights13)

flights_2 <- flights %>% 
  mutate(departure_month = ymd(str_c(year, month, "01", sep = "-"))) %>% 
  group_by(departure_month) %>% 
  summarize(arrival_delay = mean(arr_delay, na.rm = TRUE))
flights_2

hchart_func <- function(df, x, y) {
  x <- enexpr(x)
  y <- enexpr(y)
  hchart(df, 
         type = "line",
         hcaes(!!x, !!y),
         name = "Average Arrival Delay"
  )
}

hchart_func(df = flights_2, x = departure_month, y = arrival_delay)

rplot

@tungttnguyen
Copy link

tungttnguyen commented Apr 12, 2018

Another (simpler) example:

x <- "displ"
y <- "cyl"

x <- rlang::sym(x)
y <- rlang::sym(y)

hchart(mpg, "point", hcaes(UQ(x), UQ(y)))

rplot04

@tungttnguyen
Copy link

tungttnguyen commented Apr 12, 2018

Testing old examples:


library("igraph")
N <- 40

net <- sample_gnp(N, p = 2/N)
wc <- cluster_walktrap(net)

V(net)$label <- seq(N)
V(net)$name <- paste("I'm #", seq(N))
V(net)$page_rank <- round(page.rank(net)$vector, 2)
V(net)$betweenness <- round(betweenness(net), 2)
V(net)$degree <- degree(net)
V(net)$size <- V(net)$degree
V(net)$comm <- membership(wc)
V(net)$color <- colorize(membership(wc))

hchart(net, layout = layout_with_fr)

rplot02

x <- stl(log(AirPassengers), "per")
hchart(x)

rplot03

data(mpg, diamonds, package = "ggplot2")

hchart(mpg, "point", hcaes(displ, hwy, group = drv), regression = TRUE) %>% 
  hc_colors(c("#d35400", "#2980b9", "#2ecc71")) %>% 
  hc_add_dependency("plugins/highcharts-regression.js")

rplot05

Session Info:

Session info -------------------------------------------------------------------
 version  R version 3.4.4 (2018-03-15)
 system   x86_64, mingw32             
 ui       RStudio (1.1.444)        

Packages -----------------------------------------------------------------------
 package      * version    date       source                                   
 assertthat     0.2.0      2017-04-11 CRAN (R 3.4.4)                           
 base         * 3.4.4      2018-03-15 local                                    
 bindr          0.1.1      2018-03-13 CRAN (R 3.4.4)                           
 bindrcpp     * 0.2.2      2018-03-29 CRAN (R 3.4.4)                           
 broom          0.4.4      2018-03-29 CRAN (R 3.4.4)                           
 cellranger     1.1.0      2016-07-27 CRAN (R 3.4.4)                           
 cli            1.0.0      2017-11-05 CRAN (R 3.4.4)                           
 colorspace     1.3-2      2016-12-14 CRAN (R 3.4.4)                           
 compiler       3.4.4      2018-03-15 local                                    
 cowplot        0.9.2.9900 2018-04-07 Github (wilkelab/cowplot@bb819a1)        
 crayon         1.3.4      2017-09-16 CRAN (R 3.4.4)                           
 curl           3.2        2018-03-28 CRAN (R 3.4.4)                           
 data.table     1.10.4-3   2017-10-27 CRAN (R 3.4.4)                           
 datasets     * 3.4.4      2018-03-15 local                                    
 devtools       1.13.5     2018-02-18 CRAN (R 3.4.3)                           
 digest         0.6.15     2018-01-28 CRAN (R 3.4.3)                           
 dplyr        * 0.7.4.9004 2018-04-12 Github (tidyverse/dplyr@30953f1)         
 forcats      * 0.3.0      2018-02-19 CRAN (R 3.4.4)                           
 foreign        0.8-69     2017-06-22 CRAN (R 3.4.4)                           
 fortunes       1.5-4      2016-12-29 CRAN (R 3.4.1)                           
 GGally         1.3.2      2017-08-02 CRAN (R 3.4.4)                           
 ggplot2      * 2.2.1.9000 2018-04-12 Github (tidyverse/ggplot2@3c9c504)       
 glue           1.2.0      2017-10-29 CRAN (R 3.4.4)                           
 graphics     * 3.4.4      2018-03-15 local                                    
 grDevices    * 3.4.4      2018-03-15 local                                    
 grid           3.4.4      2018-03-15 local                                    
 gtable         0.2.0      2016-02-26 CRAN (R 3.4.4)                           
 haven          1.1.1      2018-01-18 CRAN (R 3.4.4)                           
 highcharter  * 0.6.0      2018-04-12 Github (cnolanminich/highcharter@85a0d56)
 hms            0.4.2      2018-04-07 Github (tidyverse/hms@c0cfc01)           
 htmltools      0.3.6      2017-04-28 CRAN (R 3.4.4)                           
 htmlwidgets    1.0        2018-01-20 CRAN (R 3.4.4)                           
 httr           1.3.1      2017-08-20 CRAN (R 3.4.4)                           
 igraph       * 1.2.1      2018-03-10 CRAN (R 3.4.4)                           
 jsonlite       1.5        2017-06-01 CRAN (R 3.4.4)                           
 labeling       0.3        2014-08-23 CRAN (R 3.4.1)                           
 lattice        0.20-35    2017-03-25 CRAN (R 3.4.4)                           
 lazyeval       0.2.1      2017-10-29 CRAN (R 3.4.4)                           
 lubridate    * 1.7.2      2018-02-06 CRAN (R 3.4.3)                           
 magrittr       1.5        2014-11-22 CRAN (R 3.4.4)                           
 memoise        1.1.0      2017-04-21 CRAN (R 3.4.4)                           
 methods      * 3.4.4      2018-03-15 local                                    
 mnormt         1.5-5      2016-10-15 CRAN (R 3.4.1)                           
 modelr         0.1.1      2017-07-24 CRAN (R 3.4.4)                           
 munsell        0.4.3      2016-02-13 CRAN (R 3.4.4)                           
 nlme           3.1-137    2018-04-07 CRAN (R 3.4.4)                           
 nycflights13 * 0.2.2      2017-01-27 CRAN (R 3.4.4)                           
 parallel       3.4.4      2018-03-15 local                                    
 pillar         1.2.1      2018-02-27 CRAN (R 3.4.4)                           
 pkgconfig      2.0.1      2017-03-21 CRAN (R 3.4.4)                           
 plyr           1.8.4      2016-06-08 CRAN (R 3.4.4)                           
 psych          1.8.3.3    2018-03-30 CRAN (R 3.4.4)                           
 purrr        * 0.2.4      2017-10-18 CRAN (R 3.4.4)                           
 quantmod       0.4-12     2017-12-10 CRAN (R 3.4.4)                           
 R6             2.2.2      2017-06-17 CRAN (R 3.4.4)                           
 raster         2.6-7      2017-11-13 CRAN (R 3.4.4)                           
 RColorBrewer   1.1-2      2014-12-07 CRAN (R 3.4.1)                           
 Rcpp           0.12.16    2018-03-13 CRAN (R 3.4.4)                           
 readr        * 1.2.0      2018-04-07 Github (tidyverse/readr@23eb46d)         
 readxl         1.0.0.9000 2018-04-06 Github (tidyverse/readxl@eeeebf8)        
 reshape        0.8.7      2017-08-06 CRAN (R 3.4.4)                           
 reshape2       1.4.3      2017-12-11 CRAN (R 3.4.4)                           
 rlang        * 0.2.0.9001 2018-04-12 Github (r-lib/rlang@70d2d40)             
 rlist          0.4.6.1    2016-04-04 CRAN (R 3.4.4)                           
 rstudioapi     0.7        2017-09-07 CRAN (R 3.4.4)                           
 rvest          0.3.2      2016-06-17 CRAN (R 3.4.3)                           
 scales         0.5.0.9000 2018-04-12 Github (hadley/scales@d767915)           
 sp             1.2-7      2018-01-19 CRAN (R 3.4.3)                           
 stats        * 3.4.4      2018-03-15 local                                    
 stringi        1.1.7      2018-03-12 CRAN (R 3.4.4)                           
 stringr      * 1.3.0      2018-02-19 CRAN (R 3.4.4)                           
 tibble       * 1.4.2      2018-01-22 CRAN (R 3.4.4)                           
 tidyr        * 0.8.0      2018-01-29 CRAN (R 3.4.4)                           
 tidyselect     0.2.4      2018-02-26 CRAN (R 3.4.4)                           
 tidyverse    * 1.2.1      2017-11-14 CRAN (R 3.4.4)                           
 tools          3.4.4      2018-03-15 local                                    
 TTR            0.23-3     2018-01-24 CRAN (R 3.4.3)                           
 utf8           1.1.3      2018-01-03 CRAN (R 3.4.4)                           
 utils        * 3.4.4      2018-03-15 local                                    
 whisker        0.3-2      2013-04-28 CRAN (R 3.4.4)                           
 withr          2.1.2      2018-03-15 CRAN (R 3.4.4)                           
 xml2           1.2.0      2018-01-24 CRAN (R 3.4.3)                           
 xts            0.10-2     2018-03-14 CRAN (R 3.4.4)                           
 yaml           2.1.18     2018-03-08 CRAN (R 3.4.4)                           
 zoo            1.8-1      2018-01-08 CRAN (R 3.4.4)


if(drop)
data <- select_(data, .dots = names(mapping))
newv <- quos(names(mapping))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reuse newv which is already defined in line # 409?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tungmilan 👍 good call --

I realized while doing this that it would be better to use rlang::syms() vs rlang::quos(), as in the following in muate_mapping. If using rlang::quos() and re-using newv I get the following error: Error: newvmust resolve to integer column positions, not a list. Anyway, I think syms() makes more sense anyway.
If you agree, I can push the version using syms() and we can test again :)

mutate_mapping <- function(data, mapping, drop = FALSE) {
  
  stopifnot(is.data.frame(data), inherits(mapping, "hcaes"), inherits(drop, "logical"))
  
  # https://stackoverflow.com/questions/45359917/dplyr-0-7-equivalent-for-deprecated-mutate
  # https://www.johnmackintosh.com/2018-02-19-theory-free-tidyeval/
  
  tran <- as.character(mapping)
  newv <- names(mapping)
  list_names <- setNames(tran, newv) %>% lapply(rlang::parse_quosure)
  
  data <- dplyr::mutate(data, !!! list_names)
  # Reserverd  highcharts names (#241)
  if(has_name(data, "series"))
    #old <- "seriess"
    #new <- "series"
    data <- dplyr::rename(data, "seriess" = "series")
  
  if(drop)
    newv <- rlang::syms(newv)
    data <- dplyr::select(data, !!! newv)
  
  data
  
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that. Let me know when it's ready for testing again :)

@cnolanminich
Copy link
Author

@tungmilan made the switch you suggested -- let me know how your tests go!

@tungttnguyen
Copy link

@cnolanminich : great! will do it later today

@tungttnguyen
Copy link

@cnolanminich : I've finished the testing. Everything looks good. I think it's ready :)

@cnolanminich
Copy link
Author

@jbkunst let me know if there's anything else you need on my end for this to be merged

@jbkunst
Copy link
Owner

jbkunst commented Apr 17, 2018

Ok! @cnolanminich @tungmilan let's merge guys.

Thanks you all!

@jbkunst jbkunst merged commit ccfdbe5 into jbkunst:master Apr 17, 2018
@PaulC91
Copy link
Contributor

PaulC91 commented Apr 17, 2018

great job guys. much appreciated! @cnolanminich @tungmilan @jbkunst

ari-nz pushed a commit to ari-nz/highcharter that referenced this pull request May 23, 2020
Add tidyeval to `hcaes` and `mutate_mapping`, thnks to @cnolanminich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants