csgillespie / efficientR

Efficient R programming: a book
https://csgillespie.github.io/efficientR/
Other
721 stars 375 forks source link

Using deprecated function rbind_all of dplyr package which is no longer supported and code fails #306

Open shafayetShafee opened 2 years ago

shafayetShafee commented 2 years ago

Code snippet from 1.6.3 Profiling contains a function dplyr::rbind_all() which is no longer supported the current version of dplyr package.

Code snippet from book

library("profvis")
profvis(expr = {

  library("ggplot2")
  out = readRDS("extdata/out-ice.Rds")
  df = dplyr::rbind_all(out, id = "Year")

  ggplot(df, aes(long, lat, group = paste(group, Year))) +
    geom_path(aes(colour = Year))
  ggsave("figures/icesheet-test.png")
}, interval = 0.01, prof_output = "ice-prof")

When tried to create a dataframe from out using rbind_all got the following error

extdata_path <- "https://raw.githubusercontent.com/csgillespie/efficientR/master/extdata/"
out <- readr::read_rds(paste0(extdata_path, "out-ice.Rds"))
df = dplyr::rbind_all(out, id = "Year")
#> Error: 'rbind_all' is not an exported object from 'namespace:dplyr'

Fix

Now dplyr offers bind_rows function to bind data-frames which should be used instead of rbind_all

extdata_path <- "https://raw.githubusercontent.com/csgillespie/efficientR/master/extdata/"
out <- readr::read_rds(paste0(extdata_path, "out-ice.Rds"))
df <- dplyr::bind_rows(out, .id = "Year")
  head(df)
#>   Year   long     lat order  hole piece id group
#> 1 1985 575000 5700000     1 FALSE     1  0   0.1
#> 2 1985 600000 5700000     2 FALSE     1  0   0.1
#> 3 1985 600000 5675000     3 FALSE     1  0   0.1
#> 4 1985 575000 5675000     4 FALSE     1  0   0.1
#> 5 1985 575000 5700000     5 FALSE     1  0   0.1
#> 6 1985 500000 5625000     1 FALSE     1  1   1.1

Please note that I am using dplyr version 1.0.8

packageVersion("dplyr")
#> [1] '1.0.8'
shafayetShafee commented 2 years ago

@Robinlovelace, Can I make a pull request suggesting this fix 😃?

Robinlovelace commented 2 years ago

@Robinlovelace, Can I make a pull request suggesting this fix smiley?

Yes, that would be greatly appreciated. Apologies for slow response..

shafayetShafee commented 2 years ago

@Robinlovelace, I need help with another thing, that is, which of the following approach should I follow to read the out-ice.Rds,

out = readRDS("extdata/out-ice.Rds")

which will not work for someone, who just wants to copy code from the book and run it.

Or,

extdata_path <- "https://raw.githubusercontent.com/csgillespie/efficientR/master/extdata/"
out <- readr::read_rds(paste0(extdata_path, "out-ice.Rds"))

which will require the reader to install the readr package and this should be mentioned in the prerequisites section of the 1st chapter of the book.

Thank you 😄!

Robinlovelace commented 2 years ago

I like the 2nd option better, good point!