Reputation: 121
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?
Upvotes: 6
Views: 15832
Reputation: 2367
I had the same "node stack overflow"
error when I run source("myfile.R")
and it took me hours to figured out what was causing it, which eventually I was able to do by elimination (eliminating line by line my code in "myfile.R")
So this is what I found:
Indeed (as noted in other answers to this question) there was another line source ("myfile.R")
which however I had inside the FALSE if statement, like shown below:
# myfile.R
# Bunch of useful functions goes below
...
# Then in the end of the file I have a block of "commented out" codes to test these functions. However I use `if (F)` to comment them out (not `#` sign)
if (F) {
source ("myfile.R")
# testing my functions below
}
So, apparently, even though it is inside the FALSE part of the if
statement, when i compiled the code from rmarkdown, it was still trying to access the codes inside this FALSE part! - causing the infinite call and error message.
Once I removed this line (actually uncommenting it using #
, the error was gone). I lean to believe that this might be a bug with knitr, rmarkdown, or flexdashboard - the way they first assemble the code from all sourced files and then performing actual execution of the code (like running if
statement)
For completeness, I was sourcing myfile.R from rmarkdown file:
---
title: "Diamonds Explorer"
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
---
```{r global, include=F}
knitr::opts_chunk$set(echo = F, message=FALSE, warning=FALSE)
source("myfile.R", echo=TRUE)
The session info is below:
sessionInfo() R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_Canada.utf8
[2] LC_CTYPE=English_Canada.utf8
[3] LC_MONETARY=English_Canada.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=English_Canada.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] flexdashboard_0.6.1 DBI_1.1.3 shinyBS_0.61.1
[4] shinybusy_0.3.1 shiny_1.7.3 readxl_1.4.1
[7] glue_1.6.2 R6_2.5.1 forcats_0.5.2
[10] stringr_1.4.1 DT_0.27 plotly_4.10.1
[13] data.table_1.14.6 lubridate_1.9.0 timechange_0.1.1
[16] ggplot2_3.4.0 magrittr_2.0.3
Upvotes: 0
Reputation: 71
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.
Upvotes: 7
Reputation: 226332
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 ...
Upvotes: 18