Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

r - shiny: How to update a reactiveValues object?

I have a pair of auxiliary inputs that allows user to choose combinations from a set of choices. Also, it is convenient one to be able to remove an item that was created before.

For this task, a named list, in the form of reactiveValues object, listN <- reactiveValues(), will be in charge to store these information.

The function to ADD items is working like a charm, but when I try to REMOVE items from listN, its item names persists forever!

My strategy was make use of reactiveValuesToList(), manipulate its items and replace listN with a brand new instance of reactiveValues() (or do.call(reactiveValues, listN_as_list).

I stored a reproducible app at Gist. I hope it is sufficient to you guys help me out. Please insist in more clarification if needed.

URL:

gist.github.com/d43e72959c4576d27535

Code to run on console:

shiny::runGist('d43e72959c4576d27535')

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Answer from Joe Cheng at 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...