Last active
April 22, 2025 15:02
-
-
Save yjunechoe/ad49bccf035507251255f64893c3503d to your computer and use it in GitHub Desktop.
Difference between `get()` vs. `getFromNamespace()` and function vs. lazydata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Without dplyr loaded ---- | |
isNamespaceLoaded("dplyr") | |
#> [1] FALSE | |
## FUNCTION: Both work for getting `dplyr::mutate` | |
get("mutate", envir = asNamespace("dplyr")) | |
#> function (.data, ...) | |
#> { | |
#> UseMethod("mutate") | |
#> } | |
#> <bytecode: 0x000001f61ffaf840> | |
#> <environment: namespace:dplyr> | |
getFromNamespace("mutate", "dplyr") | |
#> function (.data, ...) | |
#> { | |
#> UseMethod("mutate") | |
#> } | |
#> <bytecode: 0x000001f61ffaf840> | |
#> <environment: namespace:dplyr> | |
## LAZY DATA: Neither works for getting `dplyr::storms` | |
get("storms", envir = asNamespace("dplyr")) | |
#> Error in get("storms", envir = asNamespace("dplyr")): object 'storms' not found | |
getFromNamespace("storms", "dplyr") | |
#> Error in get(x, envir = ns, inherits = FALSE): object 'storms' not found | |
# With dplyr loaded ---- | |
library(dplyr) | |
isNamespaceLoaded("dplyr") | |
#> [1] TRUE | |
## FUNCTION: Both continue to work for `dplyr::mutate` | |
get("mutate", envir = asNamespace("dplyr")) | |
#> function (.data, ...) | |
#> { | |
#> UseMethod("mutate") | |
#> } | |
#> <bytecode: 0x000001f61ffaf840> | |
#> <environment: namespace:dplyr> | |
getFromNamespace("mutate", "dplyr") | |
#> function (.data, ...) | |
#> { | |
#> UseMethod("mutate") | |
#> } | |
#> <bytecode: 0x000001f61ffaf840> | |
#> <environment: namespace:dplyr> | |
## LAZY DATA: `get()` now works, `getFromNamespace()` still fails | |
get("storms", envir = asNamespace("dplyr")) |> head(1) | |
#> # A tibble: 1 × 13 | |
#> name year month day hour lat long status category wind pressure | |
#> <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <fct> <dbl> <int> <int> | |
#> 1 Amy 1975 6 27 0 27.5 -79 tropical de… NA 25 1013 | |
#> # ℹ 2 more variables: tropicalstorm_force_diameter <int>, | |
#> # hurricane_force_diameter <int> | |
getFromNamespace("storms", "dplyr") | |
#> Error in get(x, envir = ns, inherits = FALSE): object 'storms' not found |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://fosstodon.org/@[email protected]/113506139107148206