I am trying to put together a framework to get Shiny working asynchronously on a set of classes I have, using futures and possibly promises. I have put together a test project using a set of external modules which mimic my real setup.
Note: I have also tried to implement the exact same call that is throwing the error in this framework: FutureProcessor.R, and the returned error is identical.
Basically, the button click calls a function that instantiates an instance of a class, which then carries out a simple calculation. When run with the first button as a straight process, this works fine. However when I run it using the %<-% assigmment it returns the following error: Warning: Error in getClass: "cTest" is not a defined class
It's clear to me that I am not getting this right! However I am not sure whether what I am trying to do is even possible?
Setup as follows:
Shiny app:
## Load required libraries
pacman::p_load(shiny, here, promises, future)
setwd(here())
source(here("testing.R"))
source(here("TestClass.R"))
plan(multisession)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("Test external classes"),
# Sidebar
sidebarLayout(
sidebarPanel(
actionButton("clickMe", "I work"),
actionButton("clickMeToo", "I don't work")
),
# Show a text output
mainPanel(
verbatimTextOutput("outputText1"),
verbatimTextOutput("outputText2")
)
)
)
# Define server logic
server <- function(input, output) {
myResult <- NULL
observeEvent(input$clickMe, {
## This works:
myResult <<- testFutures()
output$outputText1 <- renderText({paste0("test: ", myResult$Item3)})
})
observeEvent(input$clickMeToo, {
## This works not:
myResult %<-% {testFutures()}
output$outputText2 <- renderText({paste0("test: ", myResult$Item3)})
})
}
# Run the application
shinyApp(ui = ui, server = server)
My test class:
cTest <- setRefClass("cTest",
fields=list(
Item1="numeric",
Item2="numeric",
Item3= "numeric"),
methods = list(
Reset = function() {
Item1 <<- 0
Item2 <<- 0
Item3 <<- 0
},
AddUp = function() {
Item3 <<- Item1 + Item2
}
)
My test function:
testFutures <- function() {
output <- new ("cTest")
output$Reset()
output$Item1 <- 3
output$Item2 <- 4
output$AddUp()
return(output)
}