R记录脚本的显示名称 [英] R Logging display name of the script

查看:118
本文介绍了R记录脚本的显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前问题的一个原子例子:

this is an atomic example of my current issue:

目前,我有一个包含多个R脚本的项目(全部位于名为DIR的同一目录中).我在DIR采购所有R文件时都有一个主要脚本,其中包含basicconfig:

For the moment I have a project containing several R scripts (all in the same directory named DIR). I have a main script in DIR sourcing all the R files and, containing a basicconfig:

basicConfig()

我在DIR中使用了两个脚本dog.r和cat.r.我目前在这些脚本中只有一个功能.在dog.r中:

I take two scripts in DIR, dog.r and cat.r. I have currently only one function in these scripts. In dog.r :

feedDog <- function(){
    loginfo("The dog is happy to eat!", logger="dog.r")

}

在cat.r中:

feedCat <- function(){
     loginfo("The cat is voracious", logger="cat.r")
}

这个例子很好.但是实际上,我每个人都有大约20个脚本和20条可能的错误消息.这样就不用写了:

It's fine with this example. But in real I have something like 20 scripts and 20 possible error messages in each. So that instead of writting:

loginfo("some message", logger="name of script")

我想写:

loginfo("some message", logger=logger)

并配置其他记录器. 问题是,如果我在每个R脚本中都声明一个记录器,那么当我使用main来获取所有文件时,只会考虑一个记录器.我不知道该如何绕过此问题.

And configure different loggers. The issue is that if I declare a logger in each R scripts, only one will be taken into account when I source all files with my main ... I dunno how to bypass this issue.

PS:在Python中,可以在每个文件中定义一个记录器,并自动采用如下脚本名称:

PS: in Python it is possible to define a logger in each file taking automatically the name of the script like this:

logger = logging.getLogger(__name__)

但是我担心R中不可能吗?

But I am afraid it is not possible in R ?

推荐答案

如果您的source()是文件,则在该文件中创建的函数将具有一个名为srcref的属性,该属性存储了源文件中该函数的位置来自.如果您有一个指向该函数的名称,则可以使用getSrcFilename来获取该函数来自的文件名.例如,创建一个我们可以获取的文件

If your source() a file, the functions created in that file will have an attribute called srcref that stored the location from the sourced file that the function came from. If you have a name that points to that function, you can use getSrcFilename to get the filename the function came from. For example, create a file that we can source

# -- testthis.R --

testthis <- function() {
    loginfo("Hello")
}

现在,如果我们输入R,就可以运行

Now if we enter R, we can run

loginfo <-function(msg) {
    fnname <- sys.call(-1)[[1]]
    fnfile <- getSrcFilename(eval(fnname))
    paste(msg, "from", deparse(fnname), "in", fnfile)
}

source("testthis.R")

testthis()
# [1] "Hello from testthis in testthis.R"

函数loginfo使用sys.call(-1)查看从中调用了什么函数.然后,它从该调用中提取名称(使用[[1]]),然后使用eval()将那个名称"对象转换为实际函数.一旦有了该功能,就可以获取源文件名.这与运行相同

The function loginfo uses sys.call(-1) to see what function it was called from. Then it extracts the name from that call (with [[1]]) and then we use eval() to turn that "name" object into the actual function. Once we have the function, we can get the source file name. This is the same as running

getSrcFilename(testthis)

(如果您已经知道函数的名称).因此,这可能有点棘手.我相信这个特殊属性只会添加到函数中.除此之外,每个源文件都不会获得其自己的名称空间或任何名称空间,因此它们不能各自拥有自己的记录器.

if you already knew the name of the function. So it is possible, it's just a bit tricky. I believe this special attribute is only added to functions. Other than that, each source file doesn't get it's own namespace or anything so they can't each have their own logger.

这篇关于R记录脚本的显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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