[英]R Shiny - undo callModule
有没有办法撤消 callModule? 用例是我有可变数量的模块 - 数量响应用户选择。 假设用户选择了 10,然后选择了不同的 10 - 有没有办法删除原来的 10? 它会自动发生吗? 我担心 memory 被占用而不被释放。
创建一个reprex有点棘手,但这是我的意思的一个片段:
observeEvent(
input$people
, {
input$people %>%
walk(
~callModule(people_info_server, .x)
)
}
)
每次向量 input$people 发生变化时,带有 people_info_server 的模块都会在 input$people 的每个元素上被调用,并为每个人生成一个信息页面。
我想做的是这样的:
observeEvent(
input$people
, {
remove_existing_calls(people_info_server) # Not sure how to define this function
input$people %>%
walk(
~callModule(people_info_server, .x)
)
}
)
我想您必须为您的模块创建一个“析构函数”,并确保模块的客户端(“主应用程序”)在正确的时间调用析构函数。 例如,假设模块将析构函数作为闭包返回。
library(shiny)
## module definition
module_with_destructor <- function(input, output, session) {
output$plot <- renderPlot({
plot(1:input$n)
})
destructor <- function() {
# add more cleanup logic here
output$plot <- NULL
}
return(destructor)
}
我们现在需要确保主应用程序在 memory 应该被释放时执行析构函数。
## main app (client)
myenv <- new.env()
observeEvent(input$create_module, {
if (is.null(myenv$destructor))
myenv$destructor <- callModule(module_with_destructor, "module_id")
})
observeEvent(input$destroy_module, {
if (!is.null(myenv$destructor))
myenv$destructor()
})
当然,您必须实现一些额外的逻辑才能将这个想法与动态数量的模块一起使用。 但是,在您的情况下,您可以创建一个列表来收集所有单独的析构函数,然后遍历它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.