gpt4 book ai didi

r - Shiny :如何更新 reactiveValues 对象?

转载 作者:行者123 更新时间:2023-12-05 03:14:09 25 4
gpt4 key购买 nike

我有一对辅助输入,允许用户从一组选项中选择组合。此外,能够删除之前创建的项目也很方便。

对于这个任务,一个命名列表,以 reactiveValues 对象的形式,listN <- reactiveValues() , 将负责存储这些信息。

添加项目的功能非常有用,但是当我尝试从 listN 中删除项目时,它的项目名称将永远存在!

我的策略是利用 reactiveValuesToList() , 操纵它的项目并替换 listN使用一个全新的实例 reactiveValues() (或 do.call(reactiveValues, listN_as_list)

我在 Gist 存储了一个可复制的应用程序。我希望这足以让你们帮助我。如果需要,请坚持进行更多说明。

网址:

gist.github.com/d43e72959c4576d27535

在控制台上运行的代码:

shiny::runGist('d43e72959c4576d27535')

提前致谢!

最佳答案

来自 Joe Cheng 的回答 Shiny Google Groups:

Yeah, you can't replace an entire reactiveValues instance like that and expect anything that's bound to the previous reactiveValues instance to instantly know about the new one. The slots on the reactiveValues instance itself are reactive, but its own variable is not.

I think the real issue here is that, unlike lists and envs, you can't remove values from reactiveValues, only set them to NULL.

There are two easy workarounds I can think of:

1) In addition to the reactiveValues instance's slots being reactive, also make the variable reactive, using makeReactiveBinding.

2) You could also use reactiveValues as normal, but keep a list IN the reactiveValues that holds the combinations, not having the reactiveValues itself hold the values. In other words, values <- reactiveValues(combos = list()), and when something new gets added, values$combos[[x]] <- y.

In trying out fix number 1 above, I found that updateSelectInput doesn't work properly when choices is a length-0 vector. Instead of sending a 0-length vector to the client, it doesn't send anything for choices at all, so the choices never change.

I've forked your gist and added two revisions: one that implements workaround #1 (along with some other problems I found), and one that works around the updateSelectInput issue by using renderUI. https://gist.github.com/jcheng5/eaedfed5095d37217fca/revisions

关于r - Shiny :如何更新 reactiveValues 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26803536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com