Skip to content

Support Shiny async/promises based rendering #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2018
Merged

Support Shiny async/promises based rendering #1281

merged 4 commits into from
Jun 5, 2018

Conversation

jcheng5
Copy link
Contributor

@jcheng5 jcheng5 commented Jun 5, 2018

Example:

library(shiny)
library(plotly)
library(ggplot2)
library(promises)
library(future)
plan(multisession)

ui <- fluidPage(
  plotlyOutput("plot1"),
  plotlyOutput("plot2"),
  plotlyOutput("plot3"),
  plotlyOutput("plot4")
)

server <- function(input, output, session) {
  output$plot1 <- renderPlotly({
    # Async plot_ly
    future({ Sys.sleep(2); cars }) %...>%
      plot_ly(x = ~speed, y = ~dist, type = "scatter", mode = "markers")
  })

  output$plot2 <- renderPlotly({
    # Async ggplotly
    future({ Sys.sleep(2); mtcars }) %...>%
      { ggplot(., aes(hp, mpg)) + geom_point() } %...>%
      ggplotly()
  })
  
  output$plot3 <- renderPlotly({
    # Not async
    plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width,
      type = "scatter", mode = "markers")
  })
  
  output$plot4 <- renderPlotly({
    # Ensure errors are handled properly (should be blank)
    future({}) %...>%
      { req(FALSE) }
  })
}

shinyApp(ui, server)

@cpsievert
Copy link
Collaborator

Thanks! Have you verified this works? I don't seem to get a result for plot1 or plot2 (I just see plot3 and empty containers for plot1/plot2). This seems to work though (plot2 doesn't appear until plot1 is ready because of shiny's flush cycle, right?):

library(shiny)
library(plotly)
library(ggplot2)
library(promises)
library(future)
plan(multisession)

ui <- fluidPage(
  plotOutput("plot"),
  plotlyOutput("plot2")
)

server <- function(input, output, session) {
  
  output$plot <- renderPlot({
    # Async ggplotly
    future({ Sys.sleep(2); mtcars }) %...>%
    { ggplot(., aes(hp, mpg)) + geom_point() }
  })
  
  output$plot2 <- renderPlotly({
    # Not async
    plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width,
            type = "scatter", mode = "markers")
  })
  
}

shinyApp(ui, server)
> session_info()
Session info ------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.0 (2018-04-23)
 system   x86_64, darwin15.6.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/Chicago             
 date     2018-06-05                  

Packages ----------------------------------------------------------------------
 package      * version    date       source                            
 assertthat     0.2.0      2017-04-11 CRAN (R 3.5.0)                    
 backports      1.1.2      2017-12-13 CRAN (R 3.5.0)                    
 base         * 3.5.0      2018-04-24 local                             
 base64enc      0.1-3      2015-07-28 CRAN (R 3.5.0)                    
 bindr          0.1.1      2018-03-13 CRAN (R 3.5.0)                    
 bindrcpp     * 0.2.2      2018-03-29 CRAN (R 3.5.0)                    
 Cairo          1.5-9      2015-09-26 CRAN (R 3.5.0)                    
 codetools      0.2-15     2016-10-05 CRAN (R 3.5.0)                    
 colorout       1.2-0      2018-04-25 Github (jalvesaq/colorout@c42088d)
 colorspace     1.3-2      2016-12-14 CRAN (R 3.5.0)                    
 commonmark     1.5        2018-04-28 CRAN (R 3.5.0)                    
 compiler       3.5.0      2018-04-24 local                             
 crayon         1.3.4      2017-09-16 CRAN (R 3.5.0)                    
 crosstalk      1.0.0      2016-12-21 CRAN (R 3.5.0)                    
 data.table     1.11.4     2018-05-27 CRAN (R 3.5.0)                    
 datasets     * 3.5.0      2018-04-24 local                             
 desc           1.2.0      2018-05-01 CRAN (R 3.5.0)                    
 devtools     * 1.13.5     2018-02-18 CRAN (R 3.5.0)                    
 digest         0.6.15     2018-01-28 CRAN (R 3.5.0)                    
 dplyr          0.7.5      2018-05-19 CRAN (R 3.5.0)                    
 fortunes       1.5-4      2016-12-29 CRAN (R 3.5.0)                    
 future       * 1.8.1      2018-05-03 CRAN (R 3.5.0)                    
 ggplot2      * 2.2.1.9000 2018-05-31 Github (tidyverse/ggplot2@a819813)
 globals        0.11.0     2018-01-10 CRAN (R 3.5.0)                    
 glue           1.2.0.9000 2018-05-09 Github (tidyverse/glue@7230ed2)   
 graphics     * 3.5.0      2018-04-24 local                             
 grDevices    * 3.5.0      2018-04-24 local                             
 grid           3.5.0      2018-04-24 local                             
 gtable         0.2.0      2016-02-26 CRAN (R 3.5.0)                    
 hexbin         1.27.2     2018-01-15 CRAN (R 3.5.0)                    
 htmltools      0.3.6      2017-04-28 CRAN (R 3.5.0)                    
 htmlwidgets    1.2        2018-04-19 CRAN (R 3.5.0)                    
 httpuv         1.4.3      2018-05-10 CRAN (R 3.5.0)                    
 httr           1.3.1      2017-08-20 CRAN (R 3.5.0)                    
 inline         0.3.15     2018-05-18 CRAN (R 3.5.0)                    
 jsonlite       1.5        2017-06-01 CRAN (R 3.5.0)                    
 knitr          1.20       2018-02-20 CRAN (R 3.5.0)                    
 labeling       0.3        2014-08-23 CRAN (R 3.5.0)                    
 later          0.7.2      2018-05-01 CRAN (R 3.5.0)                    
 lattice        0.20-35    2017-03-25 CRAN (R 3.5.0)                    
 lazyeval       0.2.1      2017-10-29 CRAN (R 3.5.0)                    
 listenv        0.7.0      2018-01-21 CRAN (R 3.5.0)                    
 magrittr       1.5        2014-11-22 CRAN (R 3.5.0)                    
 memoise        1.1.0      2017-04-21 CRAN (R 3.5.0)                    
 methods      * 3.5.0      2018-04-24 local                             
 mime           0.5        2016-07-07 CRAN (R 3.5.0)                    
 munsell        0.4.3      2016-02-13 CRAN (R 3.5.0)                    
 parallel       3.5.0      2018-04-24 local                             
 pillar         1.2.3      2018-05-25 CRAN (R 3.5.0)                    
 pkgconfig      2.0.1      2017-03-21 CRAN (R 3.5.0)                    
 pkgload        1.0.0      2018-04-25 Github (r-lib/pkgload@a9184a0)    
 plotly       * 4.7.1.9000 <NA>       local                             
 plyr           1.8.4      2016-06-08 CRAN (R 3.5.0)                    
 promises     * 1.0.1      2018-04-13 CRAN (R 3.5.0)                    
 purrr          0.2.5      2018-05-29 cran (@0.2.5)                     
 R6             2.2.2      2017-06-17 CRAN (R 3.5.0)                    
 RColorBrewer   1.1-2      2014-12-07 CRAN (R 3.5.0)                    
 Rcpp           0.12.17    2018-05-18 CRAN (R 3.5.0)                    
 rlang          0.2.1      2018-05-30 CRAN (R 3.5.0)                    
 roxygen2       6.0.1      2017-02-06 CRAN (R 3.5.0)                    
 rprojroot      1.3-2      2018-01-03 CRAN (R 3.5.0)                    
 scales         0.5.0.9000 2018-05-31 Github (hadley/scales@d767915)    
 shiny        * 1.1.0      2018-05-17 CRAN (R 3.5.0)                    
 stats        * 3.5.0      2018-04-24 local                             
 stringi        1.2.2      2018-05-02 CRAN (R 3.5.0)                    
 stringr        1.3.1      2018-05-10 CRAN (R 3.5.0)                    
 testthat       2.0.0      2017-12-13 CRAN (R 3.5.0)                    
 tibble         1.4.2      2018-01-22 CRAN (R 3.5.0)                    
 tidyr          0.8.1      2018-05-18 CRAN (R 3.5.0)                    
 tidyselect     0.2.4      2018-02-26 CRAN (R 3.5.0)                    
 tools          3.5.0      2018-04-24 local                             
 utils        * 3.5.0      2018-04-24 local                             
 viridisLite    0.3.0      2018-02-01 CRAN (R 3.5.0)                    
 withr          2.1.2      2018-05-31 Github (jimhester/withr@70d6321)  
 xml2           1.2.0      2018-01-24 CRAN (R 3.5.0)                    
 xtable         1.8-2      2016-02-05 CRAN (R 3.5.0)                    
 yaml           2.1.19     2018-05-01 CRAN (R 3.5.0)   

@jcheng5
Copy link
Contributor Author

jcheng5 commented Jun 5, 2018

Oh sorry, I think you need ramnathv/htmlwidgets@async too. But good to know that the sync case doesn't break if you aren't using promises and don't have the latest version of htmlwidgets.

And your comment about the flush cycle is correct.

@cpsievert
Copy link
Collaborator

Great, thanks! It works now...do you see any harm in merging and submitting to CRAN before htmlwidgets >1.2 is on CRAN?

@jcheng5
Copy link
Contributor Author

jcheng5 commented Jun 5, 2018

I... guess there's no harm?

@jcheng5
Copy link
Contributor Author

jcheng5 commented Jun 5, 2018

Oh, looks like the async branch for htmlwidgets has been merged into master. I don't know when it's going to CRAN though.

@cpsievert cpsievert merged commit c79ba2c into master Jun 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants