How to import an .R file and assign an alias to it? Like import myfile.R as mf

大憨熊 提交于 2021-02-18 08:17:15

问题


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').

回答1:


Sounds like you want to define an environment and source a file into it. I find sys.source useful for this.

I have an example file called "my_test_script.R" which contains:

MYCONSTANT <- 3

testfun <- function(val){
  print(val)
}

testfun2 <- function(x){
  return(x + MYCONSTANT)
}

Now an example session reading that file into an environment so I can 'alias' the information inside of it as 'tstEnv':

> tstEnv <- new.env()
> sys.source(file = "my_test_script.R", envir = tstEnv, toplevel.env = tstEnv)
> tstEnv$testfun("it works")
[1] "it works"
> tstEnv$testfun2(0) 
[1] 3
> tstEnv$testfun2(1)
[1] 4
> tstEnv$MYCONSTANT # I can read my constants too
[1] 3


来源:https://stackoverflow.com/questions/55521517/how-to-import-an-r-file-and-assign-an-alias-to-it-like-import-myfile-r-as-mf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!