Explanation of R: options(expressions=) to non-computer scientists

后端 未结 2 1194
攒了一身酷
攒了一身酷 2020-12-03 07:11

I have written a recursive function of the form

foo=function(vars,i=2){
  **do something with vars**
  if(i==length(vars)){
    return(**something**)
  }else         


        
相关标签:
2条回答
  • 2020-12-03 07:28

    I guess a site like this is not the right place for a general crash-course on computer science, but in your case there is a match between the site name and the question: You are experiencing a stack overflow! :-)

    In computer science, a stack is a data structure where you can access only its last element (a half-queue, so to say). For more information see e.g. Wikipedia or CMU. Stacks play a central role when calling functions, because the return address and often function arguments are stored there. Returning from a function simply means retrieving the return address from the stack and continuing the program execution from the point in code specified by that address.

    Since you are applying recursion in your code (calling a function from within itself), the stack grows with every new call. Eventually, your program runs out of memory for storing the whole stack.

    See also Memory in R documentation.

    0 讨论(0)
  • 2020-12-03 07:29

    Cutting some corners here... The expressions -option sets the maximum number of nested expressions that will be evaluated. With deep recursion the default is sometimes exceeded, and increasing the value often solves the problem. But if it doesn't (a new error message is given), you might need to additionally increase the size of the protection stack. Computers store information about the active routines in stacks. Sometimes when the information doesn't quite fit to the stack, the information is written beyond the stacks boundary, which is bad, since it typically creates, e.g., memory access problems. This can potentially be rectified by by setting the option --max-ppsize when starting R. It's like giving a child a larger paper when he or she overdraws the current paper, and colors the table too.

    For more background, see Wikipedia, and links thereof. For details of R's command line options, see An Introduction to R, section B.1.

    0 讨论(0)
提交回复
热议问题