├── NAMESPACE ├── .Rbuildignore ├── .gitignore ├── importar.Rproj ├── DESCRIPTION ├── man ├── import.Rd └── import_fun.Rd ├── README.md └── R └── importar.R /NAMESPACE: -------------------------------------------------------------------------------- 1 | exportPattern("^[[:alpha:]]+") 2 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /importar.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: importar 2 | Type: Package 3 | Title: Enables Importing/Loading of Packages or Functions While Creating an Alias for Them 4 | Version: 0.1.2 5 | Author: Andrea Cantieni 6 | Maintainer: Andrea Cantieni 7 | Description: Enables 'Python'-like importing/loading of packages or functions 8 | with aliasing to prevent namespace conflicts. 9 | URL: https://github.com/andreaphsz/importar 10 | License: GPL-3 11 | Encoding: UTF-8 12 | LazyData: true 13 | RoxygenNote: 6.0.1 14 | Suggests: dplyr 15 | Imports: utils 16 | -------------------------------------------------------------------------------- /man/import.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/importar.R 3 | \name{import} 4 | \alias{import} 5 | \title{This function imports/loads packages as in 'Python', i.e., ``import package as alias''} 6 | \usage{ 7 | import(package, alias) 8 | } 9 | \arguments{ 10 | \item{package}{Package name (unquoted).} 11 | 12 | \item{alias}{Alias (unquoted) for the package.} 13 | } 14 | \description{ 15 | This function imports/loads packages as in 'Python', i.e., ``import package as alias'' 16 | } 17 | \examples{ 18 | import(dplyr, d) 19 | df <- data.frame(a=1:3, b=4:6) 20 | df \%>\% d$filter(a == 2) 21 | } 22 | -------------------------------------------------------------------------------- /man/import_fun.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/importar.R 3 | \name{import_fun} 4 | \alias{import_fun} 5 | \title{This function imports/loads functions as in 'Python', i.e., ``from package import function as alias''} 6 | \usage{ 7 | import_fun(package, fun, alias) 8 | } 9 | \arguments{ 10 | \item{package}{Package name (unquoted).} 11 | 12 | \item{fun}{Function name (unquoted).} 13 | 14 | \item{alias}{Alias (unquoted) for the function.} 15 | } 16 | \description{ 17 | This function imports/loads functions as in 'Python', i.e., ``from package import function as alias'' 18 | } 19 | \examples{ 20 | import_fun(dplyr, filter, fil) 21 | df <- data.frame(a=1:3, b=4:6) 22 | fil(df, a == 2) 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # importar 2 | 3 | The goal of 'importar' is to prevent namespace conflicts by having loaded a lot of R packages. So 'importar' makes it easy to assign a (short) alias to a package or a function. 4 | 5 | ## Installation 6 | 7 | You can install 'importar' from GitHub with: 8 | 9 | ``` r 10 | devtools::install_github("andreaphsz/importar") 11 | ``` 12 | 13 | or from CRAN with: 14 | 15 | ``` r 16 | install.packages("importar") 17 | ``` 18 | 19 | ## Example 20 | 21 | This is an example which shows you how to assign a short alias to the 'dplyr' package. 22 | 23 | ``` r 24 | ## assign 'd' to 'dplyr' and use 'dplyr' functions by invoking the '$' operator. 25 | import(dplyr, d) 26 | df <- data.frame(a=1:3, b=4:6) 27 | df %>% d$filter(a == 2) 28 | ``` 29 | 30 | You can also assign an alias to functions to prevent namespace conflicts. 31 | ```r 32 | import_fun(dplyr, filter, fil) 33 | df <- data.frame(a=1:3, b=4:6) 34 | fil(df, a == 2) 35 | ``` 36 | -------------------------------------------------------------------------------- /R/importar.R: -------------------------------------------------------------------------------- 1 | #' This function imports/loads packages as in 'Python', i.e., ``import package as alias'' 2 | #' 3 | #' @param package Package name (unquoted). 4 | #' @param alias Alias (unquoted) for the package. 5 | #' @examples 6 | #' import(dplyr, d) 7 | #' df <- data.frame(a=1:3, b=4:6) 8 | #' df %>% d$filter(a == 2) 9 | import <- function(package, alias) { 10 | if(missing(package) | missing(alias)) 11 | stop("Both arguments must be passed.", call. = FALSE) 12 | package. <- as.character(substitute(package)) 13 | alias. <- as.character(substitute(alias)) 14 | base::suppressPackageStartupMessages(library(package., character.only=TRUE)) 15 | base::assign(alias., loadNamespace(package.), inherits = TRUE) 16 | } 17 | 18 | #' This function imports/loads functions as in 'Python', i.e., ``from package import function as alias'' 19 | #' 20 | #' @param package Package name (unquoted). 21 | #' @param fun Function name (unquoted). 22 | #' @param alias Alias (unquoted) for the function. 23 | #' @examples 24 | #' import_fun(dplyr, filter, fil) 25 | #' df <- data.frame(a=1:3, b=4:6) 26 | #' fil(df, a == 2) 27 | import_fun <- function(package, fun, alias) { 28 | if(missing(package) | missing(fun) | missing(alias)) 29 | stop("All three arguments must be passed.", call. = FALSE) 30 | alias. <- as.character(substitute(alias)) 31 | fun. <- as.character(substitute(fun)) 32 | package. <- as.character(substitute(package)) 33 | fun.p <- utils::getFromNamespace(fun., package.) 34 | base::assign(alias., fun.p, inherits = TRUE) 35 | } 36 | --------------------------------------------------------------------------------