IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 102 forks source link

Replace depreciated dplyr::select_, dplyr::filter_ etc. functions #5198

Open dannyparsons opened 5 years ago

dannyparsons commented 5 years ago

The dplyr functions to use variables within their functions has been depreciated and replaced with a different system described here https://dplyr.tidyverse.org/articles/programming.html

There are now warnings that the old functions are depreciated so we need to replace them soon before they are removed completely and will break our code.

Here's an example of the new code which we should be able to adapt for most of our uses:

start_day <- "start"
end_day <- "end"
col_names <- c("year, "start", "end")
by <- "year"
season_data <- daily_data %>% 
  dplyr::select(!!! rlang::syms(col_names)) %>%
  dplyr::group_by(!!! rlang::syms(by)) %>%
  dplyr::summarise(!! rlang::sym(start_day) := dplyr::first(!! rlang::sym(start_day)),
                   !! rlang::sym(end_day) := dplyr::first(!! rlang::sym(end_day)))

In general:

dannyparsons commented 5 years ago

This is also the basis for our calculation system so very important to update correctly.

dannyparsons commented 5 years ago

For dplyr::filter, since we have the whole expression as a string we need something different, as described here: https://stackoverflow.com/questions/51665956/tidyeval-way-to-programatically-send-values-to-filter/51668207#51668207

filter(iris, !! rlang::parse_expr(string))

Note, this it is not recommend to use string inputs and this would be "better" in tidyverse if we passed in expressions not as strings instead.