Importing functions in R
Question: In Python we have chance to import a certain function from a library with a
command "import _function_ from _library_ **as smth**. Do we have something
similar in R? I know that we can call the function like "_library_
::_function_()", my question mostly refers to the "as" part.


Answer: It is not common and not necessary to do this in R. The assignment operator
`<-` can be used to give a new name to an existing function. For example, one
could define a function that does exactly the same as lubridate's, `year()`
function with:

    
    
    asYear <- lubridate::year
    

One could argue that, by doing so, the `year()` function has been "imported"
from the `lubridate` package and that it is now called `asYear()`. In fact,
the new function does just the same (which is no surprise, simply because it
_is_ the same):

    
    
    asYear(Sys.Date())
    #[1] 2016
    

So it is possible to construct an analogy to "from package import as", but it
is not a good idea to do this. Here are a few reasons I can think of:

  * Debugging a code where library functions have been renamed will be much more difficult.
  * The documentation is not available for the renamed function. In this example, `?asYear` won't work, in contrast to `?lubridate::year` or `library(lubridate); help(year)`.
  * The function is not only renamed but it is copied, which clutters the environment and is inefficient in terms of memory usage.
  * The maintenance of the code becomes unnecessarily difficult. If another programmer (or the original programmer a few years later) looks at a code containing such a redefinition of a function, it will be harder for her or him to understand what this function is doing. 

There are probably more reasons, but I hope that this is sufficient to
discourage the use of such a construction. Different programming languages
have different peculiarities, and as a programmer it is necessary to adapt to
them. What is common in Python can be awkward in R, and vice versa.

A simple and commonly used way to handle such a standard situation in R is to
use `library()` to load the entire namespace of the package containing the
requested function:

    
    
    library (lubridate)
    year(Sys.Date())
    

However, one should be aware of possible namespace clashes, especially if many
libraries are loaded simultaneously. Different functions could be defined with
the same name in different packages. A well-known example thereof are the
contrasting implementations of the `lag()` function in the `dplyr` and `stats`
package. In such cases one can use the double colon operator `::` to resolve
the namespace that should be addressed. This would be similar to the use of
"from" in the case of "import", but such a specification would be needed each
time the function is called.

    
    
    lubridate::year(Sys.Date()) 
    #[1] 2016
    

