Search code examples
rreturnsapply

I am trying to append a return function, error is Error in FUN(X[[i]], ...) : unused argument


I have a sapply line and I want to include a return function so that I can see how far my code is progressing.

When I run this however I return the error:

Error in FUN(X[[i]], ...) : unused argument (return(i))

The code is:

r <- sapply(unique(temp$sim), 
            function(i) optimize(f = eval, interval = c(0, 0.05), df=filter(temp, sim==i))$minimum,return(i))

Where am I going wrong?


Solution

  • I can't reproduce you since temp is not accessible, but the error is probably due to a small typos you made. The , between your return and $minimum should ;.

    You can try:

    sapply(unique(temp$sim), function(i) optimize(f = eval, interval = c(0, 0.05), df=filter(temp, sim==i))$minimum; return(i))
    

    or better explicitly add the function brackets:

    sapply(unique(temp$sim), function(i) {
      optimize(f = eval, interval = c(0, 0.05), df=filter(temp, sim==i))$minimum
      return(i)
    })