0

I have an overall function in R with several functions inside, but some of the parameters are not recognized.

The code is summarized here:

  MY_FUNCTION <- function(PATH, n_c = 4, My_Parameter = 100){
  
  library(MY_MODEL)
  
  nc <- n_c
  My_Limit <- 10
  
  # args = a number of arguments are put in the function
  OUTPUT <- genI(args, My_Limit, My_Parameter) # A function within the model
  
  ....
  
  Results <- RUN_MY_MODEL(args, nc = nc) 
  }

When I try to run MY_FUNCTION I get the error:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
object 'My_Parameter' was not found

The function can run if I before I run MY_FUNCTION set My_Parameter = 100 and My_Limit = 10 in the console.

What is the difference between: n_c, My_Parameter and My_Limit?

Why is My_Parameter not recognized even when a default value is put in the function?

Best regards, Jesper

EDIT: Normally, I run this part of the code as a script, and that causes no issues

  library(MY_MODEL)
  
  nc <- n_c
  My_Limit <- 10
  My_Parameter <- 100
  
  # args = a number of arguments are put in the function
  OUTPUT <- genI(args, My_Limit, My_Parameter) # A function within the model
  
  ....
  
  Results <- RUN_MY_MODEL(args, nc = nc)

In this way there are no problems with My_Parameter and My_Limit in any of the sub-functions.

My code is running a very large model (I did not make that myself) and making this reproducible is not possible. :-(

  • Please make your example reproducible – Dason Aug 25 '20 at 15:10
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. It kind of depends on what the `genI` function does. It's possible that function uses non-standard evaluation and evaluates symbols in non-traditional environments. – MrFlick Aug 25 '20 at 15:10
  • 1
    (1) Setting `My_Parameter` on the console before calling the function does nothing, since it should be defined in the function formals and therefore your console value is masked. (2) That error message looks like it's coming from a call to `data.frame(...)`. It's hard for us to help much more without knowing what `genI` is, but my guess is that *that* function (or one of its descendents) is doing something wrong. – r2evans Aug 25 '20 at 15:11
  • I edited my post for clarification. – Jesper Nørlem Kamp Aug 25 '20 at 15:26
  • We understand what you're saying. And generally what you're trying to do works. Passing parameters on works fine. With the amount of detail you've provided, it's very hard to say why it's not working for you. You haven't given us any runnable code that replicates the problem--just sketches of the overall structure, so we need more info to be able to help. At least run `traceback()` after the error so you can see which function call originates the error. – Gregor Thomas Aug 25 '20 at 15:31
  • 1
    My best guess is that `genI()` requires more arguments, and the 3rd argument isn't `My_Parameter`. But that contradicts the fact that you say it runs fine in a script. Can you post the definition of `genI()`? Or at least the start of it? Or maybe put a `browser()` call in your function right before the `genI()` call and verify that `args`, `My_Limit`, and `My_Parameter` are all what you think they are. – Gregor Thomas Aug 25 '20 at 15:36
  • This [debugging in R](https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio) might help you narrow down the problem and either solve it yourself or isolate it small enough that you can post a reproducible example. – Gregor Thomas Aug 25 '20 at 15:37
  • Thanks for your comments, I can see that I do not give you much to go on, but I appreciate the inputs. One last comment, I do not understand why ´MyLimit` is not seen as it is just a parameter given at the begining of the script. A simple parameter stated, but not recognized some lines later. – Jesper Nørlem Kamp Aug 25 '20 at 19:02

0 Answers0