Skip to content

Instantly share code, notes, and snippets.

@maurolepore
Created February 25, 2022 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maurolepore/2e4aa250a59c9bf950a06dadddabd851 to your computer and use it in GitHub Desktop.
Save maurolepore/2e4aa250a59c9bf950a06dadddabd851 to your computer and use it in GitHub Desktop.
# https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/delayedAssign

# The code of any pkg, e.g. r2dii.data -----------------------------------------
# R/abcd_demo.R
abcd_demo <- head(mtcars)
delayedAssign("ald_demo", {
  warning("`ald_demo` is retired. Instead use `abcd_demo`")
  abcd_demo
})

# The code of the user ---------------------------------------------------------
# library(pkg)

# This works, of course.
abcd_demo
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

# Normally this would fail because pkg::ald_demo does not exist
# But thanks to R's dark magic (i.e. lazy evaluation), it works!
ald_demo
#> Warning: `ald_demo` is retired. Instead use `abcd_demo`
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Created on 2022-02-25 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment