klmr / box

Write reusable, composable and modular R code
https://klmr.me/box/
MIT License
829 stars 47 forks source link

using function alias and module alias together leads to creation of a binding in the global namespace #357

Closed idavydov closed 3 months ago

idavydov commented 4 months ago

Error description

box::use(hm = ComplexHeatmap[heatmap = Heatmap, ...])
hm$heatmap
#> Error in hm$heatmap: name 'heatmap' not found in 'hm'
heatmap
#> function (matrix, col, name, na_col = "grey", color_space = "LAB", 
#>     rect_gp = gpar(col = NA), border = NA, border_gp = gpar(col = "black"), 
#>     cell_fun = NULL, layer_fun = NULL, jitter = FALSE, row_title = character(0), 
#>     row_title_side = c("left", "right"), row_title_gp = gpar(fontsize = 13.2), 
#>     row_title_rot = switch(row_title_side[1], left = 90, right = 270), 
#>     column_title = character(0), column_title_side = c("top", 
#>         "bottom"), column_title_gp = gpar(fontsize = 13.2), column_title_rot = 0, 
#>     cluster_rows = TRUE, cluster_row_slices = TRUE, clustering_distance_rows = "euclidean", 
#>     clustering_method_rows = "complete", row_dend_side = c("left", 
#>         "right"), row_dend_width = unit(10, "mm"), show_row_dend = TRUE, 
#>     row_dend_reorder = is.logical(cluster_rows) || is.function(cluster_rows), 
#>     row_dend_gp = gpar(), cluster_columns = TRUE, cluster_column_slices = TRUE, 
#>     clustering_distance_columns = "euclidean", clustering_method_columns = "complete", 
#>     column_dend_side = c("top", "bottom"), column_dend_height = unit(10, 
#>         "mm"), show_column_dend = TRUE, column_dend_gp = gpar(), 
#>     column_dend_reorder = is.logical(cluster_columns) || is.function(cluster_columns), 
#>     row_order = NULL, column_order = NULL, row_labels = rownames(matrix), 
#>     row_names_side = c("right", "left"), show_row_names = TRUE, 
#>     row_names_max_width = unit(6, "cm"), row_names_gp = gpar(fontsize = 12), 
#>     row_names_rot = 0, row_names_centered = FALSE, column_labels = colnames(matrix), 
#>     column_names_side = c("bottom", "top"), show_column_names = TRUE, 
#>     column_names_max_height = unit(6, "cm"), column_names_gp = gpar(fontsize = 12), 
#>     column_names_rot = 90, column_names_centered = FALSE, top_annotation = NULL, 
#>     bottom_annotation = NULL, left_annotation = NULL, right_annotation = NULL, 
#>     km = 1, split = NULL, row_km = km, row_km_repeats = 1, row_split = split, 
#>     column_km = 1, column_km_repeats = 1, column_split = NULL, 
#>     gap = unit(1, "mm"), row_gap = unit(1, "mm"), column_gap = unit(1, 
#>         "mm"), show_parent_dend_line = ht_opt$show_parent_dend_line, 
#>     heatmap_width = unit(1, "npc"), width = NULL, heatmap_height = unit(1, 
#>         "npc"), height = NULL, show_heatmap_legend = TRUE, heatmap_legend_param = list(title = name), 
#>     use_raster = NULL, raster_device = c("png", "jpeg", "tiff", 
#>         "CairoPNG", "CairoJPEG", "CairoTIFF", "agg_png"), raster_quality = 1, 
#>     raster_device_param = list(), raster_resize_mat = FALSE, 
#>     raster_by_magick = requireNamespace("magick", quietly = TRUE), 
#>     raster_magick_filter = NULL, post_fun = NULL) 
#> {
#> ...
#> }
#> <bytecode: 0xa6e60f0>
#> <environment: namespace:ComplexHeatmap>

Created on 2024-03-06 with reprex v2.1.0

R version

platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          3.0                         
year           2023                        
month          04                          
day            21                          
svn rev        84292                       
language       R                           
version.string R version 4.3.0 (2023-04-21)
nickname       Already Tomorrow

‘box’ version

1.2.0

klmr commented 3 months ago

This is by design: only the attached name is changed, not the name imported from the module. This is analogous to how e.g. Python or JavaScript handle aliases.

In principle it would be possible to allow renaming the exports inside the module, but since this would be changing the import’s API I’m not sure there’s a strong enough reason to implement it, even though I personally viscerally hate e.g. “fauxspaces” (stringr::str_*, or forcats::fct_*), and I am dreaming of having a way of eradicating them when importing such packages using ‘box’.

idavydov commented 3 months ago

Thanks, @klmr .

Not sure I would personally use renaming capabilities. But if implemented, I would imagine something like this:

box::use(hm = ComplexHeatmap[.rename = c("heatmap" = "Heatmap")])

where .rename could also receive a function:

box::use(stringr[.rename = \(x) grep("^str_", "", x)])