使用带有Shiny,R和Futures的外部类

时间:2018-10-19 08:35:47

标签: r shiny future

我正在尝试建立一个框架,以使Shiny使用期货和可能的Promise在我拥有的一组类上异步工作。我已经使用一组模拟我的实际设置的外部模块来组合一个测试项目。

注意:我还尝试过在该框架中实现引发错误的完全相同的调用:FutureProcessor.R,并且返回的错误是相同的。

基本上,单击按钮会调用一个函数,该函数实例化一个类的实例,然后执行一个简单的计算。当直接使用第一个按钮运行时,此方法工作正常。但是,当我使用%<-%分配运行它时,它返回以下错误:Warning: Error in getClass: "cTest" is not a defined class

对我来说很明显,我做错了!但是我不确定我尝试做的事是否可能?

设置如下:

闪亮的应用程序:

## 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)

我的考试班:

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
                                       }
                                     )

我的测试功能:

testFutures <- function() {
  output <- new ("cTest")
  output$Reset()
  output$Item1 <- 3
  output$Item2 <- 4
  output$AddUp()
  return(output)
}

1 个答案:

答案 0 :(得分:5)

我认为如A Future for R: Common Issues with Solutions中所述,在异步将来中使用引用类存在一些问题。

第一个是缺少全局变量。在运行之前,Future会静态检查代码,以找出需要向R进程公开的全局变量。当您使用new("classname")实例化参考类对象时,直到运行时(调用getClass()时)才知道实际的类定义,因此将来将不会导出它。

一个最小的例子:

library(future)

plan(multisession)

RefClassTest <- setRefClass("RefClassTest",
  fields = list(x = "numeric"),
  methods = list(get = function() x)
)

result %<-% new("RefClassTest")
result
## Error in getClass(Class, where = topenv(parent.frame())) :
##   "RefClassTest" is not a defined class

一种解决方法是使用类似RefClassTest$new()的类生成器实例化。但是,现在您在导出生成器时遇到问题,因为(我想)生成器在内部使用外部指针。该对象的构造不太正确。

options(future.globals.onReference = "warning")

result %<-% RefClassTest$new()
## Warning message:
##   In FALSE :
##   Detected a non-exportable reference (‘externalptr’) in one of the globals (‘RefClassTest’ of class ‘refObjectGenerator’) used in the future expression

result
## Prototypical reference class object

result$get()
## Error in result$get() : object 'x' not found

我对参考类的了解不足以解决这两个问题,因此我建议改用R6类。在将来的表达式中,它们似乎没有与引用类相同的问题。

R6Test <- R6::R6Class("R6Test",
  public = list(
    x = numeric(0),
    get = function() self$x
  )
)

result %<-% {
  R6Test$new()
}

result
## <R6Test>
##   Public:
##   clone: function (deep = FALSE)
##     get: function ()
##       x:

result$get()
## numeric(0)