Chinmay UJ
Chinmay UJ

Reputation: 21

R Shiny Async Programming using GET function

The details on my R Installation and Packages are: OS macOS R v3.5.1 Future Package -! This is not available for the version of R installed on my system

Am trying to fetch json data using API call function using (HTTR:GET). The task is that there are multiple end points from which data should be pulled on R for data processing and eventually creating Shiny App.

Issues in hand are: Current Future Package is not available for R3.5.1. What should be my action step?

Can you confirm if my code is correct: XYZ <- promise(GET(url, timeout(120)))

Third, if multiple end points have to be part of the async programming, how should i develop the script. A small example will be helpful and provide direction.

Thank you in advance.

Upvotes: 2

Views: 970

Answers (1)

Sada93
Sada93

Reputation: 2835

I not aware of the 3.5.1 issue but here is how you launch tasks for async programming using future and promises

library(promises)
library(future)
library(httr)

plan(multisession)


future(GET("http://slowwly.robertomurray.co.uk/delay/10000/url/http://www.google.co.uk"))%...>%print(.)
future(GET("http://slowwly.robertomurray.co.uk/delay/10/url/http://www.google.co.uk"))%...>%print(.)
future(GET("http://slowwly.robertomurray.co.uk/delay/10/url/http://www.google.co.uk"))%...>%print(.)
future(GET("http://slowwly.robertomurray.co.uk/delay/10/url/http://www.google.co.uk"))%...>%print(.)
future(GET("http://slowwly.robertomurray.co.uk/delay/10/url/http://www.google.co.uk"))%...>%print(.)

this is an example of a dummy api being queried with simulated delay.

plan() sets the strategy for the remainder of the program. Here it is set to multisession which launches a separate R session to execute the program. Other commonly used plans are multicore (on a mac or linux because of fork) or multiprocess. You can learn more about them with ?plan

The special %...>% operator is some syntactic sugar which basically tells the program to print the result only after the promise is resolved. Till then the control is returned to the parent process.

Following your notation you can also do this without the promisses package.

a = future(GET("http://slowwly.robertomurray.co.uk/delay/10000/url/http://www.google.co.uk"))
value(a)

Here the result of the API call is returned only when its completed. Till value() is called the parent has control to perform other tasks.

Here is the best reference for all these calls and more:

  1. https://rstudio.github.io/promises/articles/futures.html
  2. https://cran.r-project.org/web/packages/future/vignettes/future-1-overview.html

Upvotes: 4

Related Questions