Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

source() doesn't work ("node stack overflow")

Tags:

r

I have the following few lines of code in my R script called 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:

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

What is the problem?

like image 998
purcell Avatar asked Nov 28 '22 01:11

purcell


2 Answers

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

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

like image 155
Ben Bolker Avatar answered Dec 07 '22 17:12

Ben Bolker


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.

like image 29
yousisme Avatar answered Dec 07 '22 15:12

yousisme