R beginner here, who really misses Python's
import pandas as pd
import my_file_which_is_just_a_file_not_a_package as mf
out = mf.my_cool_function()
I have found a way to implement something similar to the former (assigning an alias to a package), but how to do the latter, i.e. how to assign an alias to an .R file (not a package) you are importing? E.g. you have put some functions you use frequently into a separate .R file, or you are dividing your program into multiple .R files to keep things tidy.
NB: this is NOT a duplicate of questions asking how to assign an alias to a package - I am talking about single R files, not packages.
I understand that the namespace library provides a functionality similar to import as
for installed packages:
library(namespace)
registerNamespace('ggp', loadNamespace('ggplot2'))
data(iris)
ggp::ggplot(iris, ggp::aes(x = Petal.Length, y = Sepal.Length)) + ggp::geom_point()
I also understand that you can use import to import only certain functions from another .R file, e.g. from another script (not a package):
import::here(fun_a, fun_b, .from = "other_resources.R")
a <- fun_a(…)
Finally, you can use source to load another script.
But none of this addresses my point about importing with an alias.
Edit
Since I was asked, I am trying to learn some R because:
- I am curious; last time I tried R was before the tidyverse "revolution"; I hated it at the time, found it obscure, arcane, and with extremely poor documentation. Everyone's saying how wonderful tidyverse is, so I'm curious to give R another try
- I am unlikely to migrate all my workflow from Python to R, but there might be certain things that I might want to do in R. An example is reading large Excel files and exporting to SQL; both of these tasks are much faster in R. I can now import large xlsx files into SQL in a fraction of the time, then get Python to read from SQL and leave the rest of my workflow unchanged. I posted about it here, where I also explained why CSV is not the best option for me (please do not reply saying 'use CSV').