源()不工作(“节点堆栈溢出”)

时间:2022-08-23 14:52:46

I have the following few lines of code in my R script called assign1.R:

在我的R脚本中,我有以下几行代码,称为assign1.R:

(u <- c(1, 1, 0, 1, 0)) # a)
u[3] # b)
ones_u <- which(u == 1) # c)
ones_u
source("assign1.R")

Only, the source() function does not work. R shows me the following error message:

只有source()函数不起作用。R向我展示了以下错误信息:

Error in match(x, table, nomatch = 0L) : node stack overflow
Error during wrapup: node stack overflow

What is the problem?

这个问题是什么?

2 个解决方案

#1


12  

I didn't get exactly the same error you did, but I was able to get something pretty similar with a trivial example:

我没有得到和你完全相同的错误,但是我可以得到一个很相似的例子:

writeLines("source('badsource.R')",con="badsource.R")
source("badsource.R")
## Error in guess(ll) : node stack overflow

As one of the comments above states, the file you're sourcing is trying to source() itself.

作为上面的注释之一,您正在寻找的文件正在尝试源()本身。

This is how you would test for that possibility from within R, without just opening the file in a text editor (which is a much more sensible approach):

这是在R内测试这种可能性的方法,而不只是在文本编辑器中打开文件(这是一种更明智的方法):

grepl("source('badsource.R')",readLines("badsource.R"),fixed=TRUE)  ## TRUE

(obviously you should fill in the name of your assignment file here ...)

(显然,你应该在这里填写你的作业文件的名称…)

It feels like you should have noticed this yourself, but I'm answering anyway because the problem is delightfully recursive ...

感觉你应该自己注意到这一点,但我还是要回答,因为这个问题是递归的。

#2


3  

Your are sourcing the file that you are in. That source() line of code should be deleted. If you are sourcing some code from another R file then you would use the source() function, otherwise there is no need to source another file. Also, if all the code works in the one file without running other bits of code in other files, it is likely that you already have the code you need and you wouldn't need to source another file.

你正在寻找你所在的文件。应该删除该源代码()行。如果您正在从另一个R文件中寻找一些代码,那么您将使用source()函数,否则就不需要为另一个文件提供源代码。另外,如果所有的代码都在一个文件中工作,而不运行其他文件中的其他代码,那么很可能您已经拥有了所需的代码,并且您不需要从另一个文件中获取代码。

#1


12  

I didn't get exactly the same error you did, but I was able to get something pretty similar with a trivial example:

我没有得到和你完全相同的错误,但是我可以得到一个很相似的例子:

writeLines("source('badsource.R')",con="badsource.R")
source("badsource.R")
## Error in guess(ll) : node stack overflow

As one of the comments above states, the file you're sourcing is trying to source() itself.

作为上面的注释之一,您正在寻找的文件正在尝试源()本身。

This is how you would test for that possibility from within R, without just opening the file in a text editor (which is a much more sensible approach):

这是在R内测试这种可能性的方法,而不只是在文本编辑器中打开文件(这是一种更明智的方法):

grepl("source('badsource.R')",readLines("badsource.R"),fixed=TRUE)  ## TRUE

(obviously you should fill in the name of your assignment file here ...)

(显然,你应该在这里填写你的作业文件的名称…)

It feels like you should have noticed this yourself, but I'm answering anyway because the problem is delightfully recursive ...

感觉你应该自己注意到这一点,但我还是要回答,因为这个问题是递归的。

#2


3  

Your are sourcing the file that you are in. That source() line of code should be deleted. If you are sourcing some code from another R file then you would use the source() function, otherwise there is no need to source another file. Also, if all the code works in the one file without running other bits of code in other files, it is likely that you already have the code you need and you wouldn't need to source another file.

你正在寻找你所在的文件。应该删除该源代码()行。如果您正在从另一个R文件中寻找一些代码,那么您将使用source()函数,否则就不需要为另一个文件提供源代码。另外,如果所有的代码都在一个文件中工作,而不运行其他文件中的其他代码,那么很可能您已经拥有了所需的代码,并且您不需要从另一个文件中获取代码。