How to use a column from a future dataframe

Viewed 144

I just dont seem to be able to understand how to reference a column from a dataframe returned from a future promise in shiny

  observeEvent(input$user_btn,{
    Resultsdf$data<-myResultsFunction()
    future({Resultsdf$data})%...>%
    Resultsdf$data$Acolumn<-gsub("blabla","Replace_me",Resultsdf$data$Acolumn) 
    Resultsdf$data$Comments<-"Please state plan here"
})

How do I reference Acolumn and Comments as part of the promise or if I cant do that then how do I place the result of the promise in a new dataframe so I can reference it?

where

Resultsdf<-reactiveValues(data = data.frame()) 
1 Answers

Here is a simple example, the data.frame is stored in a reactiveVal once the future is resolved:

library(shiny)
library(promises)
library(future)
library(datasets)
plan(multiprocess)

myResultsFunction <- function(){
  Sys.sleep(5)
  return(iris)
}

ui <- fluidPage(
  actionButton("user_btn", "user_btn")
)

server <- function(input, output, session) {
  
  Resultsdf <- reactiveVal()
  
  observeEvent(input$user_btn, {
    future({myResultsFunction()}) %...>% Resultsdf()
  })
  
  observeEvent(Resultsdf(), {
    print(Resultsdf()$Sepal.Length)
    print(Resultsdf()$Species)
  })
  
}

shinyApp(ui, server)

Result

The same pattern is used in this official article.


Edit - Here is how to do the same for reactiveValues:

library(shiny)
library(promises)
library(future)
library(datasets)
plan(multiprocess)

myResultsFunction <- function(){
  Sys.sleep(5)
  return(iris)
}

ui <- fluidPage(
  actionButton("user_btn", "user_btn")
)

server <- function(input, output, session) {
  
  Resultsdf <- reactiveValues(data = data.frame()) 
  
  observeEvent(input$user_btn, {
    future({myResultsFunction()}) %...>% (function(result){Resultsdf$data <- result})
  })
  
  observeEvent(Resultsdf$data, {
    print(Resultsdf$data$Sepal.Length)
    print(Resultsdf$data$Species)
  })
  
}

shinyApp(ui, server)

PS: Please consider providing full reproducible examples (not only code snippets).

Related