Make 'Mirai' 'Promises'

Allows 'mirai' objects encapsulating asynchronous computations, from the 'mirai' package by Gao (2023) , to be used interchangeably with 'promise' objects from the 'promises' package by Cheng (2021) < https://CRAN.R-project.org/package=promises>. This facilitates their use with packages 'plumber' by Schloerke and Allen (2022) < https://CRAN.R-project.org/package=plumber> and 'shiny' by Cheng, Allaire, Sievert, Schloerke, Xie, Allen, McPherson, Dipert and Borges (2022) < https://CRAN.R-project.org/package=shiny>.


mirai.promises

CRANstatus mirai.promises statusbadge R-CMD-check Codecov DOI

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 release from CRAN:

install.packages("mirai.promises")

Or the development version from 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

Joe Cheng for optimising the promises method to work seamlessly within Shiny.

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.

Links

mirai.promises on CRAN: https://cran.r-project.org/package=mirai.promises

mirai website: https://shikokuchuo.net/mirai/
mirai on CRAN: https://cran.r-project.org/package=mirai
  - High Performance Computing CRAN Task View: https://cran.r-project.org/view=HighPerformanceComputing

nanonext website: https://shikokuchuo.net/nanonext/
nanonext on CRAN: https://cran.r-project.org/package=nanonext

–

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.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("mirai.promises")

0.5.0 by Charlie Gao, 2 years ago


https://shikokuchuo.net/mirai.promises/, https://github.com/shikokuchuo/mirai.promises/


Report a bug at https://github.com/shikokuchuo/mirai.promises/issues


Browse source code at https://github.com/cran/mirai.promises


Authors: Charlie Gao [aut, cre] , Hibiki AI Limited [cph]


Documentation:   PDF Manual  


GPL (>= 3) license


Imports nanonext, promises

Suggests mirai


See at CRAN