简体   繁体   English

R函数包含在另一个文件中时需要包声明吗?

[英]R Functions require package declaration when they are included from another file?

I am writing some data manipulation scripts in R, and I finally decided to create an external .r file and call my functions from there. 我正在R中编写一些数据操作脚本,最后我决定创建一个外部.r文件并从那里调用函数。 But it started giving me some problems when I try calling some functions. 但是当我尝试调用某些函数时,它开始给我带来一些问题。 Simple example: 简单的例子:

This one works with no problem: 这可以正常工作:

change_column_names <- function(file,new_columns,seperation){
    new_data <- read.table(file, header=TRUE, sep=seperation)
    colnames(new_data) <- new_columns
    write.table(new_data, file=file, sep=seperation, quote=FALSE, row.names = FALSE)
}
change_column_names("myfile.txt",c("Column1", "Column2", "Cost"),"|")

When I crate a file "data_manipulation.r", and put the above change_column_names function in there, and do this 当我创建文件“ data_manipulation.r”并将其放在上面的change_column_names函数时,执行此操作

sys.source("data_manipulation.r")
change_column_names("myfile.txt",c("Column1", "Column2", "Cost"),"|")

it does not work. 这是行不通的。 It gives me could not find function "read.table" error. 它给了我could not find function "read.table"错误。 I fixed it by changing the function calls to util:::read.table and util:::write.table . 我通过将函数调用更改为util:::read.tableutil:::write.table来修复它。

But this kinda getting frustrating. 但这有点令人沮丧。 Now I have the same issue with the aggregate function, and I do not even know what package it belongs to. 现在,我对aggregate函数有同样的问题,我什至不知道它属于哪个包。

My questions: Which package aggregate belongs to? 我的问题:哪个软件包aggregate属于? How can I easily know what packages functions come from? 我如何轻松知道包功能来自何处? Any cleaner way to handle this issue? 有没有更清洁的方法来解决此问题?

The sys.source() by default evaluates inside the base environment (which is empty) rather than the global environment (where you usually evaluate code). 默认情况下, sys.source()在基本环境(空)中而不是在全局环境(通常在其中评估代码)中求值。 You probably should just be using source() instead. 您可能应该只使用source()

You can also see where functions come from by looking at their environment. 您还可以通过查看功能的环境来了解功能的来源。

environment(aggregate)
# <environment: namespace:stats>

For the first part of your question: If you want to find what package a function belongs to, and that function is working properly you can do one of two (probably more) things: 对于问题的第一部分:如果要查找某个函数所属的包,并且该函数正常工作,则可以执行以下两项操作(可能更多)之一:

1.) Access the help files 1.)访问帮助文件

?aggregate and you will see the package it belongs to in the top of the help file. ?aggregate ,您将在帮助文件的顶部看到该软件包所属的软件包。

Another way, is to simply type aggregate without any arguments into the R console: 另一种方法是在R控制台中简单地键入不带任何参数的aggregate

> aggregate
function (x, ...) 
UseMethod("aggregate")
<bytecode: 0x7fa7a2328b40>
<environment: namespace:stats>

The namespace is the package it belongs to. 名称空间是它所属的包。

2.) Both of those functions that you are having trouble with are base R functions and should always be loaded. 2)您遇到的这两个函数都是基本R函数,应始终加载。 I was not able to recreate the issue. 我无法重现该问题。 Try using source instead of sys.source and let me know if it alleviates your error. 尝试使用source而不是sys.source ,让我知道它是否可以减轻您的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM