Skip to content

Commit 4b40e5b

Browse files
committed
async example, thanks @jcheng5
1 parent 0dd3613 commit 4b40e5b

File tree

1 file changed

+42
-0
lines changed
  • inst/examples/shiny/async

1 file changed

+42
-0
lines changed

inst/examples/shiny/async/app.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
library(shiny)
2+
library(plotly)
3+
library(ggplot2)
4+
library(promises)
5+
library(future)
6+
plan(multisession)
7+
8+
ui <- fluidPage(
9+
plotlyOutput("plot1"),
10+
plotlyOutput("plot2"),
11+
plotlyOutput("plot3"),
12+
plotlyOutput("plot4")
13+
)
14+
15+
server <- function(input, output, session) {
16+
output$plot1 <- renderPlotly({
17+
# Async plot_ly
18+
future({ Sys.sleep(2); cars }) %...>%
19+
plot_ly(x = ~speed, y = ~dist, type = "scatter", mode = "markers")
20+
})
21+
22+
output$plot2 <- renderPlotly({
23+
# Async ggplotly
24+
future({ Sys.sleep(2); mtcars }) %...>%
25+
{ ggplot(., aes(hp, mpg)) + geom_point() } %...>%
26+
ggplotly()
27+
})
28+
29+
output$plot3 <- renderPlotly({
30+
# Not async
31+
plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width,
32+
type = "scatter", mode = "markers")
33+
})
34+
35+
output$plot4 <- renderPlotly({
36+
# Ensure errors are handled properly (should be blank)
37+
future({}) %...>%
38+
{ req(FALSE) }
39+
})
40+
}
41+
42+
shinyApp(ui, server)

0 commit comments

Comments
 (0)