Generate import calls.
Ever got bitten by conflicting symbols in the packages you attach via library()
?
The import
package offers a solution
by specifying exactly which symbols to load into a dedicated environment,
instead of attaching the whole package to the search path.
But what if you actually want all (or almost all) symbols from a package?
import.gen::from("magrittr", "tidyr", .output = "cat")
## # The imports below were generated using the following call:
## # import.gen::from("magrittr", "tidyr")
## # The following symbols are duplicates and therefore not imported:
## # import::from("magrittr", "extract")
## import::from("magrittr", "%<>%", "%>%", "%$%", "add", "and",
## "debug_fseq", "debug_pipe", "divide_by", "divide_by_int",
## "equals", "extract2", "freduce", "functions", "inset", "inset2",
## "is_greater_than", "is_in", "is_less_than", "is_weakly_greater_than",
## "is_weakly_less_than", "mod", "multiply_by", "multiply_by_matrix",
## "n'est pas", "not", "or", "raise_to_power", "set_attr", "set_attributes",
## "set_colnames", "set_names", "set_rownames", "subtract",
## "%T>%", "tamper", "undebug_fseq", "use_series")
## import::from("tidyr", "expand", "expand_", "extract", "extract_",
## "extract_numeric", "gather", "gather_", "separate", "separate_",
## "seq_range", "spread", "spread_", "unite", "unite_", "unnest",
## "unnest_")
By default, the generated code is copied to the clipboard so that the developer can paste it at the appropriate place right away.
But what's the difference to library()
then?
The autogenerated import::from
calls will continue working even if a package
that has been imported from introduces a new conflicting symbol.
In the example above, imagine tidyr
adds a set_names
function.
Attaching magrittr
and then tidyr
will overwrite the set_names
function,
which will probably break code that used this function.
In contrast, the import declaration remains unchanged unless we choose to
rerun the import.gen::from
call.
The package is at a very early stage but should work as advertised above.
It uses the clipr
package by Matthew Lincoln.
Install the package via:
devtools::install_github("krlmlr/import.gen")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.