如何导入 .R 文件并为其分配别名?像 import myfile.R as mf [英] How to import an .R file and assign an alias to it? Like import myfile.R as mf

查看:42
本文介绍了如何导入 .R 文件并为其分配别名?像 import myfile.R as mf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的 R 初学者,真的很怀念 Python 的

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()

我找到了一种实现与前者类似的方法(为包分配别名),但是如何实现后者,即如何为 .R 文件(不是包)分配别名您正在进口? 例如您已将一些经常使用的函数放入单独的 .R 文件中,或者您将程序分成多个 .R 文件以保持整洁.

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.

注意:这不是询问如何为包分配别名的问题的重复 - 我在谈论单个 R 文件,而不是包.

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.

我知道命名空间库为已安装的包提供了类似于 import as 的功能:

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()

我也知道您可以使用 import 仅从另一个 .R 文件导入某些功能,例如来自另一个脚本(不是包):

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(…)

最后,您可以使用 source加载另一个脚本.

Finally, you can use source to load another script.

但是这些都没有解决我关于使用别名导入的观点.

But none of this addresses my point about importing with an alias.

自从有人问我以来,我正在尝试学习一些 R 语言,因为:

Since I was asked, I am trying to learn some R because:

  • 我很好奇;我上次尝试 R 是在 tidyverse 革命"之前;我当时讨厌它,发现它晦涩难懂,而且文档极其糟糕.每个人都说 tidyverse 有多棒,所以我很想再试一次 R
  • 我不太可能将所有工作流程从 Python 迁移到 R,但我可能希望在 R 中执行某些操作.例如,读取大型 Excel 文件并导出到 SQL;这两个任务在 R 中都快得多.我现在可以在很短的时间内将大型 xlsx 文件导入 SQL,然后让 Python 从 SQL 中读取,并保持我的工作流程的其余部分不变.我发布了关于它这里,我还解释了为什么 CSV 不是我的最佳选择(请不要回复说使用 CSV").
  • 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').

推荐答案

听起来您想要定义一个环境并将一个文件导入其中.我发现 sys.source 对此很有用.

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

我有一个名为my_test_script.R"的示例文件,其中包含:

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)
}

现在是一个将该文件读入环境的示例会话,以便我可以将其中的信息别名"为tstEnv":

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

这篇关于如何导入 .R 文件并为其分配别名?像 import myfile.R as mf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆