While using browser() with an active Shiny app, I can get a vector of all the inputs created using:
reactiveValuesToList(input) %>% names()

I can also get a vector of all the output names created using:
outputOptions(output) %>% names()

But what about if I want to get a list/vector of all the active observers?
Say make an actionButton with id = mybutton1, it will appear in the list of all inputs.
Now, I create an observer for mybutton1:

observeEvent(input$mybutton1, {
     print("Button clicked!")
})

This observer is now created, right?
Is there a way I can access the list of observers with something like obs$names or getObs() ? (I'm making these up).
If there is, can I simply get the name of an observer to destroy it? rm(obs$mybutton1) or destroy(obs$mybutton1).

推荐答案

观察者的类为Observer,您可以将观察者分配给变量:

library(shiny)

values <- reactiveValues(A = 1)

obsB <- observe({
  print(values$A + 1)
})
obsC <- observe({
  print(values$A * 2)
})

然后,您可以获得存储观察者的所有变量的名称,如下所示:

Filter(function(x) inherits(get(x), "Observer"), ls())
# "obsB" "obsC"

观察者也是一个R6对象,它有一个方法destroy():

obsB$destroy()

但摧毁观察员并不能将其移除:

Filter(function(x) inherits(get(x), "Observer"), ls())
# "obsB" "obsC"

它不会再观察了,仅此而已.因此,如果需要,您必须使用rm.

R相关问答推荐

如何从使用lapply()的r中的拆分数据帧中删除多个部分?

R:随机抽取所有可能排列的样本

生成具有受控相关性的x和y

根据列中的数字移动单元格位置

将Multilinetring合并到一个线串中,使用sf生成规则间隔的点

基于R中的GPS点用方向箭头替换点

从R中的另一个包扩展S3类的正确方法是什么

如何将在HW上运行的R中的消息(错误、警告等)作为批处理任务输出

R:更新后无法运行控制台

在R中创建一个包含转换和转换之间的时间的列

如何编辑ggplot的图例字使用自定义对象(gtable)?'

是否可以创建一个ggplot与整洁判断的交互作用

根据文本字符串中的值粘贴新列

如何将R中数据帧中的任何Nas替换为最后4个值

使用不同的定性属性定制主成分分析中点的 colored颜色 和形状

如何将一个方阵分解成没有循环的立方体

观察器中的inaliateLater的位置

解析嵌套程度极高的地理数据

将多个列合并为一个列的有效方法是什么?

R将函数参数传递给ggploy