0

I have two functions. I need call one function and return a value, but then I want to call a second function. I cannot execute the function that returns nothing and then execute the second one for time issues

segunda <- function(){

 number <- 0
 ........

}


primera <- function(msg){

 return(paste(msg, " 1 "))

 segunda()

}

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • 1
    What exactly is the problem? – iod Jul 23 '19 at 12:15
  • 3
    Not possible. Returning exits the function. – Roland Jul 23 '19 at 12:17
  • 1
    Since `print(msg)` returns `msg`, you can put `return(paste(segunda(msg), " 1 "))` in `primera`. – user2554330 Jul 23 '19 at 12:18
  • I updated the code, the processing of the "segunda" function is internal. I dont want to return anything in "segunda" function – Francisco Palomares Jul 23 '19 at 12:26
  • Couldn't you simply execute `segunda` first and then the `return` statement in `primera`, or am I misunderstanding your problem? – Dunois Jul 23 '19 at 12:31
  • @Dunois , The code is very simple, but for reasons of time, I must first execute the return and then the "segunda" function – Francisco Palomares Jul 23 '19 at 12:33
  • Does `segunda` HAVE to be executed from within `primera`? – Dunois Jul 23 '19 at 12:38
  • This is unclear. I suspect that it is an [XY problem](https://en.wikipedia.org/wiki/XY_problem). It might help if you explain what you mean by "reasons of time." Assuming that it is possible (which doesn't seem to be the case), how were you going to be using this return value if the returning function is still executing? – John Coleman Jul 23 '19 at 12:50
  • 1
    I updated the Code. I have and API that process a json. Then I save in bdd and send a response (return) ~ 80ms. Then I call second function (internal process) ("segunda") , dont have return ~20000ms. So I Need to return and then call internal proccess. The code is very simple in this post – Francisco Palomares Jul 23 '19 at 13:26
  • Sounds like you want to fork the R process, and have `segunda()` continue executing after `primera()` has returned. That's not easy. You should read the vignette in the `parallel` package to see what's possible, or some of the references from the High Performance Computing task view https://cran.r-project.org/web/views/HighPerformanceComputing.html. – user2554330 Jul 23 '19 at 13:28
  • 1
    Maybe the question [loops - Asynchronous programming in R](https://stackoverflow.com/q/56814256/4996248) might help. The [future package](https://cran.r-project.org/web/packages/future/vignettes/future-1-overview.html), as discussed in that question could be relevant. – John Coleman Jul 23 '19 at 14:18

1 Answers1

1

What you want to do is impossible, but you could first print the value to be returned, then call the second function, then use invisible() to silently return the value. Something like:

segunda <- function(msg){
  number <- 0
}


primera <- function(msg){
  s <- paste(msg, " 1 ")
  print(s)
  segunda(msg)
  invisible(s)
}

If in the console you evaluate:

x <- primera("test")

Then "test 1" is only printed to the console once before segunda(msg) is evaluated, but the result is still assigned to x.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • Thanks, but this is not posible. Read the last comment up – Francisco Palomares Jul 23 '19 at 13:27
  • @FranciscoPalomares I see. I'll keep the answer up since it might help someone who searches for the problem in the title of the question. Hopefully someone else can solve your actual problem (which seems that it might require some form of multithreading). – John Coleman Jul 23 '19 at 13:36