Introduction

This attempt provides a new help function for importas notation of R in a pipeable way. The R package importas mimic "import as **" notaion in Python. One of the advantages of this notation is that you can select function name by auto-completion. Another merit of the notation is to prevent name conflict. Be careful if you have functions in different packages that have the same name but work differently. The importas notation solves the problem.

In this work, I implemented a new help function for the importas package. I believe that the package would be more convenient with this kind of help function that can be called via the pipe operator.

For more details on using importas package, see materials by the original package author: the TokyoR slide https://atusy.github.io/tokyor88-importas/ and the GitHub repo https://github.com/atusy/importas.

remotes::install_github("atusy/importas")
devtools::install_github("tidyverse/magrittr")

Note that: magrittr_1.5.0.9000 or later is requireted to use “lazy” pipe.
See for detail, https://github.com/tidyverse/magrittr/issues/120.

library(importas)
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2.9000     ✓ purrr   0.3.4     
## ✓ tibble  3.0.3          ✓ dplyr   1.0.1     
## ✓ tidyr   1.1.1          ✓ stringr 1.4.0     
## ✓ readr   1.3.1          ✓ forcats 0.5.0
## ── Conflicts ───────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(palmerpenguins)

An usecase of importas package with palmerpenguins dataset

ggplot2 %as% gg
dplyr %as% dp
forcats %as% fc
penguins %>% 
  dp$mutate(sex = fc$fct_explicit_na(sex, "unknown")) %>% 
  gg$ggplot() +
  gg$geom_point(aes(x = flipper_length_mm, 
                    y = body_mass_g, 
                    colour = species, 
                    shape = sex), 
                size = 3, alpha = 0.8) + 
  gg$scale_color_manual(values = c("darkorange", "purple", "cyan4")) + 
  gg$labs(title = "Penguin visualization",
       subtitle = "Flipper length and body mass for Adelie, Chinstrap and Gentoo Penguins",
       x = "Flipper length (mm)",
       y = "Body mass (g)",
       color = "Penguin species",
       shape = "Penguin sex") + 
  gg$theme_classic()
## Warning: Removed 2 rows containing missing values (geom_point).

See for detail about Palmer penguins https://github.com/allisonhorst/palmerpenguins.

A new help function

help2 <- function(FUN){
  str_call <- base::sys.calls()[[1]] %>% 
    base::toString() %>% 
    stringr::str_split(pattern = "[, |\\(]", simplify = TRUE) %>% 
    .[3]
  if(str_call %>% stringr::str_detect("\\$")){
    # cat("importas notation, i.e., $ was detected.")
    queries <- str_call %>% 
      stringr::str_split(pattern = "\\$", simplify = TRUE)
    utils::help(topic = queries[, 2])
  }else if(str_call %>% stringr::str_detect(":::")){
    # cat("Standard notation, i.e., ::: was detected. ")
    queries <- str_call %>% 
      str_split(pattern = ":::", simplify = TRUE)
    utils::help(topic = queries[, 2], package = queries[, 1])
  }else if(str_call %>% stringr::str_detect("::")){
    # cat("Standard notation, i.e., :: was detected. ")
    queries <- str_call %>% 
      str_split(pattern = "::", simplify = TRUE)
    utils::help(topic = queries[, 2], package = queries[, 1])
  }else{
    cat("Please select package(s) to specify if needed.")
    utils::help(topic = str_call)
  }
}

Help usage

Call help by importas pkg$name notation via %>% operator. Brackets () and quotations "" are also accepted.

gg$aes_all %>% help2
gg$aes_all() %>% help2
"gg$aes_all()" %>% help2
"gg$aes_all" %>% help2

Call help by importas pkg$name notation without %>%.

help2(gg$aes_all)
help2(gg$aes_all())
help2("gg$aes_all")
help2("gg$aes_all()")

Standard pkg::name/pkg:::name notation is also accepted.

ggplot2::aes_all %>% help2
"ggplot2::aes_all" %>% help2
ggplot2::aes_all() %>% help2
"ggplot2::aes_all()" %>% help2
help2(ggplot2::aes_all())
help2("ggplot2::aes_all()")
help2(ggplot2::aes_all)
help2("ggplot2::aes_all")

Postscript

After the struggled attempt above, the package author taught me more sophisticated solutions to refer the environment properly as follows:

gg = importas::package(ggplot2)
h = function(t, ...) {
  S = substitute(t)
  C = as.character(S)
  do.call(help, `if`(
    C[1L] == "$" &&
      exists(C[2L], e <-
               rlang::caller_env()) &&
      inherits(a <-
                 get(C[2L], e = e), "importas"),
    list(to = C[3L], p = attr(alias, "p")),
    list(to = S, ...)
  ))
}
h(gg$aes)

via https://twitter.com/Atsushi776/status/1307637380547919872

sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] palmerpenguins_0.1.0 forcats_0.5.0        stringr_1.4.0       
##  [4] dplyr_1.0.1          purrr_0.3.4          readr_1.3.1         
##  [7] tidyr_1.1.1          tibble_3.0.3         ggplot2_3.3.2.9000  
## [10] tidyverse_1.3.0      importas_0.2.0      
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.1.0    xfun_0.16           haven_2.3.1        
##  [4] colorspace_1.4-1    vctrs_0.3.2         generics_0.0.2     
##  [7] htmltools_0.5.0     yaml_2.2.1          blob_1.2.1         
## [10] rlang_0.4.7         pillar_1.4.6        glue_1.4.1         
## [13] withr_2.2.0         DBI_1.1.0           dbplyr_1.4.4       
## [16] modelr_0.1.8        readxl_1.3.1        lifecycle_0.2.0    
## [19] munsell_0.5.0       gtable_0.3.0        cellranger_1.1.0   
## [22] rvest_0.3.6         evaluate_0.14       labeling_0.3       
## [25] knitr_1.29          fansi_0.4.1         broom_0.7.0        
## [28] Rcpp_1.0.5          scales_1.1.1        backports_1.1.9    
## [31] jsonlite_1.7.0      farver_2.0.3        fs_1.5.0           
## [34] hms_0.5.3           digest_0.6.25       stringi_1.4.6      
## [37] grid_4.0.2          cli_2.0.2           tools_4.0.2        
## [40] magrittr_1.5.0.9000 crayon_1.3.4        pkgconfig_2.0.3    
## [43] ellipsis_0.3.1      xml2_1.3.2          reprex_0.3.0       
## [46] lubridate_1.7.9     assertthat_0.2.1    rmarkdown_2.3      
## [49] httr_1.4.2          rstudioapi_0.11     R6_2.4.1           
## [52] compiler_4.0.2