mirai.promises makes ‘mirai’ ‘promises’ for easy integration in ‘plumber’ or ‘shiny’ pipelines.

mirai.promises also makes nanonext ‘recvAio’ ‘promises’, working on asynchronous message receives.

mirai >= 0.11.1 already supports ‘promises’ out of the box. In this case, loading mirai.promises after mirai will mask the equivalent method to provide additional functionality offered by this package such as allowing a custom polling interval.

Installation

Install the latest version from the R-releases R-universe, or CRAN:

install.packages("mirai.promises", repos = "https://r-releases.r-universe.dev")

Or the development build from the author’s R-universe:

install.packages("mirai.promises", repos = "https://shikokuchuo.r-universe.dev")

Polling Interval

polling() may be used to customise the frequency with which to poll for promise resolution (defaults to every 100 ms).

Package authors wishing to use the S3 methods contained within this package may simply import the polling() function to make them available.

Example

The below example simulates a plot function requiring a long compute in a ‘shiny’ app.

This app takes c. 2s to start compared to the 8s it would otherwise take if the ‘long-running’ computations were not running on parallel workers.

library(shiny)
library(mirai)
library(promises)
library(mirai.promises)

polling(freq = 50L)

# set 4 persistent workers
daemons(n = 4L)

ui <- fluidPage(
  fluidRow(
    plotOutput("one"),
    plotOutput("two"),
  ),
  fluidRow(
    plotOutput("three"),
    plotOutput("four"),
  )
)

make_plot <- function(time) {
  Sys.sleep(time)
  runif(10)
}

args <- list(make_plot = make_plot, time = 2)

server <- function(input, output, session) {
  output$one <- renderPlot(mirai(make_plot(time), .args = args) %...>% plot())
  output$two <- renderPlot(mirai(make_plot(time), .args = args) %...>% plot())
  output$three <- renderPlot(mirai(make_plot(time), .args = args) %...>% plot())
  output$four <- renderPlot(mirai(make_plot(time), .args = args) %...>% plot())
  session$onSessionEnded(stopApp)
}

shinyApp(ui = ui, server = server)

Thanks

Daniel Falbel for the original version of the above example and agreeing to its use here, as well as the specific use case that motivated this package.

Code of Conduct

Please note that the mirai.promises project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.