beanumber / openWAR

An R package enabling the computation of openWAR using MLBAM data
99 stars 33 forks source link

Error in makeWar() after upgrading dplyr to 0.7.1 #106

Closed davidbmitchell closed 6 years ago

davidbmitchell commented 7 years ago

I stumbled across this today when I was playing around with openWAR at work. In short, I created a new instance on https://r-brain.io/ and installed the newest dplyr, 0.7.1. After a while I came to the conclusion it had to be something that changed in dplyr.

So I came home, updated dplyr on my computer, previously version 0.5.0, and sure enough, the error happened again.

I just ran this code of my computer at home, which is the same as I ran today at work.

Going through the dplyr repository it looks like there have been some major changes starting in April. I need to dig into this more to figure out what needs to be changed, and if there are any other functions that could be affected.

madeWAR <- makeWAR(MLBAM2017)
....Supplied Run Expectancy model does not have a predict method...
....Building in-sample Run Expectancy Model...
....Run Expectancy Model....
          [,1]      [,2]      [,3]
[1,] 0.5177847 0.2821832 0.1139380
[2,] 0.9053099 0.5298013 0.2418268
[3,] 1.1374686 0.7128954 0.3493837
[4,] 1.4806265 0.9529954 0.4715166
[5,] 1.4462810 0.9719626 0.3935018
[6,] 1.7551020 1.2121212 0.5348460
[7,] 2.0213675 1.5156794 0.6203125
[8,] 2.2559727 1.6905371 0.8752759
...Estimating Expected Runs...
...Estimating Fielding Runs Above Average...
....Computing the collective fielding model...
....Applying the collective fielding model...
....Building a fielding model for each position...
...Estimating Pitching Runs Above Average...
....Supplied Pitching model does not have a predict method...
....Building in-sample Pitching Model...
....Pitching Model....
factor(venueId)2395 factor(venueId)2889 throws == standTRUE    factor(venueId)7   factor(venueId)31 
      -0.0253132285       -0.0167998161       -0.0141063108       -0.0094229148       -0.0093261577 
   factor(venueId)3 factor(venueId)2392 factor(venueId)2680   factor(venueId)22   factor(venueId)17 
      -0.0089505106       -0.0076612816       -0.0055697471       -0.0039089821       -0.0020721808 
  factor(venueId)14    factor(venueId)5   factor(venueId)12    factor(venueId)2    factor(venueId)4 
      -0.0019026819       -0.0009889544       -0.0006968479        0.0016059584        0.0017267697 
factor(venueId)3289         (Intercept)   factor(venueId)32 factor(venueId)4169 factor(venueId)2681 
       0.0025096824        0.0031511049        0.0033043977        0.0042138362        0.0046351811 
factor(venueId)4705 factor(venueId)3309   factor(venueId)15  factor(venueId)680 factor(venueId)2394 
       0.0048401269        0.0063958823        0.0108375495        0.0108409605        0.0110774996 
  factor(venueId)10 factor(venueId)2602 factor(venueId)3313   factor(venueId)19   factor(venueId)13 
       0.0131867130        0.0175934692        0.0180891776        0.0192938422        0.0194564420 
factor(venueId)3312 
       0.0195039233 
Error in mutate_impl(.data, dots) : 
  Evaluation error: $ operator is invalid for atomic vectors.
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.4

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2       openWAR_0.2.3.9001

loaded via a namespace (and not attached):
 [1] splines_3.3.3       lattice_0.20-34     mosaic_0.14.4       colorspace_1.3-2    htmltools_0.3.5    
 [6] base64enc_0.1-3     survival_2.40-1     XML_3.98-1.6        rlang_0.1.1         foreign_0.8-67     
[11] glue_1.1.1          withr_1.0.2         RColorBrewer_1.1-2  bindr_0.1           plyr_1.8.4         
[16] stringr_1.2.0       munsell_0.4.3       gtable_0.2.0        devtools_1.12.0     htmlwidgets_0.8    
[21] memoise_1.0.0       latticeExtra_0.6-28 knitr_1.15.1        curl_2.4            htmlTable_1.9      
[26] Rcpp_0.12.11        acepack_1.4.1       KernSmooth_2.23-15  readr_1.1.0         scales_0.4.1       
[31] backports_1.0.5     checkmate_1.8.2     Hmisc_4.0-2         Sxslt_0.91-4        gridExtra_2.2.1    
[36] mosaicData_0.14.0   hms_0.3             ggplot2_2.2.1.9000  digest_0.6.12       stringi_1.1.3      
[41] dplyr_0.7.1.9000    grid_3.3.3          tools_3.3.3         magrittr_1.5        lazyeval_0.2.0     
[46] tibble_1.3.3        Formula_1.2-1       cluster_2.0.5       ggdendro_0.1-20     tidyr_0.6.1        
[51] pkgconfig_2.0.1     MASS_7.3-45         Matrix_1.2-8        data.table_1.10.0   assertthat_0.2.0   
[56] httr_1.2.1          R6_2.2.2            rpart_4.1-10        nnet_7.3-12         git2r_0.18.0  
davidbmitchell commented 7 years ago

Forgot to add the traceback:

Error in mutate_impl(.data, dots) : 
  Evaluation error: $ operator is invalid for atomic vectors. 
10. stop(structure(list(message = "Evaluation error: $ operator is invalid for atomic vectors.", call = mutate_impl(.data, dots), 
cppstack = NULL), .Names = c("message", "call", "cppstack"), class = c("Rcpp::eval_error", "C++Error", 
"error", "condition"))) 
9. mutate_impl(.data, dots) 
8. mutate_.tbl_df(x$data, raa.pitch = ~predict(mod.pitch, newdata = x$data[, c("venueId", "throws", "stand")]) - delta.pitch) 
7. mutate_(x$data, raa.pitch = ~predict(mod.pitch, newdata = x$data[, c("venueId", "throws", "stand")]) - delta.pitch) at makeWAR.R#152
6. makeWAR.GameDayPlays_Fielding(x, models, verbose, low.memory, step, ...) at makeWAR.R#76
5. makeWAR(x, models, verbose, low.memory, step, ...) at makeWAR.R#127
4. makeWAR.GameDayPlays_Runs(out, models, verbose, low.memory, step,  ...) at makeWAR.R#76
3. makeWAR(out, models, verbose, low.memory, step, ...) at makeWAR.R#101
2. makeWAR.GameDayPlays(May) at makeWAR.R#76
1. makeWAR(May) 
beanumber commented 7 years ago

Hmm...OK, I will investigate. Or I would be happy to field a pull request if you find the problem.

davidbmitchell commented 7 years ago

I believe dplyr is breaking some functions because of the new approach to Non-Standard Evaluation they are using called "tidyeval". I'm still trying to wrap my head around it. I made a change on Line 152 of makeWAR.R. I wrapped mutate in quo(), removed the underscore and the tilde in front of predict. This fixed the error, but I don't know 100% why.

x$data <- quo(mutate(x$data, raa.pitch = predict(mod.pitch, newdata = x$data[, c("venueId", "throws", "stand")]) - delta.pitch)) 

With that being said, it threw another error later on Line 169

  mod.off <- getModelOffense(select_(x$data, "delta", "venueId", "throws", "stand"), 
                            models[["offense"]], verbose)
Error in UseMethod("select_") : 
  no applicable method for 'select_' applied to an object of class "c('quosure', 'formula')"

I just need to learn more about "tidyeval" to figure out why some functions are running fine and others are throwing errors.