How can I send a GET request without waiting for the response

↘锁芯ラ 提交于 2019-12-06 15:22:07

Here is a way to run GET asynchronously and in a intra-session non-blocking manner (observer returning nothing):

library(shiny)
library(future)
library(promises)
library(future.callr)
library(httr)

plan(callr)

queryGoogle <- function(queryString) {
  myResponse <- httr::GET("http://google.com/", path = "search", query = list(q = queryString))
  return(myResponse)
}

ui <- fluidPage(
  br(),
  textOutput("time_output"),
  br(),
  textInput(inputId="query_input", label = NULL, value = "", placeholder = "Search google..."),
  actionButton("import", "Query"),
  hr(),
  textOutput("query_output")
)

server <- function(input, output, session) {
  futureData <- reactiveValues(response = NULL)

  observeEvent(input$import, {
    myFuture <- future({
      queryGoogle(isolate(input$query_input))
    })

    then(
      myFuture,
      onFulfilled = function(value) {
        futureData$response <- value
      },
      onRejected = NULL
    )
    return(NULL)
  })

  output$query_output <- renderPrint({
    req(futureData$response)
  })

  time <- reactive({
    invalidateLater(500, session)
    Sys.time()
  })

  output$time_output <- renderText({ paste("Something running in parallel:", time()) })
}

shinyApp(ui, server)

This is a slight modification of my answer here.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!