Shiny promises future is not working on eventReactive











up vote
0
down vote

favorite
1












I have an inputButton than when you click it, 2 querys to mysql database are done. One is a heavy one (more than 10 secs) and the other is light (less than 0.01sec to get data).



As I want to show the result of this querys on shiny app, I have intendeed to use Promises and Future packages for asyncronous loading.



In the example that I show you of my code, I have simulated the SQL querys with the function heavyFunction, which is intended to simulate the heavy query and the ligth one time loads.



The issue is that this code is not working for me, because the results of the light query are not shown till the heavy query is done.



Note: In the Rstudio console, this code works perfect...



library(future)
library(promises)
plan(multiprocess)

heavyFunction <- function(n){
Sys.sleep(n)
print(n)
}


ui <- fluidPage(
actionButton("go","Show the data"),
textOutput("result0sec"),
textOutput("result10sec")

)

server <- function(input,output,session){


data0 <- eventReactive(input$go,{
heavyFunction(0)


})

data10 <- eventReactive(input$go,{
heavyFunction(10)


})
output$result0sec <- renderText({
data <- data0()
future(data)%...>%print()
})


output$result10sec <- renderText({
data <- data10()
print(data)
})




}
shinyApp(ui,server)


What I'm doing wrong?










share|improve this question







New contributor




jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite
    1












    I have an inputButton than when you click it, 2 querys to mysql database are done. One is a heavy one (more than 10 secs) and the other is light (less than 0.01sec to get data).



    As I want to show the result of this querys on shiny app, I have intendeed to use Promises and Future packages for asyncronous loading.



    In the example that I show you of my code, I have simulated the SQL querys with the function heavyFunction, which is intended to simulate the heavy query and the ligth one time loads.



    The issue is that this code is not working for me, because the results of the light query are not shown till the heavy query is done.



    Note: In the Rstudio console, this code works perfect...



    library(future)
    library(promises)
    plan(multiprocess)

    heavyFunction <- function(n){
    Sys.sleep(n)
    print(n)
    }


    ui <- fluidPage(
    actionButton("go","Show the data"),
    textOutput("result0sec"),
    textOutput("result10sec")

    )

    server <- function(input,output,session){


    data0 <- eventReactive(input$go,{
    heavyFunction(0)


    })

    data10 <- eventReactive(input$go,{
    heavyFunction(10)


    })
    output$result0sec <- renderText({
    data <- data0()
    future(data)%...>%print()
    })


    output$result10sec <- renderText({
    data <- data10()
    print(data)
    })




    }
    shinyApp(ui,server)


    What I'm doing wrong?










    share|improve this question







    New contributor




    jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have an inputButton than when you click it, 2 querys to mysql database are done. One is a heavy one (more than 10 secs) and the other is light (less than 0.01sec to get data).



      As I want to show the result of this querys on shiny app, I have intendeed to use Promises and Future packages for asyncronous loading.



      In the example that I show you of my code, I have simulated the SQL querys with the function heavyFunction, which is intended to simulate the heavy query and the ligth one time loads.



      The issue is that this code is not working for me, because the results of the light query are not shown till the heavy query is done.



      Note: In the Rstudio console, this code works perfect...



      library(future)
      library(promises)
      plan(multiprocess)

      heavyFunction <- function(n){
      Sys.sleep(n)
      print(n)
      }


      ui <- fluidPage(
      actionButton("go","Show the data"),
      textOutput("result0sec"),
      textOutput("result10sec")

      )

      server <- function(input,output,session){


      data0 <- eventReactive(input$go,{
      heavyFunction(0)


      })

      data10 <- eventReactive(input$go,{
      heavyFunction(10)


      })
      output$result0sec <- renderText({
      data <- data0()
      future(data)%...>%print()
      })


      output$result10sec <- renderText({
      data <- data10()
      print(data)
      })




      }
      shinyApp(ui,server)


      What I'm doing wrong?










      share|improve this question







      New contributor




      jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have an inputButton than when you click it, 2 querys to mysql database are done. One is a heavy one (more than 10 secs) and the other is light (less than 0.01sec to get data).



      As I want to show the result of this querys on shiny app, I have intendeed to use Promises and Future packages for asyncronous loading.



      In the example that I show you of my code, I have simulated the SQL querys with the function heavyFunction, which is intended to simulate the heavy query and the ligth one time loads.



      The issue is that this code is not working for me, because the results of the light query are not shown till the heavy query is done.



      Note: In the Rstudio console, this code works perfect...



      library(future)
      library(promises)
      plan(multiprocess)

      heavyFunction <- function(n){
      Sys.sleep(n)
      print(n)
      }


      ui <- fluidPage(
      actionButton("go","Show the data"),
      textOutput("result0sec"),
      textOutput("result10sec")

      )

      server <- function(input,output,session){


      data0 <- eventReactive(input$go,{
      heavyFunction(0)


      })

      data10 <- eventReactive(input$go,{
      heavyFunction(10)


      })
      output$result0sec <- renderText({
      data <- data0()
      future(data)%...>%print()
      })


      output$result10sec <- renderText({
      data <- data10()
      print(data)
      })




      }
      shinyApp(ui,server)


      What I'm doing wrong?







      r shiny future






      share|improve this question







      New contributor




      jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 21 at 7:17









      jff

      32




      32




      New contributor




      jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      jff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Welcome to SO!



          This thread discusses the same issue.



          Please also see the detailed answer from Joe Cheng on GitHub.



          The main problem you are experiencing is reflected by his following statement:




          The goal, at least for this release of Shiny, is not to allow this
          kind of intra-session responsiveness, but rather, inter-session; i.e.,
          running an async operation won't make its owning session more
          responsive, but rather will allow other sessions to be more
          responsive.




          However, there are ways to work around this behaviour by running the future in a background R process with e.g. library(callr) or more convenient library(future.callr) and it's plan(callr).



          Here is a working version of your code:



          library(future)
          library(promises)
          library(future.callr)
          plan(callr)

          heavyFunction <- function(n) {
          Sys.sleep(n)
          print(n)
          }

          ui <- fluidPage(
          br(),
          actionButton("go", "Show the data"),
          br(), br(),
          textOutput("result0sec"),
          textOutput("result10sec")
          )

          server <- function(input, output, session) {
          futureData <- reactiveValues(data10 = NULL)

          data0 <- eventReactive(input$go, {
          heavyFunction(0)
          })

          observeEvent(input$go, {
          myFuture <- future({
          heavyFunction(5)
          })

          then(
          myFuture,
          onFulfilled = function(value) {
          futureData$data10 <<- value
          },
          onRejected = NULL
          )
          return(NULL)
          })

          output$result0sec <- renderText({
          data0()
          })

          output$result10sec <- renderText({
          req(futureData$data10)
          })
          }

          shinyApp(ui, server)


          The most important point here is to realize, that you shouldn't return your future directly, otherwise it will block all other actions - the observer returns nothing, it only has the side-effect of triggering the callback-function.






          share|improve this answer























          • This works for me. Thanks!
            – jff
            Nov 22 at 10:19











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          jff is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407004%2fshiny-promises-future-is-not-working-on-eventreactive%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Welcome to SO!



          This thread discusses the same issue.



          Please also see the detailed answer from Joe Cheng on GitHub.



          The main problem you are experiencing is reflected by his following statement:




          The goal, at least for this release of Shiny, is not to allow this
          kind of intra-session responsiveness, but rather, inter-session; i.e.,
          running an async operation won't make its owning session more
          responsive, but rather will allow other sessions to be more
          responsive.




          However, there are ways to work around this behaviour by running the future in a background R process with e.g. library(callr) or more convenient library(future.callr) and it's plan(callr).



          Here is a working version of your code:



          library(future)
          library(promises)
          library(future.callr)
          plan(callr)

          heavyFunction <- function(n) {
          Sys.sleep(n)
          print(n)
          }

          ui <- fluidPage(
          br(),
          actionButton("go", "Show the data"),
          br(), br(),
          textOutput("result0sec"),
          textOutput("result10sec")
          )

          server <- function(input, output, session) {
          futureData <- reactiveValues(data10 = NULL)

          data0 <- eventReactive(input$go, {
          heavyFunction(0)
          })

          observeEvent(input$go, {
          myFuture <- future({
          heavyFunction(5)
          })

          then(
          myFuture,
          onFulfilled = function(value) {
          futureData$data10 <<- value
          },
          onRejected = NULL
          )
          return(NULL)
          })

          output$result0sec <- renderText({
          data0()
          })

          output$result10sec <- renderText({
          req(futureData$data10)
          })
          }

          shinyApp(ui, server)


          The most important point here is to realize, that you shouldn't return your future directly, otherwise it will block all other actions - the observer returns nothing, it only has the side-effect of triggering the callback-function.






          share|improve this answer























          • This works for me. Thanks!
            – jff
            Nov 22 at 10:19















          up vote
          0
          down vote



          accepted










          Welcome to SO!



          This thread discusses the same issue.



          Please also see the detailed answer from Joe Cheng on GitHub.



          The main problem you are experiencing is reflected by his following statement:




          The goal, at least for this release of Shiny, is not to allow this
          kind of intra-session responsiveness, but rather, inter-session; i.e.,
          running an async operation won't make its owning session more
          responsive, but rather will allow other sessions to be more
          responsive.




          However, there are ways to work around this behaviour by running the future in a background R process with e.g. library(callr) or more convenient library(future.callr) and it's plan(callr).



          Here is a working version of your code:



          library(future)
          library(promises)
          library(future.callr)
          plan(callr)

          heavyFunction <- function(n) {
          Sys.sleep(n)
          print(n)
          }

          ui <- fluidPage(
          br(),
          actionButton("go", "Show the data"),
          br(), br(),
          textOutput("result0sec"),
          textOutput("result10sec")
          )

          server <- function(input, output, session) {
          futureData <- reactiveValues(data10 = NULL)

          data0 <- eventReactive(input$go, {
          heavyFunction(0)
          })

          observeEvent(input$go, {
          myFuture <- future({
          heavyFunction(5)
          })

          then(
          myFuture,
          onFulfilled = function(value) {
          futureData$data10 <<- value
          },
          onRejected = NULL
          )
          return(NULL)
          })

          output$result0sec <- renderText({
          data0()
          })

          output$result10sec <- renderText({
          req(futureData$data10)
          })
          }

          shinyApp(ui, server)


          The most important point here is to realize, that you shouldn't return your future directly, otherwise it will block all other actions - the observer returns nothing, it only has the side-effect of triggering the callback-function.






          share|improve this answer























          • This works for me. Thanks!
            – jff
            Nov 22 at 10:19













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Welcome to SO!



          This thread discusses the same issue.



          Please also see the detailed answer from Joe Cheng on GitHub.



          The main problem you are experiencing is reflected by his following statement:




          The goal, at least for this release of Shiny, is not to allow this
          kind of intra-session responsiveness, but rather, inter-session; i.e.,
          running an async operation won't make its owning session more
          responsive, but rather will allow other sessions to be more
          responsive.




          However, there are ways to work around this behaviour by running the future in a background R process with e.g. library(callr) or more convenient library(future.callr) and it's plan(callr).



          Here is a working version of your code:



          library(future)
          library(promises)
          library(future.callr)
          plan(callr)

          heavyFunction <- function(n) {
          Sys.sleep(n)
          print(n)
          }

          ui <- fluidPage(
          br(),
          actionButton("go", "Show the data"),
          br(), br(),
          textOutput("result0sec"),
          textOutput("result10sec")
          )

          server <- function(input, output, session) {
          futureData <- reactiveValues(data10 = NULL)

          data0 <- eventReactive(input$go, {
          heavyFunction(0)
          })

          observeEvent(input$go, {
          myFuture <- future({
          heavyFunction(5)
          })

          then(
          myFuture,
          onFulfilled = function(value) {
          futureData$data10 <<- value
          },
          onRejected = NULL
          )
          return(NULL)
          })

          output$result0sec <- renderText({
          data0()
          })

          output$result10sec <- renderText({
          req(futureData$data10)
          })
          }

          shinyApp(ui, server)


          The most important point here is to realize, that you shouldn't return your future directly, otherwise it will block all other actions - the observer returns nothing, it only has the side-effect of triggering the callback-function.






          share|improve this answer














          Welcome to SO!



          This thread discusses the same issue.



          Please also see the detailed answer from Joe Cheng on GitHub.



          The main problem you are experiencing is reflected by his following statement:




          The goal, at least for this release of Shiny, is not to allow this
          kind of intra-session responsiveness, but rather, inter-session; i.e.,
          running an async operation won't make its owning session more
          responsive, but rather will allow other sessions to be more
          responsive.




          However, there are ways to work around this behaviour by running the future in a background R process with e.g. library(callr) or more convenient library(future.callr) and it's plan(callr).



          Here is a working version of your code:



          library(future)
          library(promises)
          library(future.callr)
          plan(callr)

          heavyFunction <- function(n) {
          Sys.sleep(n)
          print(n)
          }

          ui <- fluidPage(
          br(),
          actionButton("go", "Show the data"),
          br(), br(),
          textOutput("result0sec"),
          textOutput("result10sec")
          )

          server <- function(input, output, session) {
          futureData <- reactiveValues(data10 = NULL)

          data0 <- eventReactive(input$go, {
          heavyFunction(0)
          })

          observeEvent(input$go, {
          myFuture <- future({
          heavyFunction(5)
          })

          then(
          myFuture,
          onFulfilled = function(value) {
          futureData$data10 <<- value
          },
          onRejected = NULL
          )
          return(NULL)
          })

          output$result0sec <- renderText({
          data0()
          })

          output$result10sec <- renderText({
          req(futureData$data10)
          })
          }

          shinyApp(ui, server)


          The most important point here is to realize, that you shouldn't return your future directly, otherwise it will block all other actions - the observer returns nothing, it only has the side-effect of triggering the callback-function.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 17:42

























          answered Nov 21 at 14:11









          ismirsehregal

          79718




          79718












          • This works for me. Thanks!
            – jff
            Nov 22 at 10:19


















          • This works for me. Thanks!
            – jff
            Nov 22 at 10:19
















          This works for me. Thanks!
          – jff
          Nov 22 at 10:19




          This works for me. Thanks!
          – jff
          Nov 22 at 10:19










          jff is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          jff is a new contributor. Be nice, and check out our Code of Conduct.













          jff is a new contributor. Be nice, and check out our Code of Conduct.












          jff is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407004%2fshiny-promises-future-is-not-working-on-eventreactive%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sphinx de Gizeh

          Dijon

          Guerrita