rstudio / promises

A promise library for R
https://rstudio.github.io/promises
Other
198 stars 19 forks source link

having issue running shiny and promises #10

Closed felloumi closed 6 years ago

felloumi commented 6 years ago

Hi Joe,

I looked at your slides at https://www.dropbox.com/s/2gf6tfk1t345lyf/async-talk-slides.pdf?dl=0 and run the following similar code:

library(promises) library(shiny) # async version library(future) plan(multiprocess) # use another R process/running in the background

availableCores() # 8

longRunningFunction <- function(value) { Sys.sleep(10) return(value) }

ui <- fluidPage(

Application title

titlePanel("Testing shiny@async, future and promises"),

actionButton("train", "slowest function"), verbatimTextOutput("summary"),

actionButton("option", "Display 20"), verbatimTextOutput("result") )

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

model <- eventReactive(input$train, {

# longRunningFunction(5)
future(longRunningFunction(5))

})

output$summary <- renderPrint({

#model()
model() %...>% print()

})

model2 <- eventReactive(input$option, {

return(20)

})

output$result <- renderPrint({

print(model2())

}) }

Run the application

shinyApp(ui = ui, server = server)

I expected that when I click on the first button (slowest function) and then immediately click on the second button to display 20 however it happened after I got the message from the first button. I thought that the first function will be run on the background. I am not sure if some thing is wrong with my code. I appreciate any clarification on this.

Thanks,

FE

jcheng5 commented 6 years ago

That used to be true, but we have gone out of our way to disallow that. We don't process new input messages (or invalidateLater timer events) for a session unless/until all previous input messages have been fully processed. If we didn't do this it would be difficult to reason about reactive code because you would have inputs changing state in the middle of async operations. The goal of the async functionality is less about reducing latency for a single session, and more about preventing multiple sessions from blocking each other.

felloumi commented 6 years ago

Ok thanks. I have another issue rendering a table using future/promises. Here is the example:

library(promises)
library(shiny) # async version
library(future)
plan(multiprocess) # use another R process/running in the background
#availableCores() # 8
library(DT)

longRunningFunction <- function(value) {
  Sys.sleep(10)
  return(value)
}

rr <- function() {
  x=read.delim("~/file.txt") # any text tab delimited file
  DT::datatable(x)
}
# 
ui <- fluidPage(

   # Application title
   titlePanel("Testing shiny@async, future and promises"),

   #  
   actionButton("train", "slowest function"), 
   verbatimTextOutput("summary"),

   actionButton("option", "Table"), 
   DT::dataTableOutput("result")
   )

# Define server logic required to draw a histogram
server <- function(input, output,session) {

  model <- eventReactive(input$train, { 

    # longRunningFunction(5)
    future(longRunningFunction(5))

  }) 

  output$summary <- renderPrint({ 

    model()
    # model() %...>% print()

  })

  model2 <- eventReactive(input$option, { 

    #rr()
    future(rr())

  }) 

  output$result <- DT::renderDataTable({ 

    model2()
    #model2() %...>% View()
  }) 

}

# Run the application 
shinyApp(ui = ui, server = server)

Running the app and clicking on command table gives an error:

screen shot 2017-12-06 at 3 11 15 pm

The code run perfectly without future. Any solution for the issue?

jcheng5 commented 6 years ago

DT isn't ready for promises yet, I have to make some changes there. Most htmlwidget packages will not need modifications to work for their render functions to work with promises, but DT does a lot of custom stuff.

jcheng5 commented 6 years ago

There's now an async branch for DT, FYI.

felloumi commented 6 years ago

Thanks, I will try it

On Thu, Feb 15, 2018 at 7:49 PM, Joe Cheng notifications@github.com wrote:

There's now an async branch for DT, FYI.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rstudio/promises/issues/10#issuecomment-366112109, or mute the thread https://github.com/notifications/unsubscribe-auth/AJ-FwSmjakc0VBsiqI6lwqQ0Uqv03Pz7ks5tVNCIgaJpZM4Q2d76 .

boxiangliu commented 6 years ago

Currently the async branch does not seem to function. The datatable output is blank.

dmenne commented 6 years ago

It works for me on Window, not on Ubuntu (blank) as @boxiangliu

PauloJhonny commented 6 years ago

I'm having the same issue when I try to use DT with promises, as @felloumi :

'Error in datatable: 'data' must be 2-dimensional (e.g. data frame or matrix)'.

dmenne commented 6 years ago

Try to use the github version of htmlwidgets

PauloJhonny commented 6 years ago

Thank you, @dmenne. I just installed the dev version of htmlwidgets, but it didn't work. So, I tried to install the dev version of DT as well, and it seems it worked. At least the async datatable output show up.

dmenne commented 6 years ago

Sorry, my error. I should have said "of DT". Glad it worked.